FrmSqlConnectionStringBuilder.aspx |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title></title> </head> <body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnConnection" runat="server" Text="연결" onclick="btnConnection_Click" />
<asp:Label ID="lblDisplay" runat="server" Text="" ForeColor="Red"></asp:Label>
</div>
</form> </body> </html> |
FrmSqlConnectionStringBuilder.aspx.cs |
protected void btnConnection_Click(object sender, EventArgs
e) {
//[1] 동적으로 데이터베이스 연결 문자열 생성
SqlConnectionStringBuilder
sb = new SqlConnectionStringBuilder();
//[2] 인덱서를 사용해서 값을 입력받음 : SQL 인젝션 해결
sb["Data Source"]
= "WINDOWS-XP\\SQLSERVER";
sb["Initial Catalog"]
= "Test";
sb["User ID"]
= "Test";
sb["Password"]
= "1234";
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString = sb.ConnectionString; //[3] 위에서 지정한 값으로 설정
objCon.Open();
lblDisplay.Text = "연결완료";
objCon.Close(); } |
결과화면 |
[그림2-1] |
'ASP.NET' 카테고리의 다른 글
05.ADO.NET - SqlDataReader (0) | 2009.09.24 |
---|---|
04.ADO.NET - SqlCommand (0) | 2009.09.24 |
03.ADO.NET - SqlException (0) | 2009.09.24 |
01.ADO.NET - SqlConnection (4) | 2009.09.24 |
00.ADO.NET - ADO.NET 이란? (0) | 2009.09.24 |
Comments