FrmDbProviderFactory.aspx |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title></title> </head> <body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="ctlCategoryList" runat="server">
</asp:GridView>
</div>
</form> </body> </html> |
FrmDbProviderFactory.aspx.cs |
protected void Page_Load(object
sender, EventArgs
e) {
if (!Page.IsPostBack)
{
DisplayData();
} } private void DisplayData() {
//[!] SQL Server, Oracle, Access DB에서 모두 똑같은 코드로 작성하고자한다면???
//[1] Configuration 정보 가져오기 : ProviderName에 따른 공급자 결정
DbProviderFactory
factory = DbProviderFactories.GetFactory( ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName);
//[2] 커넥션 : SqlConnection, OleDbConnection 등을 DbConnection으로 통일
DbConnection con =
factory.CreateConnection();
con.ConnectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
con.Open();
//[3] 커멘드 : SqlCommand, OracleCommand 등을 DbCommand로 공통처리
DbCommand cmd =
factory.CreateCommand();
cmd.Connection = con;
cmd.CommandText =
"Select * From Categories Where Num
Between @First And @Second";
cmd.CommandType = System.Data.CommandType.Text;
//[!] 파라미터 추가
DbParameter first =
cmd.CreateParameter();
first.ParameterName = "@First";
first.DbType = System.Data.DbType.Int32;
first.Value = 1;
DbParameter second =
cmd.CreateParameter();
second.ParameterName = "@Second";
second.DbType = System.Data.DbType.Int32;
second.Value = 20;
cmd.Parameters.Add(first);
cmd.Parameters.Add(second);
//[4] 데이터리더 : SqlDataReader, OleDbDataReader => DbDataReader DbDataReader dr = cmd.ExecuteReader();
//[5] 바인딩
this.ctlCategoryList.DataSource
= dr;
this.ctlCategoryList.DataBind();
//[6] 마무리
con.Close(); } |
결과확인 |
[그림13-1] |
'ASP.NET' 카테고리의 다른 글
15.ADO.NET - DataSet (0) | 2009.09.28 |
---|---|
14.ADO.NET - Transaction (0) | 2009.09.28 |
12.ADO.NET - Parameters (0) | 2009.09.25 |
11.ADO.NET - ExecuteScalar (0) | 2009.09.25 |
10.ADO.NET - ConfigurationManager (0) | 2009.09.25 |
Comments