一个通用的数据库类
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;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
private SqlConnection conn;
private SqlCommand sc;
private SqlDataAdapter sa;
//返回当前的连接,事务时使用
public SqlConnection Conn
{
get { return conn; }
}
public DataBase()
{
//当对象一但被创建则建立连接
this.conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("conStr"));
this.conn.Open();
}
//关闭数据库连接
public void dis_connect()
{
if (conn != null)
conn.Close();
}
//查询数据库
public DataSet query(string cmd)
{
sc = new SqlCommand(cmd, conn); //用当前对象的连接建立数据命令
sa = new SqlDataAdapter(sc);
DataSet myds = new DataSet();
myds = new DataSet();
sa.Fill(myds); //执行查询命令,并将查询结果保存在DataSet中.
return myds;
}
//新增记录
public int update(string cmd)
{
sc = new SqlCommand(cmd, conn);
int re;
try
{
re = sc.ExecuteNonQuery(); //执行其它命令(insert,update,delete)
}
catch (Exception e)
{
Console.Write(e.Message);
re = -5; //数据库操作有异常
}
return re;
}
/*调用查询类的存储过程
* prceName: 存储过程名
* parames[]:参数数组
*/
public DataSet call_que_proce(string prceName,params SqlParameter[] parames)
{
sa = new SqlDataAdapter();
sc = new SqlCommand();
DataSet ds = new DataSet();
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
sa.Fill(ds);
sc.Parameters.Clear();
sa.Dispose();
return ds;
}
/*调用非查询类的存储过程
* prceName
*/
public int call_noqeury_proce(string prceName, params SqlParameter[] parames)
{
sc = new SqlCommand();
int result = 0;
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
result = sc.ExecuteNonQuery();
sc.Parameters.Clear();
return result;
}
}
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;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
private SqlConnection conn;
private SqlCommand sc;
private SqlDataAdapter sa;
//返回当前的连接,事务时使用
public SqlConnection Conn
{
get { return conn; }
}
public DataBase()
{
//当对象一但被创建则建立连接
this.conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("conStr"));
this.conn.Open();
}
//关闭数据库连接
public void dis_connect()
{
if (conn != null)
conn.Close();
}
//查询数据库
public DataSet query(string cmd)
{
sc = new SqlCommand(cmd, conn); //用当前对象的连接建立数据命令
sa = new SqlDataAdapter(sc);
DataSet myds = new DataSet();
myds = new DataSet();
sa.Fill(myds); //执行查询命令,并将查询结果保存在DataSet中.
return myds;
}
//新增记录
public int update(string cmd)
{
sc = new SqlCommand(cmd, conn);
int re;
try
{
re = sc.ExecuteNonQuery(); //执行其它命令(insert,update,delete)
}
catch (Exception e)
{
Console.Write(e.Message);
re = -5; //数据库操作有异常
}
return re;
}
/*调用查询类的存储过程
* prceName: 存储过程名
* parames[]:参数数组
*/
public DataSet call_que_proce(string prceName,params SqlParameter[] parames)
{
sa = new SqlDataAdapter();
sc = new SqlCommand();
DataSet ds = new DataSet();
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
sa.Fill(ds);
sc.Parameters.Clear();
sa.Dispose();
return ds;
}
/*调用非查询类的存储过程
* prceName
*/
public int call_noqeury_proce(string prceName, params SqlParameter[] parames)
{
sc = new SqlCommand();
int result = 0;
sc.Connection = conn;
sc.CommandText = prceName;
sc.CommandType = CommandType.StoredProcedure;
sa.SelectCommand = sc;
foreach (SqlParameter sp in parames)
{
sa.SelectCommand.Parameters.Add(sp);
}
result = sc.ExecuteNonQuery();
sc.Parameters.Clear();
return result;
}
}