下面给出了一个C#操作MS SQL Server 数据库的通用类,通过该类可以对数据库进行任何操作,包括执行SQL语句、执行存储过程。以下是其详细实现过程,希望大家共同修改优化之。稍后将介绍如何使用它实现N层的程序设计。
配置web.config文件的链接参数
C#代码
using System;
using System.Data;
using System.Data.SqlClient;
namespace Com.LXJ.Database
{
///
/// ConnDB 的摘要说明。
///
public class ConnDB
{
protected SqlConnection Connection;
private string connectionString;
///
/// 默认构造函数
///
public ConnDB()
{
string connStr;
connStr = System.Configuration.ConfigurationSettings.AppSettings["connStr"].ToString();
connectionString = connStr;
Connection = new SqlConnection(connectionString);
}
///
/// 带参数的构造函数
///
/// 数据库联接字符串
public ConnDB(string newConnectionString)
{
connectionString = newConnectionString;
Connection = new SqlConnection(connectionString);
}
///
/// 完成SqlCommand对象的实例化
///
///
///
///
private SqlCommand BuildCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(storedProcName,parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null));
return command;
}
///
/// 创建新的SQL命令对象(存储过程)
///
///
///
///
private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName,Connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
///
/// 执行存储过程,无返回值
///
///
///
public void ExecuteProcedure(string storedProcName,IDataParameter[] parameters)
{
Connection.Open();
SqlCommand command;
command=BuildQueryCommand(storedProcName,parameters);
command.ExecuteNonQuery();
Connection.Close();
}
///
/// 执行存储过程,返回执行操作影响的行数目
///
///
///
///
///
public int RunProcedure(string storedProcName,IDataParameter[] parameters,out int rowsAffected)
{
int result;
Connection.Open();
SqlCommand command = BuildCommand(storedProcName,parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
Connection.Close();
return result;
}
///
/// 重载RunProcedure把执行存储过程的结果放在SqlDataReader中
///
///
///
///
public SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)
{
SqlDataReader returnReader;
Connection.Open();
SqlCommand command = BuildQueryCommand(storedProcName,parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
///
/// 重载RunProcedure把执行存储过程的结果存储在DataSet中和表tableName为可选参数
///
///
///
///
///
public DataSet RunProcedure(string storedProcName,IDataParameter[] parameters,params string[] tableName)
{
DataSet dataSet = new DataSet();
Connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(storedProcName,parameters);
string flag;
flag = "";
for(int i=0;i
if (flag!="")
sqlDA.Fill(dataSet,tableName[0]);
else
sqlDA.Fill(dataSet);
Connection.Close();
return dataSet;
}
///
/// 执行SQL语句,返回数据到DataSet中
///
///
///
public DataSet ReturnDataSet(string sql)
{
DataSet dataSet=new DataSet();
Connection.Open();
SqlDataAdapter sqlDA=new SqlDataAdapter(sql,Connection);
sqlDA.Fill(dataSet,"objDataSet");
Connection.Close();
return dataSet;
}
///
/// 执行SQL语句,返回 DataReader
///
///
///
public SqlDataReader ReturnDataReader(String sql)
{
Connection.Open();
SqlCommand command = new SqlCommand(sql,Connection);
SqlDataReader dataReader = command.ExecuteReader();
return dataReader;
}
///
/// 执行SQL语句,返回记录数
///
///
///
public int ReturnRecordCount(string sql)
{
int recordCount = 0;
Connection.Open();
SqlCommand command = new SqlCommand(sql,Connection);
SqlDataReader dataReader = command.ExecuteReader();
while(dataReader.Read())
{
recordCount++;
}
dataReader.Close();
Connection.Close();
return recordCount;
}
///
/// 执行SQL语句
///
///
///
public bool EditDatabase(string sql)
{
bool successState = false;
Connection.Open();
SqlTransaction myTrans = Connection.BeginTransaction();
SqlCommand command = new SqlCommand(sql,Connection,myTrans);
try
{
command.ExecuteNonQuery();
myTrans.Commit();
successState = true;
}
catch
{
myTrans.Rollback();
}
finally
{
Connection.Close();
}
return successState;
}
///
/// 关闭数据库联接
///
public void Close()
{
Connection.Close();
}
}//end class
}//end namespace
稍后将介使用它实现N层设计的事例,在这之前您需要熟悉存储过程的知识