ms-sql_asp.aspx
===========================================================================
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”ms-sql_asp.aspx.cs” Inherits=”FrmSqlConnectionStringBuilder” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<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>
===================================================================
ms-sql_asp.aspx.as
========================================================================
using System;
using System.Data.SqlClient; //[0]
public partial class FrmSqlConnectionStringBuilder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
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();
}
}
==========================================================================