数据库访问类的编写

数据库连接类的使用:

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public class DataBase { //使用单件模式,只能实例化一个对象,所有方法都是静态方法; private static DataBase _instance; public DataBase() { } public static DataBase GetInstance() { if(_instance==null) { _instance = new DataBase(); } return _instance; } //得到并返回一个打开的数据库链接。 public static SqlConnection ReturnConn() { string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection Conn = new SqlConnection(strConn); if (Conn.State.Equals(ConnectionState.Closed)) { Conn.Open(); } return Conn; } //①将存储过程转换成SqlCommand;(存储过程名,字段集合,数据库连接) public static SqlCommand CreateCmd(string procName, SqlParameter[] prams, SqlConnection Conn) { SqlConnection SqlConn = Conn; if (SqlConn.State.Equals(ConnectionState.Closed)) { SqlConn.Open(); } SqlCommand Cmd = new SqlCommand(); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Connection = SqlConn; Cmd.CommandText = procName; if (prams != null) { foreach (SqlParameter parameter in prams) { if (parameter != null) { Cmd.Parameters.Add(parameter); } } } return Cmd; } //②将存储过程转换成SqlCommand;(存储过程名,字段集合) public static SqlCommand CreateCmd(string procName, SqlParameter[] prams) { SqlConnection Conn = ReturnConn(); SqlCommand Cmd = new SqlCommand(); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Connection = Conn; Cmd.CommandText = procName; if (prams != null) { foreach (SqlParameter parameter in prams) { if (parameter != null) { Cmd.Parameters.Add(parameter); } } } return Cmd; } //③将存储过程转换成SqlCommand;(存储过程名,数据库连接) public static SqlCommand CreateCmd(string procName,SqlConnection Conn) { SqlConnection SqlConn = Conn; if (SqlConn.State.Equals(ConnectionState.Closed)) { SqlConn.Open(); } SqlCommand Cmd = new SqlCommand(); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Connection = SqlConn; Cmd.CommandText = procName; return Cmd; } //④将存储过程转换成SqlCommand;(存储过程名) public static SqlCommand CreateCmd(string procName) { SqlConnection Conn = ReturnConn(); SqlCommand Cmd = new SqlCommand(); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Connection = Conn; Cmd.CommandText = procName; return Cmd; } //①执行存储过程并返回一个SqlDataReader对象,(存储过程名,字段集合) public static SqlDataReader RunProcGetReader(string procName, SqlParameter[] prams) { SqlCommand Cmd = CreateCmd(procName, prams); SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); return Dr; } //②执行存储过程并返回一个SqlDataReader对象,(存储过程名,字段集合,数据库连接) public static SqlDataReader RunProcGetReader(string procName, SqlParameter[] prams, SqlConnection Conn) { SqlCommand Cmd = CreateCmd(procName, prams,Conn); SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); return Dr; } //③执行存储过程并返回一个SqlDataReader对象,(存储过程名,数据库连接) public static SqlDataReader RunProcGetReader(string procName, SqlConnection Conn) { SqlCommand Cmd = CreateCmd(procName,Conn); SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); return Dr; } //④执行存储过程并返回一个SqlDataReader对象,(存储过程名) public static SqlDataReader RunProcGetReader(string procName) { SqlCommand Cmd = CreateCmd(procName); SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); return Dr; } //执行存储过程并返回一个DataTable对象,(存储过程名,字段集合,数据库连接) public static DataTable RunProcGetTable(string procName, SqlParameter[] prams, SqlConnection Conn) { SqlCommand Cmd = CreateCmd(procName, prams, Conn); SqlDataAdapter Dtr = new SqlDataAdapter(); DataSet Ds = new DataSet(); Dtr.SelectCommand = Cmd; Dtr.Fill(Ds); DataTable Dt = Ds.Tables[0]; Conn.Close(); return Dt; } //①直接执行存储过程,返回整形数据。(存储过程名) public static int RunExecute(string procName) { SqlConnection Conn = ReturnConn(); SqlCommand Cmd = CreateCmd(procName,Conn); int intResult = Cmd.ExecuteNonQuery(); Conn.Close(); return intResult; } //②直接执行存储过程,返回整形数据。(存储过程名,字段集合) public static int RunExecute(string procName,SqlParameter[] prams) { SqlConnection Conn = ReturnConn(); SqlCommand Cmd = CreateCmd(procName,prams,Conn); int intResult = Cmd.ExecuteNonQuery(); Conn.Close(); return intResult; } public static int RunExecuteScalar(string procName) { SqlConnection Conn = ReturnConn(); SqlCommand Cmd = CreateCmd(procName, Conn); int intResult = Convert.ToInt32(Cmd.ExecuteScalar()); Conn.Close(); return intResult; } public static int RunExecuteScalar(string procName, SqlParameter[] prams) { SqlConnection Conn = ReturnConn(); SqlCommand Cmd = CreateCmd(procName, prams, Conn); int intResult =Convert.ToInt32(Cmd.ExecuteScalar()); Conn.Close(); return intResult; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值