ASP.NET开发 封装的DB类(一)

本文介绍了使用C#进行数据库操作的方法,包括参数处理、创建可执行的Command、执行SQL语句及获取DataSet对象的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是我对在C#开发过程中用到的数据库BaseDao的有关代码的整理

1对传入参数的处理

  private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
        {
if( command == null ) throw new ArgumentNullException( "command" );
if( commandParameters != null )
{
foreach (SqlParameter p in commandParameters)
{
if( p != null )
{
// Check for derived output value with no value assigned
if ( ( p.Direction == ParameterDirection.InputOutput || 
p.Direction == ParameterDirection.Input ) && 
(p.Value == null))
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
}
        } 

2  用于创建一个关联Connection可以执行的Command,

 private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection )
        {
if( command == null ) throw new ArgumentNullException( "command" );
if( commandText == null || commandText.Length == 0 ) throw new ArgumentNullException( "commandText" );


            // If the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open)
{
mustCloseConnection = true;
connection.Open();
                
}
else
{
mustCloseConnection = false;
}
            
            // Associate the connection with the command
            command.Connection = connection;
            command.CommandTimeout = 0;
            // Set the command text (stored procedure name or SQL statement)
            command.CommandText = commandText;


            // If we were provided a transaction, assign it
            if (transaction != null)
            {
if( transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );
                command.Transaction = transaction;
            }


            // Set the command type
            command.CommandType = commandType;


            // Attach the command parameters if they are provided
            if (commandParameters != null)
            {
                AttachParameters(command, commandParameters);
            }
            return;
        }

3执行sql语句

      public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
        {
if( connection == null ) throw new ArgumentNullException( "connection" );


            // Create a command and prepare it for execution
            SqlCommand cmd = new SqlCommand();cmd.CommandTimeout = 0;
bool mustCloseConnection = false;
            PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
   
            // Finally, execute the command
            int retval = cmd.ExecuteNonQuery();
   
            // Detach the SqlParameters from the command object, so they can be used again
            cmd.Parameters.Clear();
if( mustCloseConnection )
connection.Close();
            return retval;
        }

4  获取dataset对象

public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if( transaction == null ) throw new ArgumentNullException( "transaction" );
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );


// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();cmd.CommandTimeout = 0;
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );
   
// Create the DataAdapter & DataSet
using( SqlDataAdapter da = new SqlDataAdapter(cmd) )
{
DataSet ds = new DataSet();


// Fill the DataSet using default values for DataTable names, etc
da.Fill(ds);
   
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();


// Return the dataset
return ds;
}
}


个人曾经写过的数据访问接口,包含:MSSQL、Mysql、Oracle等数据库的公共处理接口。可以拿过来直接使用,放在手里好多年了。 IDatabase接口声明如下: namespace Simple.Database { /// /// IDatabase 接口 /// public interface IDatabase { DbConnection dbConn { get; set; } /// /// 创建 DbConnection 对象实例。 /// /// DbConnection 对象实例。 DbConnection CreateConnection(); /// /// 创建 DbCommand 对象实例。 /// /// DbCommand 对象实例。 DbCommand CreateCommand(); /// /// 创建 DbCommand 对象实例。 /// /// Sql 语句或存储过程名。 /// CommandType 参数。 /// DbCommand 对象实例。 DbCommand CreateCommand(string text, CommandType type); /// /// 创建 DbCommand 对象实例。 /// /// Sql 语句或存储过程名。 /// CommandType 参数。 /// 参数集合。 /// DbCommand 对象实例。 DbCommand CreateCommand(string text, CommandType type, IDataParameter[] paras); /// /// 创建 DbCommand 对象实例。 /// /// DbConnection 对象。 /// Sql 语句或存储过程名。 /// CommandType 参数。 /// 参数集合。 /// DbCommand 对象实例。 DbCommand CreateCommand(DbConnection conn, string text, CommandType type, IDataParameter[] paras); /// /// 创建 DbDataAdapter 对象实例。 /// /// DbDataAdapter 对象实例。 DbDataAdapter CreateDataAdapter(); /// /// 创建 DbParameter 对象实例。 /// /// DbParameter 对象实例。 DbParameter CreateParameter(); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数值。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, Object value); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数型。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, DbType type); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数型。 /// 数据的最大大小。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, DbType type, int size); /// /// 获取指定长度数据的 DataSet 对象。 /// /// 要读取的 Sql 语句。 /// 开始读取位置的索引。 /// 待读取记录集的长度。 /// DataSet 对象。 DataSet GetDataSet(string sql, int start, int length); /// /// 获取指定长度数据的 DataTable 对象。 /// /// 要读取的 Sql 语句。 /// 开始读取位置的索引。 /// 待读取记录集的长度。 /// DataTable 对象。 DataTable GetDataTable(string sql, int start, int length); /// /// 执行Insert、Update、Delete等操作,并返回受影响的记录数。 /// /// 要执行的 Sql 语句。 /// 受影响的记录数。 int GetEffect(string sql); /// /// 执行 Insert、Update、Delete 等操作,并返回受影响的记录数。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 的型,即该命令是 Sql 语句,还是存储过程名等。 /// 受影响的记录数。 int GetEffect(string sql, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回受影响的记录数。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// 受影响的记录数。 int GetEffect(string text, CommandType type, IDataParameter[] paras); /// /// /// /// /// /// /// /// /// int GetEffect(DbConnection conn, string text, CommandType type, IDataParameter[] paras, DbTransaction DbTrans); /// /// /// /// /// List ExecuteTransaction(params string[] sqls); /// /// 执行 Select 语句,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句。 /// DataSet 对象。 DataSet GetDataSet(string sql); /// /// 执行 Select 语句或存储过程,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数型,即该命令是 sql 语句,还是存储过程名等。 /// DataSet 对象。 DataSet GetDataSet(string text, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// DataSet 对象。 DataSet GetDataSet(string text, CommandType type, IDataParameter[] paras); /// /// 执行 Select 语句,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句。 /// DataTable 对象。 DataTable GetDataTable(string sql); /// /// 执行 Select 语句或存储过程,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数型,即该命令是 sql 语句,还是存储过程名等。 /// DataTable 对象。 DataTable GetDataTable(string text, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// DataTable 对象。 DataTable GetDataTable(string text, CommandType type, IDataParameter[] paras); /// /// 获取查询所返回的结果集中第行第列的值。 /// /// 要处理的 sql 语句(包含待查询的字段)。 /// 字段值。 object GetField(string sql); /// /// 获取查询所返回的结果集中第行指定列的值。 /// /// 待查询的数据表名称。 /// 待获取字段的列名。 /// 字段值。 object GetField(string sql, string field); /// /// 获取查询所返回的结果集中第行指定列集合的值。 /// /// 要处理的 sql 语句。 /// 待获取字段的列表。 /// 字段值集合。 object[] GetField(string sql, params string[] fields); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值