因为这个是自己没事练手写的,所以呢不太完善请大家见谅。写这个类的目的是希望达到在改动最小的情况下,各个数据库之间的切换,以方便程序的通用性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
namespace Ninja.Classes
{
public enum DataBaseType
{
//System.Data.Odbc、System.Data.OleDb、System.Data.SqlClient、System.Data.SqlServerCe 和 System.Data.OracleClient
ODBC=0,
OLEDB,
SQLCLIENT,
SQLSERVERCE,
ORACLECLIENT
}
//生成数据库对应的字符串
public class DataBaseTypeFactory
{
public static string GetDataBaseTypeString(DataBaseType dbType)
{
string msg = null;
switch (dbType)
{
case DataBaseType.ODBC:
msg = "System.Data.Odbc";
break;
case DataBaseType.OLEDB:
msg = "System.Data.OleDb";
break;
case DataBaseType.ORACLECLIENT:
msg = "System.Data.OracleClient";
break;
case DataBaseType.SQLCLIENT:
msg = "System.Data.SqlClient";
break;
case DataBaseType.SQLSERVERCE:
msg = "System.Data.SqlServerCe";
break;
default:
msg = "System.Data.SqlClient";
break;
}
return msg;
}
}
public class DataBaseHelper : IDisposable
{
private DbDataAdapter adapter;
private DbConnection connection;
private DbCommand command;
private string dataBaseType;
#region 属性定义
public string ConnectionString
{
get;
set;
}
public string CommandText
{
get;
set;
}
public CommandType CmdType
{
get;
set;
}
public string DBType
{
get
{
return dataBaseType;
}
set
{
this.dataBaseType = value;
}
}
#endregion
#region 构造函数定义
public DataBaseHelper(string dbType, CommandType cmdType)
{
this.dataBaseType = dbType;
this.ConnectionString = "";
this.CmdType = cmdType;
this.CommandText = "";
}
public DataBaseHelper(string dbType)
: this(dbType, CommandType.Text)
{
}
public DataBaseHelper()
: this(DataBaseType.SQLCLIENT.ToString())
{
}
#endregion
private void initializeDB()
{
try
{
DbProviderFactory factory = DbProviderFactories.GetFactory(this.dataBaseType);
this.connection = factory.CreateConnection();
this.command = factory.CreateCommand();
this.adapter = factory.CreateDataAdapter();
}
catch (DbException e)
{
throw e;
}
}
public void ConnectAndOpen()
{
if (this.ConnectionString == String.Empty)
{
throw new ArgumentException("ConnectionString is not allow empty.", "ConnectionString");
}
//初始化数据库相关组件
initializeDB();
this.connection.ConnectionString = this.ConnectionString;
try
{
this.connection.Open();
}
catch (DbException e)
{
throw e;
}
}
private void commandInitialize(DbParameter[] parameters)
{
this.command.Connection = this.connection;
this.command.CommandText = this.CommandText;
this.command.CommandType = this.CmdType;
if (null != parameters)
{
this.command.Parameters.AddRange(parameters);
}
}
public int ExecuteNonQuery(DbParameter[] parameters)
{
commandInitialize(parameters);
int effectRows = 0;
try
{
effectRows = this.command.ExecuteNonQuery();
}
catch(DbException e)
{
throw e;
}
return effectRows;
}
public DataSet Execute(DbParameter[] parameters, string tableName)
{
commandInitialize(parameters);
DataSet ds = new DataSet();
try
{
this.adapter.SelectCommand = this.command;
this.adapter.Fill(ds, tableName);
}
catch (SystemException e)
{
throw e;
}
return ds;
}
public void Close()
{
if (this.connection.State == ConnectionState.Open)
this.connection.Close();
}
public void Dispose()
{
if (this.connection.State == ConnectionState.Open)
this.connection.Close();
}
}
}