类一(DBO.cs):主要用来操作存储过程
类二(DB.CS):主要用来操作SQL语句字符串
由于这些也是我在网上总结的,所以不便藏私,出来混,迟早是要还的嘛,人人为我,我为人人,挥泪共享.
using
System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ProductManager
... {
//主要用来操作存储过程,当然直接操作SQL语句也行
public class DBO
...{
private static string ConStr = "server=localhost;database=ProductManager;uid=sa;pwd=";//或从web.config中读取,此句可变
private SqlConnection _conn;
private SqlCommand _comm;
private void CreateCon()//创建数据库连接。
...{
_conn = new SqlConnection(ConStr);
}
...#region
public static SqlParameter CreatePara(string _paraName, SqlDbType _dbType, int _size, ParameterDirection _paraDire, object _paraValue)
...{
SqlParameter Para = new SqlParameter(_paraName, _dbType, _size);
switch (_paraDire)
...{
case ParameterDirection.Input:
Para.Direction = ParameterDirection.Input;
Para.Value = _paraValue;
break;
case ParameterDirection.Output:
Para.Direction = ParameterDirection.Output;
break;
default:
Para.Value = _paraValue;
break;
}
return Para;
}
/**//// <summary>
/// 创建一个通用的命令对象
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns></returns>
private void CreateCmd(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
CreateCon();
_comm = new SqlCommand(_sqlStr, _conn);
switch (_cmdType)
...{
case CommandType.Text:
_comm.CommandType = CommandType.Text; break;
case CommandType.StoredProcedure:
_comm.CommandType = CommandType.StoredProcedure; break;
default:
_comm.CommandType = CommandType.Text; break;
}
if (_paras != null)
...{
for (int i = 0; i < _paras.Length; i++)
...{
_comm.Parameters.Add(_paras[i]);
}
}
}
/**//// <summary>
/// 执行增删改
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns></returns>
public int ExecAddDelUpdate(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
SqlParameter rv = new SqlParameter("ReturnValue", DbType.Int32);
rv.Direction = ParameterDirection.ReturnValue;
CreateCmd(_sqlStr, _paras, _cmdType);
_comm.Parameters.Add(rv);
_comm.Connection.Open();
_comm.ExecuteNonQuery();
_comm.Connection.Close();
_comm.Dispose();
return (int)rv.Value;
}
/**//// <summary>
/// 执行查询
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns>返回SqlDataReader</returns>
public SqlDataReader ExecReader(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
CreateCmd(_sqlStr, _paras, _cmdType);
SqlDataReader Dr = _comm.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
/**//// <summary>
/// 执行查询
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns>返回DataSet</returns>
public DataSet ExecDataSet(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
CreateCmd(_sqlStr, _paras, _cmdType);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
sda.Fill(ds);
return ds;
}
//这个函数里面写一些释放资源的代码
public static void Dispose()
...{
//.................
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ProductManager
... {
//主要用来操作存储过程,当然直接操作SQL语句也行
public class DBO
...{
private static string ConStr = "server=localhost;database=ProductManager;uid=sa;pwd=";//或从web.config中读取,此句可变
private SqlConnection _conn;
private SqlCommand _comm;
private void CreateCon()//创建数据库连接。
...{
_conn = new SqlConnection(ConStr);
}
...#region
public static SqlParameter CreatePara(string _paraName, SqlDbType _dbType, int _size, ParameterDirection _paraDire, object _paraValue)
...{
SqlParameter Para = new SqlParameter(_paraName, _dbType, _size);
switch (_paraDire)
...{
case ParameterDirection.Input:
Para.Direction = ParameterDirection.Input;
Para.Value = _paraValue;
break;
case ParameterDirection.Output:
Para.Direction = ParameterDirection.Output;
break;
default:
Para.Value = _paraValue;
break;
}
return Para;
}
/**//// <summary>
/// 创建一个通用的命令对象
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns></returns>
private void CreateCmd(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
CreateCon();
_comm = new SqlCommand(_sqlStr, _conn);
switch (_cmdType)
...{
case CommandType.Text:
_comm.CommandType = CommandType.Text; break;
case CommandType.StoredProcedure:
_comm.CommandType = CommandType.StoredProcedure; break;
default:
_comm.CommandType = CommandType.Text; break;
}
if (_paras != null)
...{
for (int i = 0; i < _paras.Length; i++)
...{
_comm.Parameters.Add(_paras[i]);
}
}
}
/**//// <summary>
/// 执行增删改
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns></returns>
public int ExecAddDelUpdate(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
SqlParameter rv = new SqlParameter("ReturnValue", DbType.Int32);
rv.Direction = ParameterDirection.ReturnValue;
CreateCmd(_sqlStr, _paras, _cmdType);
_comm.Parameters.Add(rv);
_comm.Connection.Open();
_comm.ExecuteNonQuery();
_comm.Connection.Close();
_comm.Dispose();
return (int)rv.Value;
}
/**//// <summary>
/// 执行查询
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns>返回SqlDataReader</returns>
public SqlDataReader ExecReader(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
CreateCmd(_sqlStr, _paras, _cmdType);
SqlDataReader Dr = _comm.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
/**//// <summary>
/// 执行查询
/// </summary>
/// <param name="_sqlStr"></param>
/// <param name="_paras"></param>
/// <param name="_cmdType"></param>
/// <returns>返回DataSet</returns>
public DataSet ExecDataSet(string _sqlStr, SqlParameter[] _paras, CommandType _cmdType)
...{
CreateCmd(_sqlStr, _paras, _cmdType);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(Cmd);
sda.Fill(ds);
return ds;
}
//这个函数里面写一些释放资源的代码
public static void Dispose()
...{
//.................
}
#endregion
}
}
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;
namespace XieTong_lhf
... {
/**//*********************************************************************
**作 用:把一些有关数据库的基本操作,写在这里.一般定义成静态函数
**********************************************************************/
/**//// <summary>
/// 数据库通用操作类
/// </summary>
public class DB
...{
protected static SqlConnection conn = new SqlConnection();
protected static SqlCommand comm = new SqlCommand();
public DB()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// 返回数据库的连接语句,string类型
/// </summary>
public static string ConnectionString()
...{
return ConfigurationManager.AppSettings["ConnectString"].ToString();
}
/**//// <summary>
/// 打开数据库
/// </summary>
private static void openConnection()
...{
if (conn.State == ConnectionState.Closed)
...{
conn.ConnectionString = ConfigurationManager.AppSettings["ConnectString"].ToString();
comm.Connection = conn;
try
...{
conn.Open();
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
}
}
/**//// <summary>
/// 关闭数据库
/// </summary>
private static void closeConnection()
...{
if (conn.State == ConnectionState.Open)
...{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}
/**//// <summary>
/// 执行sql语句,用于增加,删除,修改这种无返回的操作
/// </summary>
public static void excuteSql(string sqlstr)
...{
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{ closeConnection(); }
}
/**//// <summary>
/// 返回指定sql语句的SqlDataReader对象,使用时请注意关闭这个对象。用于查询语句
/// </summary>
public static SqlDataReader dataReader(string sqlstr)
...{
SqlDataReader dr = null;
try
...{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
...{
try
...{
dr.Close();
closeConnection();
}
catch ...{ }
}
return dr;
}
/**//// <summary>
/// 返回指定sql语句的SqlDataReader对象,使用时请注意关闭。用于查询语句
/// </summary>
public static void dataReader(string sqlstr, ref SqlDataReader dr)
...{
try
...{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
...{
try
...{
if (dr != null && !dr.IsClosed)
dr.Close();
}
catch
...{
}
finally
...{
closeConnection();
}
}
}
/**//// <summary>
/// 返回指定sql语句的dataset
/// </summary>
public static DataSet dataSet(string sqlstr)
...{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
return ds;
}
/**//// <summary>
/// 返回指定sql语句的dataset
/// </summary>
public static void dataSet(string sqlstr, ref DataSet ds)
...{
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
}
/**//// <summary>
/// 返回指定sql语句的datatable
/// </summary>
public static DataTable dataTable(string sqlstr)
...{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
return dt;
}
/**//// <summary>
/// 返回指定sql语句的datatable
/// </summary>
public static void dataTable(string sqlstr, ref DataTable dt)
...{
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
}
/**//// <summary>
/// 返回指定sql语句的dataview
/// </summary>
public static DataView dataView(string sqlstr)
...{
SqlDataAdapter da = new SqlDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
return dv;
}
}
}
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;
namespace XieTong_lhf
... {
/**//*********************************************************************
**作 用:把一些有关数据库的基本操作,写在这里.一般定义成静态函数
**********************************************************************/
/**//// <summary>
/// 数据库通用操作类
/// </summary>
public class DB
...{
protected static SqlConnection conn = new SqlConnection();
protected static SqlCommand comm = new SqlCommand();
public DB()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// 返回数据库的连接语句,string类型
/// </summary>
public static string ConnectionString()
...{
return ConfigurationManager.AppSettings["ConnectString"].ToString();
}
/**//// <summary>
/// 打开数据库
/// </summary>
private static void openConnection()
...{
if (conn.State == ConnectionState.Closed)
...{
conn.ConnectionString = ConfigurationManager.AppSettings["ConnectString"].ToString();
comm.Connection = conn;
try
...{
conn.Open();
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
}
}
/**//// <summary>
/// 关闭数据库
/// </summary>
private static void closeConnection()
...{
if (conn.State == ConnectionState.Open)
...{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}
/**//// <summary>
/// 执行sql语句,用于增加,删除,修改这种无返回的操作
/// </summary>
public static void excuteSql(string sqlstr)
...{
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{ closeConnection(); }
}
/**//// <summary>
/// 返回指定sql语句的SqlDataReader对象,使用时请注意关闭这个对象。用于查询语句
/// </summary>
public static SqlDataReader dataReader(string sqlstr)
...{
SqlDataReader dr = null;
try
...{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
...{
try
...{
dr.Close();
closeConnection();
}
catch ...{ }
}
return dr;
}
/**//// <summary>
/// 返回指定sql语句的SqlDataReader对象,使用时请注意关闭。用于查询语句
/// </summary>
public static void dataReader(string sqlstr, ref SqlDataReader dr)
...{
try
...{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
...{
try
...{
if (dr != null && !dr.IsClosed)
dr.Close();
}
catch
...{
}
finally
...{
closeConnection();
}
}
}
/**//// <summary>
/// 返回指定sql语句的dataset
/// </summary>
public static DataSet dataSet(string sqlstr)
...{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
return ds;
}
/**//// <summary>
/// 返回指定sql语句的dataset
/// </summary>
public static void dataSet(string sqlstr, ref DataSet ds)
...{
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
}
/**//// <summary>
/// 返回指定sql语句的datatable
/// </summary>
public static DataTable dataTable(string sqlstr)
...{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
return dt;
}
/**//// <summary>
/// 返回指定sql语句的datatable
/// </summary>
public static void dataTable(string sqlstr, ref DataTable dt)
...{
SqlDataAdapter da = new SqlDataAdapter();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
}
/**//// <summary>
/// 返回指定sql语句的dataview
/// </summary>
public static DataView dataView(string sqlstr)
...{
SqlDataAdapter da = new SqlDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
...{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception e)
...{
throw new Exception(e.Message);
}
finally
...{
closeConnection();
}
return dv;
}
}
}