[转]一个数据库操作类(C#)
前段时间,搞.net,有些数据库操作方面的事,要是每个都写那么多。那还不累死,就想写个类,把操作都封装起来,由于当时学习.net才1,2个星期,写得不是太好,现在也不想再改了,个人思维就定了,怎么改也差不多了~~
首先在web.config文件中加这么一段:
 <appSettings>
<appSettings>
 <add key="conn_handler" value="sqlserver.htm" />
    <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_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_datebasetype" value="SQLServer" />
 <add key="conn_schema" value="" />
    <add key="conn_schema" value="" />
 <add key="conn_catalog" value="" />
    <add key="conn_catalog" value="" />
 </appSettings>
    </appSettings>
 和<system.web></system.web>是平行的关系,这样做是在类中可以调用它,为什么就不多说了。
  和<system.web></system.web>是平行的关系,这样做是在类中可以调用它,为什么就不多说了。
下面是这个类的全部代码:
 using System;
using System;
 using System.Data;
using System.Data;
 using System.Data.SqlClient;
using System.Data.SqlClient;
 using System.Web.UI.WebControls;
using System.Web.UI.WebControls;

 namespace DBOperate
namespace DBOperate
 {
{
 /// <summary>
 /// <summary>
 /// 创建一个数据库对象,以实现数据操作。
 /// 创建一个数据库对象,以实现数据操作。
 /// </summary>
 /// </summary>
 public class DBObject
 public class DBObject
 {
 {
 private string ConnectionString;
  private string ConnectionString;
 private SqlConnection objConn;
  private SqlConnection objConn;
 private string objSelectString;
  private string objSelectString;
 private string objInsertString;
  private string objInsertString;
 private string objUpdateString;
  private string objUpdateString;
 private string objDeleteString;
  private string objDeleteString;
 private SqlDataReader objDR;
  private SqlDataReader objDR;
 private SqlCommand objCmd;
  private SqlCommand objCmd;
 private string objErrMessage=null;
  private string objErrMessage=null;
 private string tempErrMessage=null;
  private string tempErrMessage=null;
 private SqlDataAdapter objDA;
  private SqlDataAdapter objDA;
 private DataSet objDS;
  private DataSet objDS;
 private DataView objDW;
  private DataView objDW;
 private DataGrid objDataGrid;
  private DataGrid objDataGrid;
 private string objViewRowFilter=null;
  private string objViewRowFilter=null;
 private string objViewSort=null;
  private string objViewSort=null;
 public DBObject()
  public DBObject()
 {
  {
 //
   //
 // TODO: 在此处添加构造函数逻辑
   // TODO: 在此处添加构造函数逻辑
 //
   //
 DBConn();
   DBConn();
 }
  }  
 private void DBConn()
  private void DBConn()
 {
  {
 ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["conn_string"];
   ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["conn_string"];
 objConn = new SqlConnection(ConnectionString);
   objConn = new SqlConnection(ConnectionString);
 objConn.Open();
   objConn.Open();
 }
  }
 public void DBClose()
  public void DBClose()
 {
  {
 objConn.Close();
   objConn.Close();
 }
  }

 public string SelectString
  public string SelectString
 {
  {
 set
   set
 {
   {
 objSelectString=value;
    objSelectString=value;    
 }
   }
 }
  }
 public string InsertString
  public string InsertString
 {
  {
 set
   set
 {
   {
 objInsertString=value;
    objInsertString=value;
 DBInsert();
    DBInsert();
 }
   }
 }
  }
 public string UpdateString
  public string UpdateString
 {
  {
 set
   set
 {
   {
 objUpdateString=value;
    objUpdateString=value;
 DBUpdate();
    DBUpdate();
 }
   }
 }
  }
 public string DeleteString
  public string DeleteString
 {
  {
 set
   set
 {
   {
 objDeleteString=value;
    objDeleteString=value;
 DBDelete();
    DBDelete();
 }
   }
 }
  }
 public string ErrMessage
  public string ErrMessage
 {
  {
 get
   get
 {
   {
 tempErrMessage=objErrMessage;
    tempErrMessage=objErrMessage;
 objErrMessage=null;
    objErrMessage=null;
 return tempErrMessage;
    return tempErrMessage;
 }
   }
 }
  }
 public SqlDataReader DR
  public SqlDataReader DR
 {
  {
 get
   get
 {
   {
 GetDR();
    GetDR();
 return objDR;
    return objDR;    
 }
   }
 }
  }
 public void DRClose()
  public void DRClose()
 {
  {
 objDR.Close();
   objDR.Close();
 }
  }
 public void GetDR()
  public void GetDR()
 {
  {
 try
   try
 {
   {
 using (objCmd=new SqlCommand(objSelectString,objConn))
    using (objCmd=new SqlCommand(objSelectString,objConn))
 {
    {
 objDR=objCmd.ExecuteReader();
     objDR=objCmd.ExecuteReader();
 }
    }
 }
   }
 catch (System.Exception e)
   catch (System.Exception e)
 {
   {
 objErrMessage+="数据读取出错:";
    objErrMessage+="数据读取出错:";
 objErrMessage+="<br>";
    objErrMessage+="<br>";
 objErrMessage+=e.Message;
    objErrMessage+=e.Message;
 objErrMessage+="<br>";
    objErrMessage+="<br>";
 }
   }
 }
  }
 public void objDRRead()
  public void objDRRead()
 {
  {
 objDR.Read();
   objDR.Read();
 }
  }
 public bool SelectEmpty()
  public bool SelectEmpty()
 {
  {
 if (objDR.Read()) return false;
   if (objDR.Read()) return false;
 else return true;
   else return true;
 }
  }
 public string GetDRItem(string objDRItem)
  public string GetDRItem(string objDRItem)
 {
  {
 return objDR[objDRItem].ToString();
   return objDR[objDRItem].ToString();   
 }
  }
 public DataSet DS
  public DataSet DS
 {
  {
 get
   get
 {
   {
 try
    try
 {
    {
 using (objDA = new SqlDataAdapter(objSelectString,objConn))
     using (objDA = new SqlDataAdapter(objSelectString,objConn))
 {
     {
 objDS=new DataSet();
      objDS=new DataSet();
 objDA.Fill(objDS);
      objDA.Fill(objDS);
 }
     }  
 }
    }
 catch (System.Exception e)
    catch (System.Exception e)
 {
    {
 objErrMessage+="数据读取出错:";
     objErrMessage+="数据读取出错:";
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 objErrMessage+=e.Message;
     objErrMessage+=e.Message;
 objErrMessage+="<br>";
     objErrMessage+="<br>";     
 }
    }
 return objDS;
    return objDS;  
 }
   }
 }
  }
 public DataView DW
  public DataView DW
 {
  {
 get
   get
 {
   {
 try
    try
 {
    {
 using (objDA = new SqlDataAdapter(objSelectString,objConn))
     using (objDA = new SqlDataAdapter(objSelectString,objConn))
 {
     {
 objDS=new DataSet();
      objDS=new DataSet();
 objDA.Fill(objDS);
      objDA.Fill(objDS);
 objDW=new DataView(objDS.Tables[0]);
      objDW=new DataView(objDS.Tables[0]);
 objDW.RowFilter=objViewRowFilter;
      objDW.RowFilter=objViewRowFilter;
 objDW.Sort=objViewSort;
      objDW.Sort=objViewSort;
 }
     }
 }
    }
 catch (System.Exception e)
    catch (System.Exception e)
 {
    {
 objErrMessage+="数据读取出错:";
     objErrMessage+="数据读取出错:";
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 objErrMessage+=e.Message;
     objErrMessage+=e.Message;
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 }
    }
 return objDW;
    return objDW;
 }
   }
 }
  }
 public DataGrid DGridBindDS
  public DataGrid DGridBindDS
 {
  {
 set
   set
 {
   {
 objDataGrid=value;
    objDataGrid=value;
 BindDS();
    BindDS();    
 }
   }
 }
  }
 public DataGrid DGridBindDR
  public DataGrid DGridBindDR
 {
  {
 set
   set
 {
   {
 objDataGrid=value;
    objDataGrid=value;
 BindDR();
    BindDR();
 }
   }
 }
  }
 public string ViewRowFilter
  public string ViewRowFilter
 {
  {
 set
   set
 {
   {
 if (objViewRowFilter==null)
    if (objViewRowFilter==null)
 {
    {
 objViewRowFilter=value;
     objViewRowFilter=value;
 }
    }
 else
    else
 {
    {
 objViewRowFilter+=" and ";
     objViewRowFilter+=" and ";
 objViewRowFilter+=value;
     objViewRowFilter+=value;
 }
    }
 }
   }
 }
  }
 public string ViewSort
  public string ViewSort
 {
  {
 set
   set
 {
   {
 objViewSort=value;
    objViewSort=value;
 }
   }
 }
  }
 private void BindDR()
  private void BindDR()
 {
  {   
 try
   try
 {
   {
 using (objCmd=new SqlCommand(objSelectString,objConn))
    using (objCmd=new SqlCommand(objSelectString,objConn))
 {
    {
 objDR=objCmd.ExecuteReader();
     objDR=objCmd.ExecuteReader();
 objDataGrid.DataSource=objDR;
     objDataGrid.DataSource=objDR;
 objDataGrid.DataBind();
     objDataGrid.DataBind();
 objDR.Close();
     objDR.Close();
 }
    }
 }
   }
 catch (System.Exception e)
   catch (System.Exception e)
 {
   {
 objErrMessage+="数据读取出错:";
    objErrMessage+="数据读取出错:";
 objErrMessage+="<br>";
    objErrMessage+="<br>";
 objErrMessage+=e.Message;
    objErrMessage+=e.Message;
 objErrMessage+="<br>";
    objErrMessage+="<br>";
 }
   }  
 }
  }
 private void BindDS()
  private void BindDS()
 {
  {
 try
   try
 {
   {
 using (objDA = new SqlDataAdapter(objSelectString,objConn))
    using (objDA = new SqlDataAdapter(objSelectString,objConn))
 {
    {
 objDS=new DataSet();
     objDS=new DataSet();
 objDA.Fill(objDS);
     objDA.Fill(objDS);
 objDataGrid.DataSource=objDS;
     objDataGrid.DataSource=objDS;
 objDataGrid.DataBind();
     objDataGrid.DataBind();
 }
    }
 }
   }
 catch (System.Exception e)
   catch (System.Exception e)
 {
   {
 objErrMessage+="数据读取出错:";
    objErrMessage+="数据读取出错:";
 objErrMessage+="<br>";
    objErrMessage+="<br>";
 objErrMessage+=e.Message;
    objErrMessage+=e.Message;
 objErrMessage+="<br>";
    objErrMessage+="<br>";
 }
   }  
 }
  }  
 
  
 private void DBInsert()
  private void DBInsert()
 {
  {   
 using (objCmd=new SqlCommand(objInsertString,objConn))
   using (objCmd=new SqlCommand(objInsertString,objConn))
 {
   {
 try
    try
 {
    {
 objCmd.ExecuteNonQuery();
     objCmd.ExecuteNonQuery();
 }
    }
 catch (System.Exception e)
    catch (System.Exception e)
 {
    {
 objErrMessage+="数据插入出错:";
     objErrMessage+="数据插入出错:";
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 objErrMessage+=e.Message;
     objErrMessage+=e.Message;
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 }
    }
 }
   }
 }
  }
 private void DBUpdate()
  private void DBUpdate()
 {
  {
 using (objCmd=new SqlCommand(objUpdateString,objConn))
   using (objCmd=new SqlCommand(objUpdateString,objConn))
 {
   {
 try
    try
 {
    {
 objCmd.ExecuteNonQuery();
     objCmd.ExecuteNonQuery();
 }
    }
 catch (System.Exception e)
    catch (System.Exception e)
 {
    {
 objErrMessage+="数据更新出错:";
     objErrMessage+="数据更新出错:";
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 objErrMessage+=e.Message;
     objErrMessage+=e.Message;
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 }
    }
 }
   }
 }
  }
 private void DBDelete()
  private void DBDelete()
 {
  {
 using (objCmd=new SqlCommand(objDeleteString,objConn))
   using (objCmd=new SqlCommand(objDeleteString,objConn))
 {
   {
 try
    try
 {
    {
 objCmd.ExecuteNonQuery();
     objCmd.ExecuteNonQuery();
 }
    }
 catch (System.Exception e)
    catch (System.Exception e)
 {
    {
 objErrMessage+="数据删除出错:";
     objErrMessage+="数据删除出错:";
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 objErrMessage+=e.Message;
     objErrMessage+=e.Message;
 objErrMessage+="<br>";
     objErrMessage+="<br>";
 }
    }
 }
   }
 }
  }
 }
 }
 }
}

 
使用方法:
先添加引用,再创建一个DBObject对象,假设是obj,创建对象的同时就和数据库连接上了,所以用完了调用一个DBClose()方法,关闭数据库连接。
属性说明:
SelectString,UpdateString,DeleteString,InsertString,看字面意思就知道了,给这四个属性赋值后,相应的操作就完成了,我已经写在属性的Set方法中了。
给SelectString赋值后,就可以返回我们要的东西了。可以返回dataset,dataview,datareader,还可以直接就把控件和数据库绑定,具体可以查看代码。
由于水平有限,写得不好,有好的意见可以给我留言,谢谢!
首先在web.config文件中加这么一段:
 <appSettings>
<appSettings> <add key="conn_handler" value="sqlserver.htm" />
    <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_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_datebasetype" value="SQLServer" /> <add key="conn_schema" value="" />
    <add key="conn_schema" value="" /> <add key="conn_catalog" value="" />
    <add key="conn_catalog" value="" /> </appSettings>
    </appSettings>
下面是这个类的全部代码:
 using System;
using System; using System.Data;
using System.Data; using System.Data.SqlClient;
using System.Data.SqlClient; using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
 namespace DBOperate
namespace DBOperate {
{ /// <summary>
 /// <summary> /// 创建一个数据库对象,以实现数据操作。
 /// 创建一个数据库对象,以实现数据操作。 /// </summary>
 /// </summary> public class DBObject
 public class DBObject {
 { private string ConnectionString;
  private string ConnectionString; private SqlConnection objConn;
  private SqlConnection objConn; private string objSelectString;
  private string objSelectString; private string objInsertString;
  private string objInsertString; private string objUpdateString;
  private string objUpdateString; private string objDeleteString;
  private string objDeleteString; private SqlDataReader objDR;
  private SqlDataReader objDR; private SqlCommand objCmd;
  private SqlCommand objCmd; private string objErrMessage=null;
  private string objErrMessage=null; private string tempErrMessage=null;
  private string tempErrMessage=null; private SqlDataAdapter objDA;
  private SqlDataAdapter objDA; private DataSet objDS;
  private DataSet objDS; private DataView objDW;
  private DataView objDW; private DataGrid objDataGrid;
  private DataGrid objDataGrid; private string objViewRowFilter=null;
  private string objViewRowFilter=null; private string objViewSort=null;
  private string objViewSort=null; public DBObject()
  public DBObject() {
  { //
   // // TODO: 在此处添加构造函数逻辑
   // TODO: 在此处添加构造函数逻辑 //
   // DBConn();
   DBConn(); }
  }   private void DBConn()
  private void DBConn() {
  { ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["conn_string"];
   ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["conn_string"]; objConn = new SqlConnection(ConnectionString);
   objConn = new SqlConnection(ConnectionString); objConn.Open();
   objConn.Open(); }
  } public void DBClose()
  public void DBClose() {
  { objConn.Close();
   objConn.Close(); }
  }
 public string SelectString
  public string SelectString {
  { set
   set {
   { objSelectString=value;
    objSelectString=value;     }
   } }
  } public string InsertString
  public string InsertString {
  { set
   set {
   { objInsertString=value;
    objInsertString=value; DBInsert();
    DBInsert(); }
   } }
  } public string UpdateString
  public string UpdateString {
  { set
   set {
   { objUpdateString=value;
    objUpdateString=value; DBUpdate();
    DBUpdate(); }
   } }
  } public string DeleteString
  public string DeleteString {
  { set
   set {
   { objDeleteString=value;
    objDeleteString=value; DBDelete();
    DBDelete(); }
   } }
  } public string ErrMessage
  public string ErrMessage {
  { get
   get {
   { tempErrMessage=objErrMessage;
    tempErrMessage=objErrMessage; objErrMessage=null;
    objErrMessage=null; return tempErrMessage;
    return tempErrMessage; }
   } }
  } public SqlDataReader DR
  public SqlDataReader DR {
  { get
   get {
   { GetDR();
    GetDR(); return objDR;
    return objDR;     }
   } }
  } public void DRClose()
  public void DRClose() {
  { objDR.Close();
   objDR.Close(); }
  } public void GetDR()
  public void GetDR() {
  { try
   try {
   { using (objCmd=new SqlCommand(objSelectString,objConn))
    using (objCmd=new SqlCommand(objSelectString,objConn)) {
    { objDR=objCmd.ExecuteReader();
     objDR=objCmd.ExecuteReader(); }
    } }
   } catch (System.Exception e)
   catch (System.Exception e) {
   { objErrMessage+="数据读取出错:";
    objErrMessage+="数据读取出错:"; objErrMessage+="<br>";
    objErrMessage+="<br>"; objErrMessage+=e.Message;
    objErrMessage+=e.Message; objErrMessage+="<br>";
    objErrMessage+="<br>"; }
   } }
  } public void objDRRead()
  public void objDRRead() {
  { objDR.Read();
   objDR.Read(); }
  } public bool SelectEmpty()
  public bool SelectEmpty() {
  { if (objDR.Read()) return false;
   if (objDR.Read()) return false; else return true;
   else return true; }
  } public string GetDRItem(string objDRItem)
  public string GetDRItem(string objDRItem) {
  { return objDR[objDRItem].ToString();
   return objDR[objDRItem].ToString();    }
  } public DataSet DS
  public DataSet DS {
  { get
   get {
   { try
    try {
    { using (objDA = new SqlDataAdapter(objSelectString,objConn))
     using (objDA = new SqlDataAdapter(objSelectString,objConn)) {
     { objDS=new DataSet();
      objDS=new DataSet(); objDA.Fill(objDS);
      objDA.Fill(objDS); }
     }   }
    } catch (System.Exception e)
    catch (System.Exception e) {
    { objErrMessage+="数据读取出错:";
     objErrMessage+="数据读取出错:"; objErrMessage+="<br>";
     objErrMessage+="<br>"; objErrMessage+=e.Message;
     objErrMessage+=e.Message; objErrMessage+="<br>";
     objErrMessage+="<br>";      }
    } return objDS;
    return objDS;   }
   } }
  } public DataView DW
  public DataView DW {
  { get
   get {
   { try
    try {
    { using (objDA = new SqlDataAdapter(objSelectString,objConn))
     using (objDA = new SqlDataAdapter(objSelectString,objConn)) {
     { objDS=new DataSet();
      objDS=new DataSet(); objDA.Fill(objDS);
      objDA.Fill(objDS); objDW=new DataView(objDS.Tables[0]);
      objDW=new DataView(objDS.Tables[0]); objDW.RowFilter=objViewRowFilter;
      objDW.RowFilter=objViewRowFilter; objDW.Sort=objViewSort;
      objDW.Sort=objViewSort; }
     } }
    } catch (System.Exception e)
    catch (System.Exception e) {
    { objErrMessage+="数据读取出错:";
     objErrMessage+="数据读取出错:"; objErrMessage+="<br>";
     objErrMessage+="<br>"; objErrMessage+=e.Message;
     objErrMessage+=e.Message; objErrMessage+="<br>";
     objErrMessage+="<br>"; }
    } return objDW;
    return objDW; }
   } }
  } public DataGrid DGridBindDS
  public DataGrid DGridBindDS {
  { set
   set {
   { objDataGrid=value;
    objDataGrid=value; BindDS();
    BindDS();     }
   } }
  } public DataGrid DGridBindDR
  public DataGrid DGridBindDR {
  { set
   set {
   { objDataGrid=value;
    objDataGrid=value; BindDR();
    BindDR(); }
   } }
  } public string ViewRowFilter
  public string ViewRowFilter {
  { set
   set {
   { if (objViewRowFilter==null)
    if (objViewRowFilter==null) {
    { objViewRowFilter=value;
     objViewRowFilter=value; }
    } else
    else {
    { objViewRowFilter+=" and ";
     objViewRowFilter+=" and "; objViewRowFilter+=value;
     objViewRowFilter+=value; }
    } }
   } }
  } public string ViewSort
  public string ViewSort {
  { set
   set {
   { objViewSort=value;
    objViewSort=value; }
   } }
  } private void BindDR()
  private void BindDR() {
  {    try
   try {
   { using (objCmd=new SqlCommand(objSelectString,objConn))
    using (objCmd=new SqlCommand(objSelectString,objConn)) {
    { objDR=objCmd.ExecuteReader();
     objDR=objCmd.ExecuteReader(); objDataGrid.DataSource=objDR;
     objDataGrid.DataSource=objDR; objDataGrid.DataBind();
     objDataGrid.DataBind(); objDR.Close();
     objDR.Close(); }
    } }
   } catch (System.Exception e)
   catch (System.Exception e) {
   { objErrMessage+="数据读取出错:";
    objErrMessage+="数据读取出错:"; objErrMessage+="<br>";
    objErrMessage+="<br>"; objErrMessage+=e.Message;
    objErrMessage+=e.Message; objErrMessage+="<br>";
    objErrMessage+="<br>"; }
   }   }
  } private void BindDS()
  private void BindDS() {
  { try
   try {
   { using (objDA = new SqlDataAdapter(objSelectString,objConn))
    using (objDA = new SqlDataAdapter(objSelectString,objConn)) {
    { objDS=new DataSet();
     objDS=new DataSet(); objDA.Fill(objDS);
     objDA.Fill(objDS); objDataGrid.DataSource=objDS;
     objDataGrid.DataSource=objDS; objDataGrid.DataBind();
     objDataGrid.DataBind(); }
    } }
   } catch (System.Exception e)
   catch (System.Exception e) {
   { objErrMessage+="数据读取出错:";
    objErrMessage+="数据读取出错:"; objErrMessage+="<br>";
    objErrMessage+="<br>"; objErrMessage+=e.Message;
    objErrMessage+=e.Message; objErrMessage+="<br>";
    objErrMessage+="<br>"; }
   }   }
  }   
   private void DBInsert()
  private void DBInsert() {
  {    using (objCmd=new SqlCommand(objInsertString,objConn))
   using (objCmd=new SqlCommand(objInsertString,objConn)) {
   { try
    try {
    { objCmd.ExecuteNonQuery();
     objCmd.ExecuteNonQuery(); }
    } catch (System.Exception e)
    catch (System.Exception e) {
    { objErrMessage+="数据插入出错:";
     objErrMessage+="数据插入出错:"; objErrMessage+="<br>";
     objErrMessage+="<br>"; objErrMessage+=e.Message;
     objErrMessage+=e.Message; objErrMessage+="<br>";
     objErrMessage+="<br>"; }
    } }
   } }
  } private void DBUpdate()
  private void DBUpdate() {
  { using (objCmd=new SqlCommand(objUpdateString,objConn))
   using (objCmd=new SqlCommand(objUpdateString,objConn)) {
   { try
    try {
    { objCmd.ExecuteNonQuery();
     objCmd.ExecuteNonQuery(); }
    } catch (System.Exception e)
    catch (System.Exception e) {
    { objErrMessage+="数据更新出错:";
     objErrMessage+="数据更新出错:"; objErrMessage+="<br>";
     objErrMessage+="<br>"; objErrMessage+=e.Message;
     objErrMessage+=e.Message; objErrMessage+="<br>";
     objErrMessage+="<br>"; }
    } }
   } }
  } private void DBDelete()
  private void DBDelete() {
  { using (objCmd=new SqlCommand(objDeleteString,objConn))
   using (objCmd=new SqlCommand(objDeleteString,objConn)) {
   { try
    try {
    { objCmd.ExecuteNonQuery();
     objCmd.ExecuteNonQuery(); }
    } catch (System.Exception e)
    catch (System.Exception e) {
    { objErrMessage+="数据删除出错:";
     objErrMessage+="数据删除出错:"; objErrMessage+="<br>";
     objErrMessage+="<br>"; objErrMessage+=e.Message;
     objErrMessage+=e.Message; objErrMessage+="<br>";
     objErrMessage+="<br>"; }
    } }
   } }
  } }
 } }
}

使用方法:
先添加引用,再创建一个DBObject对象,假设是obj,创建对象的同时就和数据库连接上了,所以用完了调用一个DBClose()方法,关闭数据库连接。
属性说明:
SelectString,UpdateString,DeleteString,InsertString,看字面意思就知道了,给这四个属性赋值后,相应的操作就完成了,我已经写在属性的Set方法中了。
给SelectString赋值后,就可以返回我们要的东西了。可以返回dataset,dataview,datareader,还可以直接就把控件和数据库绑定,具体可以查看代码。
由于水平有限,写得不好,有好的意见可以给我留言,谢谢!
 
                     
                    
                 
                    
                
 


 
  
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号