封装一个通用的数据库操作类
2008-10-20 16:14 温森特 阅读(1023) 评论(0) 编辑 收藏 举报最近写一个项目,我开发的方式首先设计数据库,然后用网上免费的代码生成软件CodePlus V2.0生成mode对象,同时封装操作方法,在与数据库操作方面我封装了一个数据操作类,很方便,贡献出来。
Code
public static class SqlPlus
{
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
public static string connectionString = "Data Source=spsserver;Initial Catalog=Cooec;uid=sa;pwd=ssssaaaa";
public static SqlTransaction GetTransaction()
{
using (SqlConnection connection = new SqlConnection(SqlPlus.connectionString))
{
connection.Open();
SqlTransaction trans = connection.BeginTransaction();
return trans;
}
}
public static void ConnClose(SqlConnection connection)
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(SqlPlus.connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(SqlPlus.connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(SqlPlus.connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
public static DataTable ExecuteDataTable(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
DataTable oTab = new DataTable();
using (SqlConnection connection = new SqlConnection(SqlPlus.connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(oTab);
}
return oTab;
}
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
public static class Database
{
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, object oValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = oValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, string sValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = sValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, int iValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = iValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, DateTime dValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
if (dValue == DateTime.MinValue)
sqlPar.Value = DBNull.Value;
else
sqlPar.Value = dValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, double dValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = dValue;
return sqlPar;
}
public static string ValueToString(object oValue)
{
if (oValue == System.DBNull.Value)
return "";
else
return oValue.ToString();
}
public static DateTime ValueToDateTime(object oValue)
{
if (oValue == System.DBNull.Value)
return DateTime.MinValue;
else
return Convert.ToDateTime(oValue);
}
public static int ValueToInt(object oValue)
{
if (oValue == System.DBNull.Value)
return int.MinValue;
else
return Convert.ToInt32(oValue);
}
}
public static class SqlPlus
{
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
public static string connectionString = "Data Source=spsserver;Initial Catalog=Cooec;uid=sa;pwd=ssssaaaa";
public static SqlTransaction GetTransaction()
{
using (SqlConnection connection = new SqlConnection(SqlPlus.connectionString))
{
connection.Open();
SqlTransaction trans = connection.BeginTransaction();
return trans;
}
}
public static void ConnClose(SqlConnection connection)
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(SqlPlus.connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(SqlPlus.connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(SqlPlus.connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
public static DataTable ExecuteDataTable(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
DataTable oTab = new DataTable();
using (SqlConnection connection = new SqlConnection(SqlPlus.connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(oTab);
}
return oTab;
}
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
public static class Database
{
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, object oValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = oValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, string sValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = sValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, int iValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = iValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, DateTime dValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
if (dValue == DateTime.MinValue)
sqlPar.Value = DBNull.Value;
else
sqlPar.Value = dValue;
return sqlPar;
}
public static SqlParameter MakeInParam(string parameterName, SqlDbType dbType, int size, double dValue)
{
SqlParameter sqlPar = new SqlParameter(parameterName, dbType, size);
sqlPar.Value = dValue;
return sqlPar;
}
public static string ValueToString(object oValue)
{
if (oValue == System.DBNull.Value)
return "";
else
return oValue.ToString();
}
public static DateTime ValueToDateTime(object oValue)
{
if (oValue == System.DBNull.Value)
return DateTime.MinValue;
else
return Convert.ToDateTime(oValue);
}
public static int ValueToInt(object oValue)
{
if (oValue == System.DBNull.Value)
return int.MinValue;
else
return Convert.ToInt32(oValue);
}
}
您的支持,我的动力!