C#SQLhelp类访问数据库
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MyBookShopSys.DAL
{
///
/// 数据库访问类 测试1.3bate版
///2009-08-18 XX 修改
///2009-09-08 增加事务的处理
///
public static class DBHelper
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["MyBookShopSys"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
///
/// 执行一个增删改存储过程(有参)
///
/// 存储过程名字
/// 参数列表
///
public static int ExecuteProc(string procName, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
mandText = procName;
mandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
///
/// 执行一个查询无参存储过程,要关闭
///
/// 存储过程名字
///
public static SqlDataReader ExecuteProcSelect(string procName)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
mandText = procName;
mandType = CommandType.StoredProcedure;
return cmd.ExecuteReader();
}
///
/// 执行一个带参查询存储过程,注意要关闭
///
/// 存储过程名字
/// 参数列表
///
public static SqlDataReader ExecuteProcSelect(string procName, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
mandText = procName;
mandTy
pe = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
return cmd.ExecuteReader();
}
///
/// 执行一个无参增删改存储过程
///
/// 存储过程名字
///
public static int ExecuteProc(string procName)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
mandText = procName;
mandType = CommandType.StoredProcedure;
return cmd.ExecuteNonQuery();
}
///
/// 执行一个(无参)增删改语句
///
/// 语句
///
public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
///
/// 执行一个有参增删改操作
///
/// 语句
/// 参数
///
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
///
/// 查询第一行第一列数据(无参)(返回的是什么类型就转换成什么类型)
///
/// 语句
///
public static object GetScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
return cmd.ExecuteScalar();
}
///
/// 查询第一行第一列数据(有参)(返回的是什么类型就转换成什么类型)
///
/// 参数
///
public static object GetScalar(string safeSql,params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(safeSql,Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteScalar();
}
///
/// 返回一个SqlDataReader(注意要关闭)
///
/// 语句
///
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
///
/// 返回int
//
/
///
///
///
public static int GetScalarInt(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return Convert.ToInt32(cmd.ExecuteScalar());
}
///
/// 返回string
///
///
///
///
public static string GetScalarString(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return Convert.ToString(cmd.ExecuteScalar());
}
///
/// 返回一个有参SqlDataReader(注意要关闭)
///
/// 语句
/// 参数
///
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
///
/// 返回一个Datatable(无参)
///
/// 语句
///
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
///
/// 返回一个Datatable(有参)
///
/// 语句
/// 参数
///
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
///
/// 执行多条SQL语句,实现数据库事务。
///
/// 多条SQL语句
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
SqlTransaction tx = Connection.BeginTransaction();
cmd.Transaction=tx;
try
{
for(int n=0;n
++)
{
string strsql=SQLStringList[n].ToString();
if (strsql.Trim().Length>1)
{
mandText=strsql;
cmd.ExecuteNonQuery();
}
}
mit();
}
catch(System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
}