http://blog.csdn.net/notrynogain/archive/2007/10/25/1843974.aspx
说明:以下是我近两年来开发中最常用的C#操作sql server数据库访问类,对初学者非常有用,多研究研究,有什么问题联系我QQ:115216552,在web.config中配置一下连接字符串就可用了.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
public class SqlClass
{
protected SqlConnection Connection;
protected string connectionstring;
public SqlClass()
{
connectionstring = System.Configuration.ConfigurationSettings.AppSettings["connString"];
Connection = new SqlConnection(connectionstring);
}
public SqlClass(string connstring)
{
connectionstring = connstring;
Connection = new SqlConnection(connectionstring);
}
/*执行返回结果和不返回结果的存储过程方法*/
///
/// 根据存储过程名称和参数生成对应的SQL命令对象
///
/// 存储过程名
/// 存储过程参数
///
private SqlCommand BuilderQueryCommand(string storedProcName, SqlParameter[] parameters)
{
SqlCommand command = new SqlCommand();
command.Connection = Connection;
command.CommandText = storedProcName.Trim();
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (SqlParameter p in parameters)
{
command.Parameters.Add(p);
}
}
return command;
}
///
/// 返回结果的存储过程
///
/// 存储过程名
/// 参数值
///
public SqlDataReader GetDataReader(string storedProcName, SqlParameter[] parameters)
{
SqlDataReader reader;
SqlCommand cmd = BuilderQueryCommand(storedProcName, parameters);
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
///
/// 不返回结果的存储过程
///
/// 存储过程名
/// 参数值
///
public int GetEffect(string storedProcName, SqlParameter[] parameters)
{
int result = 0;
try
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
SqlTransaction trans = Connection.BeginTransaction();
try
{
SqlCommand cmd = BuilderQueryCommand(storedProcName, parameters);
cmd.Transaction = trans;
result = cmd.ExecuteNonQuery();
trans.Commit();
Connection.Close();
return result;
}
catch (Exception ex)
{
if (trans != null)
{
trans.Rollback();
return result;
}
throw ex;
}
finally
{
if (trans != null)
{
trans.Dispose();
}
Connection.Close();
}
}
catch (Exception ex1)
{
return 0;
// throw new Exception(ex1.Message);
}
}
///
/// 返回dateSet
///
///
///
///
///
public DataSet GetDataSet(string ProcName, SqlParameter[] parameters, string tableName)
{
try
{
DataSet ds = new DataSet();
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
SqlDataAdapter myDa = new SqlDataAdapter();
myDa.SelectCommand = BuilderQueryCommand(ProcName, parameters);
myDa.Fill(ds, tableName);
return ds;
}
catch
{
return null;
}
finally
{
Connection.Close();
}
}
///
/// 自定义分页
///
/// 存储过程名
/// 参数名
/// 起始页
/// 记录数
/// 表名
///
public DataSet GetDataSet(string ProcName, SqlParameter[] parameters, int start, int maxRecord, string tableName)
{
try
{
DataSet ds = new DataSet();
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
Connection.Open();
SqlDataAdapter myDa = new SqlDataAdapter();
myDa.SelectCommand = BuilderQueryCommand(ProcName, parameters);
myDa.Fill(ds, start, maxRecord, tableName);
return ds;
}
catch
{
Connection.Close();
return null;
}
finally
{
Connection.Close();
}
}
///
/// 返回object类型 比如首行首列
///
/// 存储过程名称
/// 参数列表
///
public object GetObject(string storedProcName, SqlParameter[] parameters)
{
object result = null;
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
// SqlTransaction trans = Connection.BeginTransaction();
try
{
SqlCommand cmd = BuilderQueryCommand(storedProcName, parameters);
// cmd.Transaction = trans;
result = cmd.ExecuteScalar();
// trans.Commit();
Connection.Close();
return result;
}
catch
{
// trans.Rollback();
return result;
}
finally
{
Connection.Close();
}
}
///
/// 通过ID号操作某条记录,比如删除ID号的记录
///
/// 存储过程名
/// 值
/// 参数名如:@ID
///
public int RunProcByID(string ProcName, int id, string paraname)
{
try
{
SqlParameter[] p ={ new SqlParameter(paraname, SqlDbType.Int) };
p[0].Value = id;
return GetEffect(ProcName, p);
}
catch
{
Connection.Close();
return 0;
}
finally
{
Connection.Close();
}
}
///
/// 修改ID号为keyValue的记录的ParaDieldName字段的值为FiledValue
///
/// 相信过程名
/// ID号的值
/// ID号对应的字段名
/// 要修改的字段名
/// 要修改字段的值
/// 字段的长度
///
public int UpdateByID(string ProcName, int keyValue, string paraKeyName, string paraFieldName, string FieldValue, int length)
{
try
{
SqlParameter[] p ={ new SqlParameter(paraFieldName,SqlDbType.VarChar,length),
new SqlParameter(paraFieldName, SqlDbType.Int)
};
p[0].Value = FieldValue;
p[1].Value = keyValue;
return GetEffect(ProcName, p);
}
catch
{
Connection.Close();
return 0;
}
finally
{
Connection.Close();
}
}
///
/// 执行SQL语句返回DataSet
///
/// SQL语句
/// 表名
///
public DataSet RunSql(string strSql, string tableName)
{
try
{
DataSet ds = new DataSet();
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
SqlDataAdapter myDa = new SqlDataAdapter();
myDa.SelectCommand = new SqlCommand(strSql, Connection);
myDa.Fill(ds, tableName);
return ds;
}
catch
{
return null;
}
finally
{
Connection.Close();
}
}
///
/// 执行SQL语句返回影响行数
///
/// sql语句
/// 输出行数
///
public int RunSql(string strSql, out int effect)
{
effect = 0;
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
SqlTransaction trans = Connection.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand(strSql, Connection);
cmd.Transaction = trans;
effect = cmd.ExecuteNonQuery();
trans.Commit();
return effect;
}
catch
{
trans.Rollback();
Connection.Close();
return effect;
}
finally
{
Connection.Close();
}
}
///
/// 执行SQL语句返回SqlDataReader
///
/// sql语句
///
public SqlDataReader RunSql(string strsql)
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
try
{
SqlCommand cmd = new SqlCommand(strsql, Connection);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
Connection.Close();
return null;
}
}
///
/// 执行SQL语句返回object
///
/// sql语句
///
public object GetObjectByRunSQL(string strsql)
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
object obj = null;
try
{
SqlCommand cmd = new SqlCommand(strsql, Connection);
obj = cmd.ExecuteScalar();
Connection.Close();
return obj;
}
catch
{
Connection.Close();
return null;
}
finally
{
Connection.Close();
}
}
}