通用数据访问
Code
/* 在开发过程中数据库可能用的很多,可能即用sqlserver 又用 oracle 等等........,总结了几次在网上看了很多资料写了个觉得还可以用的,需要修改的地方很多希望各位大哥指教,小弟在这里先谢了 */
namespace Connection
{
class Program
{
static private string connString, sql;
static IDbConnection conn = null;
static IDbDataAdapter adapter = null;
static void Main(string[] args)
{
int dbconn = Convert.ToInt32(Console.ReadLine());
GenericAdoNetComp genDP = new GenericAdoNetComp();
sql = "select * from spt_values";
switch (dbconn)
{
case 1:
connString =
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\Northwind.mdb";
/* 取得数据库连接对象 ACCESS数据库代码连接 */
conn = genDP.GetConnection(ConnEnum.DbTypeConn.Oledb, connString);
adapter = genDP.GetDataAdapter(ConnEnum.DbTypeConn.Oledb, connString, sql);
break;
case 2:
/*sqlserver 数据库练级*/
connString =
"Data Source=.;Initial Catalog=master;user id=sa;password=sa;";
conn = genDP.GetConnection(ConnEnum.DbTypeConn.Sql, connString);
adapter = genDP.GetDataAdapter(ConnEnum.DbTypeConn.Sql, connString, sql);
break;
case 3:
conn = genDP.GetConnection(ConnEnum.DbTypeConn.Odbc, connString);
adapter = genDP.GetDataAdapter(ConnEnum.DbTypeConn.Odbc, connString, sql);
break;
}
try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
Console.Write(ds.Tables[0].Rows[i][j]);
}
}
}
}
catch (Exception exp)
{
throw exp;
}
finally
{
conn.Close();
}
}
}
}
/*枚举 */
namespace ConnEnum
{
public enum DbTypeConn
{
Oledb,
Sql,
Odbc,
}
}
/*接口,在这里简单的写了下三层模式没有写工厂模式,请见谅 */
namespace Connection
{
interface IConnection
{
IDbConnection GetConnection(DbTypeConn connType, string connString);
IDbDataAdapter GetDataAdapter(DbTypeConn connType, string connString, string sql);
}
}
/* BLL */
namespace myenm
{
public class GenericAdoNetComp : Connection.IConnection
{
private IDbConnection idbConn = null;
private IDbDataAdapter idbAdapter = null;
public GenericAdoNetComp()
{
}
/* GetConnection returns IDbConnection */
public IDbConnection GetConnection(DbTypeConn ConnDbType, string connString)
{
switch (ConnDbType)
{
case ConnEnum.DbTypeConn.Oledb: // OleDb Data Provider
idbConn = new OleDbConnection(connString);
break;
case ConnEnum.DbTypeConn.Sql: // Sql Data Provider
idbConn = new SqlConnection(connString);
break;
case ConnEnum.DbTypeConn.Odbc: // ODBC Data Provider
idbConn = new OdbcConnection(connString);
break;
// case 3: // Add your custom data provider
default:
break;
}
return idbConn;
}
/* GetDataAdapter returns IDbDataAdapter 使用适配器模式读取数据 */
public IDbDataAdapter GetDataAdapter(DbTypeConn ConnDbType,
string connString, string sql)
{
switch (ConnDbType)
{
case ConnEnum.DbTypeConn.Oledb: // OleDb Data Provider
idbAdapter = new OleDbDataAdapter(sql, connString);
break;
case ConnEnum.DbTypeConn.Sql: // Sql Data Provider
idbAdapter = new SqlDataAdapter(sql, connString);
break;
case ConnEnum.DbTypeConn.Odbc: // ODBC Data Provider
idbAdapter = new OdbcDataAdapter(sql, connString);
break;
// case 3: // Add your custom data provider
default:
break;
}
return idbAdapter;
}
}
}
/* 在开发过程中数据库可能用的很多,可能即用sqlserver 又用 oracle 等等........,总结了几次在网上看了很多资料写了个觉得还可以用的,需要修改的地方很多希望各位大哥指教,小弟在这里先谢了 */
namespace Connection
{
class Program
{
static private string connString, sql;
static IDbConnection conn = null;
static IDbDataAdapter adapter = null;
static void Main(string[] args)
{
int dbconn = Convert.ToInt32(Console.ReadLine());
GenericAdoNetComp genDP = new GenericAdoNetComp();
sql = "select * from spt_values";
switch (dbconn)
{
case 1:
connString =
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\Northwind.mdb";
/* 取得数据库连接对象 ACCESS数据库代码连接 */
conn = genDP.GetConnection(ConnEnum.DbTypeConn.Oledb, connString);
adapter = genDP.GetDataAdapter(ConnEnum.DbTypeConn.Oledb, connString, sql);
break;
case 2:
/*sqlserver 数据库练级*/
connString =
"Data Source=.;Initial Catalog=master;user id=sa;password=sa;";
conn = genDP.GetConnection(ConnEnum.DbTypeConn.Sql, connString);
adapter = genDP.GetDataAdapter(ConnEnum.DbTypeConn.Sql, connString, sql);
break;
case 3:
conn = genDP.GetConnection(ConnEnum.DbTypeConn.Odbc, connString);
adapter = genDP.GetDataAdapter(ConnEnum.DbTypeConn.Odbc, connString, sql);
break;
}
try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
Console.Write(ds.Tables[0].Rows[i][j]);
}
}
}
}
catch (Exception exp)
{
throw exp;
}
finally
{
conn.Close();
}
}
}
}
/*枚举 */
namespace ConnEnum
{
public enum DbTypeConn
{
Oledb,
Sql,
Odbc,
}
}
/*接口,在这里简单的写了下三层模式没有写工厂模式,请见谅 */
namespace Connection
{
interface IConnection
{
IDbConnection GetConnection(DbTypeConn connType, string connString);
IDbDataAdapter GetDataAdapter(DbTypeConn connType, string connString, string sql);
}
}
/* BLL */
namespace myenm
{
public class GenericAdoNetComp : Connection.IConnection
{
private IDbConnection idbConn = null;
private IDbDataAdapter idbAdapter = null;
public GenericAdoNetComp()
{
}
/* GetConnection returns IDbConnection */
public IDbConnection GetConnection(DbTypeConn ConnDbType, string connString)
{
switch (ConnDbType)
{
case ConnEnum.DbTypeConn.Oledb: // OleDb Data Provider
idbConn = new OleDbConnection(connString);
break;
case ConnEnum.DbTypeConn.Sql: // Sql Data Provider
idbConn = new SqlConnection(connString);
break;
case ConnEnum.DbTypeConn.Odbc: // ODBC Data Provider
idbConn = new OdbcConnection(connString);
break;
// case 3: // Add your custom data provider
default:
break;
}
return idbConn;
}
/* GetDataAdapter returns IDbDataAdapter 使用适配器模式读取数据 */
public IDbDataAdapter GetDataAdapter(DbTypeConn ConnDbType,
string connString, string sql)
{
switch (ConnDbType)
{
case ConnEnum.DbTypeConn.Oledb: // OleDb Data Provider
idbAdapter = new OleDbDataAdapter(sql, connString);
break;
case ConnEnum.DbTypeConn.Sql: // Sql Data Provider
idbAdapter = new SqlDataAdapter(sql, connString);
break;
case ConnEnum.DbTypeConn.Odbc: // ODBC Data Provider
idbAdapter = new OdbcDataAdapter(sql, connString);
break;
// case 3: // Add your custom data provider
default:
break;
}
return idbAdapter;
}
}
}
出处:http://www.cnblogs.com/liuyong/
作者喜欢研究 Sql Server ,ASP.NET MVC , Jquery WCF 等技术,同时关心分布式架构的设计应用。转载请保留原文链接,谢谢!