这是一个借鉴而来的,正在项目中使用的DataInterface数据访问接口。请大家讨论指正!
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace TSB1Services
{
/**//// <summary>
/// DBInterface类主要用来实现对数据库的公共访问
/// 实现的方法用来对SQL Server数据库进行操作
/// </summary>
public abstract class DBInterface
{
//数据库连接字符串
//public static readonly string CONN_STRING = System.Configuration.ConfigurationSettings.AppSettings["SQLConnString"];
//public static readonly string CONN_STRING = "workstation id=localhost;Integrated Security=SSPI;database = xx";
public static string CONN_STRING = globals.mc.ConnectString;
执行SQL命令没有返回的结果集,只返回受影响的行数#region 执行SQL命令没有返回的结果集,只返回受影响的行数
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回受影响的行数.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>返回执行SQL命令受影响的行数</returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
#endregion
/**//// <summary>
///
/// </summary>
/// <param name="cmdText"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string cmdText)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
int val = cmd.ExecuteNonQuery();
cmd.CommandText = "";
return val;
}
}
执行SQL命令没有返回的结果集,只返回受影响的行数#region 执行SQL命令没有返回的结果集,只返回受影响的行数
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回受影响的行数.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">SQL数据库事务</param>
/// <param name="commandType">SQL命令类型(stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>返回执行SQL命令受影响的行数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
#endregion
执行SQL命令返回只读只进的SqlDataReader#region 执行SQL命令返回只读只进的SqlDataReader
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回SqlDataReader.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型(stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>包含执行结果的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(CONN_STRING);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
#endregion
执行SQL命令返回只读只进的SqlDataReader#region 执行SQL命令返回只读只进的SqlDataReader
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回SqlDataReader.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>包含执行结果的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(CONN_STRING);
try
{
cmd.Connection = conn;
cmd.CommandType = cmdType;
cmd.CommandText = cmdText;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch
{
conn.Close();
throw;
}
}
#endregion
执行SQL命令返回DataTable#region 执行SQL命令返回DataTable
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataTable.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet("select * from PublishOrders", "PublishOrders");
/// </remarks>
/// <param name="SqlString">需要执行的T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataTable的数据表名</param>
/// <returns>包含执行结果的DataTable</returns>
public static DataTable ExecuteDataTable(string SqlString, string TableName)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter myCommand = new SqlDataAdapter(SqlString, conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, TableName);
DataTable dtName = ds.Tables[TableName].Copy();
ds.Dispose();
return dtName;
}
}
#endregion
执行SQL命令返回DataTable#region 执行SQL命令返回DataTable
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataTable.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet(CommandType.StoredProcedure, "PublishOrders","Orders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataTable的数据表名</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>包含执行结果的DataTable</returns>
public static DataTable ExecuteDataTable(CommandType cmdType, string cmdText, string tableName, params SqlParameter[] cmdParms)
{
DataSet dataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = new SqlCommand();
PrepareCommand(sqlDA.SelectCommand, conn, null, cmdType, cmdText, cmdParms);
sqlDA.Fill(dataSet, tableName);
DataTable dtName = dataSet.Tables[tableName].Copy();
dataSet.Dispose();
sqlDA.SelectCommand.Parameters.Clear();
return dtName;
}
}
#endregion
执行SQL命令返回DataSet#region 执行SQL命令返回DataSet
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataSet.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet("select * from PublishOrders", "PublishOrders");
/// </remarks>
/// <param name="SqlString">需要执行的T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataSet中的数据表名</param>
/// <returns>包含执行结果的DataSet</returns>
public static DataSet ExecuteDataSet(string SqlString, string TableName)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter myCommand = new SqlDataAdapter(SqlString, conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, TableName);
return ds;
}
}
#endregion
执行SQL命令返回DataSet#region 执行SQL命令返回DataSet
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataSet.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet(CommandType.StoredProcedure, "PublishOrders","Orders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataSet中的数据表名</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>包含执行结果的DataSet</returns>
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tableName, params SqlParameter[] cmdParms)
{
DataSet dataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = new SqlCommand();
PrepareCommand(sqlDA.SelectCommand, conn, null, cmdType, cmdText, cmdParms);
sqlDA.Fill(dataSet, tableName);
sqlDA.SelectCommand.Parameters.Clear();
return dataSet;
}
}
#endregion
准备执行SQL语句#region 准备执行SQL语句
/**//// <summary>
/// 为执行SQL语句准备SqlCommand对象
/// </summary>
/// <param name="cmd">SqlCommand 对象</param>
/// <param name="conn">SqlConnection 对象</param>
/// <param name="trans">SqlTransaction 对象</param>
/// <param name="cmdType">SQL命令类型 stored procedure or text</param>
/// <param name="cmdText">存储过程名或T-SQL语句 Select * from Products</param>
/// <param name="cmdParms">执行命令用到的SQL参数集合</param>
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);
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace TSB1Services
{
/**//// <summary>
/// DBInterface类主要用来实现对数据库的公共访问
/// 实现的方法用来对SQL Server数据库进行操作
/// </summary>
public abstract class DBInterface
{
//数据库连接字符串
//public static readonly string CONN_STRING = System.Configuration.ConfigurationSettings.AppSettings["SQLConnString"];
//public static readonly string CONN_STRING = "workstation id=localhost;Integrated Security=SSPI;database = xx";
public static string CONN_STRING = globals.mc.ConnectString;
执行SQL命令没有返回的结果集,只返回受影响的行数#region 执行SQL命令没有返回的结果集,只返回受影响的行数
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回受影响的行数.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>返回执行SQL命令受影响的行数</returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
#endregion
/**//// <summary>
///
/// </summary>
/// <param name="cmdText"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string cmdText)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
int val = cmd.ExecuteNonQuery();
cmd.CommandText = "";
return val;
}
}
执行SQL命令没有返回的结果集,只返回受影响的行数#region 执行SQL命令没有返回的结果集,只返回受影响的行数
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回受影响的行数.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">SQL数据库事务</param>
/// <param name="commandType">SQL命令类型(stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>返回执行SQL命令受影响的行数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
#endregion
执行SQL命令返回只读只进的SqlDataReader#region 执行SQL命令返回只读只进的SqlDataReader
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回SqlDataReader.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型(stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>包含执行结果的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(CONN_STRING);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
#endregion
执行SQL命令返回只读只进的SqlDataReader#region 执行SQL命令返回只读只进的SqlDataReader
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回SqlDataReader.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>包含执行结果的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(CONN_STRING);
try
{
cmd.Connection = conn;
cmd.CommandType = cmdType;
cmd.CommandText = cmdText;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch
{
conn.Close();
throw;
}
}
#endregion
执行SQL命令返回DataTable#region 执行SQL命令返回DataTable
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataTable.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet("select * from PublishOrders", "PublishOrders");
/// </remarks>
/// <param name="SqlString">需要执行的T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataTable的数据表名</param>
/// <returns>包含执行结果的DataTable</returns>
public static DataTable ExecuteDataTable(string SqlString, string TableName)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter myCommand = new SqlDataAdapter(SqlString, conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, TableName);
DataTable dtName = ds.Tables[TableName].Copy();
ds.Dispose();
return dtName;
}
}
#endregion
执行SQL命令返回DataTable#region 执行SQL命令返回DataTable
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataTable.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet(CommandType.StoredProcedure, "PublishOrders","Orders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataTable的数据表名</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>包含执行结果的DataTable</returns>
public static DataTable ExecuteDataTable(CommandType cmdType, string cmdText, string tableName, params SqlParameter[] cmdParms)
{
DataSet dataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = new SqlCommand();
PrepareCommand(sqlDA.SelectCommand, conn, null, cmdType, cmdText, cmdParms);
sqlDA.Fill(dataSet, tableName);
DataTable dtName = dataSet.Tables[tableName].Copy();
dataSet.Dispose();
sqlDA.SelectCommand.Parameters.Clear();
return dtName;
}
}
#endregion
执行SQL命令返回DataSet#region 执行SQL命令返回DataSet
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataSet.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet("select * from PublishOrders", "PublishOrders");
/// </remarks>
/// <param name="SqlString">需要执行的T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataSet中的数据表名</param>
/// <returns>包含执行结果的DataSet</returns>
public static DataSet ExecuteDataSet(string SqlString, string TableName)
{
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter myCommand = new SqlDataAdapter(SqlString, conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, TableName);
return ds;
}
}
#endregion
执行SQL命令返回DataSet#region 执行SQL命令返回DataSet
/**//// <summary>
/// 通过连接字符串建立SqlConnection,执行SqlCommand,返回DataSet.
/// 通过以下参数
/// </summary>
/// <remarks>
/// e.g.:
/// DataSet r = ExecuteDataSet(CommandType.StoredProcedure, "PublishOrders","Orders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">SQL命令类型 (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="TableName">返回结果集保存在DataSet中的数据表名</param>
/// <param name="commandParameters">执行命令用到的SQL参数集合</param>
/// <returns>包含执行结果的DataSet</returns>
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tableName, params SqlParameter[] cmdParms)
{
DataSet dataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = new SqlCommand();
PrepareCommand(sqlDA.SelectCommand, conn, null, cmdType, cmdText, cmdParms);
sqlDA.Fill(dataSet, tableName);
sqlDA.SelectCommand.Parameters.Clear();
return dataSet;
}
}
#endregion
准备执行SQL语句#region 准备执行SQL语句
/**//// <summary>
/// 为执行SQL语句准备SqlCommand对象
/// </summary>
/// <param name="cmd">SqlCommand 对象</param>
/// <param name="conn">SqlConnection 对象</param>
/// <param name="trans">SqlTransaction 对象</param>
/// <param name="cmdType">SQL命令类型 stored procedure or text</param>
/// <param name="cmdText">存储过程名或T-SQL语句 Select * from Products</param>
/// <param name="cmdParms">执行命令用到的SQL参数集合</param>
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);
}
}
#endregion
}
}