Web.config 파일 중 일부...
<!--[1] 데이터베이스 연결 문자열 기록 -->
<connectionStrings>
<!-- SQL Server 전용 -->
<add name="ConnectionString" connectionString="Password=Root;Persist Security Info=True;User ID=Root;Initial Catalog=Root;Data Source=." providerName="System.Data.SqlClient"/>
<!-- Oracle 전용
<add name="ConnectionString"
connectionString="Provider=MSDAORA.1;
Data Source=//172.16.10.108/orcl;Persist Security Info=True;
User ID=scott;Password=scott;Unicode=True"
providerName="System.Data.OleDb" /> -->
<!-- MS Access 전용
<add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\RedPlus.Book\!ASP.NET 2.0 웹 사이트 프로그래밍.소스\ASPNET2\WebMemoJSON\App_Data\MemoDB.mdb;Persist Security Info=True"
providerName="System.Data.OleDb" /> -->
</connectionStrings>
웹 폼 중 일부...
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 CategoryID 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();
}