2004年12月22日发表于blog.csdn.net/zxub
前段时间,搞.net,有些数据库操作方面的事,要是每个都写那么多。那还不累死,就想写个类,把操作都封装起来,由于当时学习.net才1,2个星期,写得不是太好,现在也不想再改了,个人思维就定了,怎么改也差不多了~~
首先在web.config文件中加这么一段:
<appSettings>
<add key="conn_handler" value="sqlserver.htm" />
<add key="conn_string" value="Persist Security Info=False;Data Source=localhost;Initial Catalog=northwind;User ID=xxxx;Password=xxxxxx;" />
<add key="conn_datebasetype" value="SQLServer" />
<add key="conn_schema" value="" />
<add key="conn_catalog" value="" />
</appSettings>
和<system.web></system.web>是平行的关系,这样做是在类中可以调用它,为什么就不多说了。
下面是这个类的全部代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace DBOperate
{
/// <summary>
/// 创建一个数据库对象,以实现数据操作。
/// </summary>
public class DBObject
{
private string ConnectionString;
private SqlConnection objConn;
private string objSelectString;
private string objInsertString;
private string objUpdateString;
private string objDeleteString;
private SqlDataReader objDR;
private SqlCommand objCmd;
private string objErrMessage=null;
private string tempErrMessage=null;
private SqlDataAdapter objDA;
private DataSet objDS;
private DataView objDW;
private DataGrid objDataGrid;
private string objViewRowFilter=null;
private string objViewSort=null;
public DBObject()
{
//
// TODO: 在此处添加构造函数逻辑
//
DBConn();
}
private void DBConn()
{
ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["conn_string"];
objConn = new SqlConnection(ConnectionString);
objConn.Open();
}
public void DBClose()
{
objConn.Close();
}
public string SelectString
{
set
{
objSelectString=value;
}
}
public string InsertString
{
set
{
objInsertString=value;
DBInsert();
}
}
public string UpdateString
{
set
{
objUpdateString=value;
DBUpdate();
}
}
public string DeleteString
{
set
{
objDeleteString=value;
DBDelete();
}
}
public string ErrMessage
{
get
{
tempErrMessage=objErrMessage;
objErrMessage=null;
return tempErrMessage;
}
}
public SqlDataReader DR
{
get
{
GetDR();
return objDR;
}
}
public void DRClose()
{
objDR.Close();
}
public void GetDR()
{
try
{
using (objCmd=new SqlCommand(objSelectString,objConn))
{
objDR=objCmd.ExecuteReader();
}
}
catch (System.Exception e)
{
objErrMessage+="数据读取出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
}
public void objDRRead()
{
objDR.Read();
}
public bool SelectEmpty()
{
if (objDR.Read()) return false;
else return true;
}
public string GetDRItem(string objDRItem)
{
return objDR[objDRItem].ToString();
}
public DataSet DS
{
get
{
try
{
using (objDA = new SqlDataAdapter(objSelectString,objConn))
{
objDS=new DataSet();
objDA.Fill(objDS);
}
}
catch (System.Exception e)
{
objErrMessage+="数据读取出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
return objDS;
}
}
public DataView DW
{
get
{
try
{
using (objDA = new SqlDataAdapter(objSelectString,objConn))
{
objDS=new DataSet();
objDA.Fill(objDS);
objDW=new DataView(objDS.Tables[0]);
objDW.RowFilter=objViewRowFilter;
objDW.Sort=objViewSort;
}
}
catch (System.Exception e)
{
objErrMessage+="数据读取出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
return objDW;
}
}
public DataGrid DGridBindDS
{
set
{
objDataGrid=value;
BindDS();
}
}
public DataGrid DGridBindDR
{
set
{
objDataGrid=value;
BindDR();
}
}
public string ViewRowFilter
{
set
{
if (objViewRowFilter==null)
{
objViewRowFilter=value;
}
else
{
objViewRowFilter+=" and ";
objViewRowFilter+=value;
}
}
}
public string ViewSort
{
set
{
objViewSort=value;
}
}
private void BindDR()
{
try
{
using (objCmd=new SqlCommand(objSelectString,objConn))
{
objDR=objCmd.ExecuteReader();
objDataGrid.DataSource=objDR;
objDataGrid.DataBind();
objDR.Close();
}
}
catch (System.Exception e)
{
objErrMessage+="数据读取出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
}
private void BindDS()
{
try
{
using (objDA = new SqlDataAdapter(objSelectString,objConn))
{
objDS=new DataSet();
objDA.Fill(objDS);
objDataGrid.DataSource=objDS;
objDataGrid.DataBind();
}
}
catch (System.Exception e)
{
objErrMessage+="数据读取出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
}
private void DBInsert()
{
using (objCmd=new SqlCommand(objInsertString,objConn))
{
try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
objErrMessage+="数据插入出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
}
}
private void DBUpdate()
{
using (objCmd=new SqlCommand(objUpdateString,objConn))
{
try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
objErrMessage+="数据更新出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
}
}
private void DBDelete()
{
using (objCmd=new SqlCommand(objDeleteString,objConn))
{
try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
objErrMessage+="数据删除出错:";
objErrMessage+="<br>";
objErrMessage+=e.Message;
objErrMessage+="<br>";
}
}
}
}
}
使用方法:
先添加引用,再创建一个DBObject对象,假设是obj,创建对象的同时就和数据库连接上了,所以用完了调用一个DBClose()方法,关闭数据库连接。
属性说明:
SelectString,UpdateString,DeleteString,InsertString,看字面意思就知道了,给这四个属性赋值后,相应的操作就完成了,我已经写在属性的Set方法中了。
给SelectString赋值后,就可以返回我们要的东西了。可以返回dataset,dataview,datareader,还可以直接就把控件和数据库绑定,具体可以查看代码。
由于水平有限,写得不好,有好的意见可以给我留言,谢谢!