以下是数据库持久性操作共通类。
但问题:1:属于那个包
       2:怎么调用最好?


  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Data.SqlClient;  
  11.   
  12. using System.Windows.Forms;  
  13. using System.Collections.Generic;  
  14.   
  15. ///   
  16. public class DataBaseHandle  
  17. {  
  18.     #region ====== Filed & Constructor ======  
  19.     //定义SqlConnection对象  
  20.     private SqlConnection con;  
  21.     ///   
  22.     public DataBaseHandle()  
  23.     {  
  24.   
  25.     }  
  26.   
  27.     ///   
  28.     ///   
  29.     private bool DBState()  
  30.     {  
  31.         bool nReturn = false;  
  32.         try  
  33.         {  
  34.             con = new SqlConnection("在此写上你的sql连接string");  
  35.             con.Open();  
  36.             nReturn = true;  
  37.         }  
  38.         catch  
  39.         {  
  40.             con.Close();  
  41.             nReturn = false;  
  42.         }  
  43.         return nReturn;  
  44.     }  
  45.  
  46.     #endregion ====== Filed & Constructor ======  
  47.   
  48.     #region ====== Insert ======  
  49.   
  50.     ///   
  51.     /// 传入一个Table  
  52.     ///   
  53.     ///   
  54.     ///   
  55.     public int Insert(DataTable datInsert, string strTableName)  
  56.     {  
  57.         int nReturn = -1;  
  58.         if (!DBState() || datInsert == null || datInsert.Rows.Count == 0)  
  59.         { return nReturn; } //如果DB连接失败,或没传入要插入的表,则返回-1;  
  60.         //拼sql  
  61.         string strSql = GetInsertSql(datInsert, strTableName);  
  62.         if (string.IsNullOrEmpty(strSql))  
  63.         { return nReturn; }  
  64.   
  65.         try  
  66.         {  
  67.             //用sqlCommand执行sql  
  68.             SqlCommand sqlCom = new SqlCommand(strSql, con);  
  69.             sqlCom.ExecuteNonQuery();  
  70.             nReturn = 0;  
  71.         }  
  72.         catch (Exception)  
  73.         {  
  74.             return -1;  
  75.         }  
  76.   
  77.         return nReturn;  
  78.     }  
  79.   
  80.     ///   
  81.     /// 传入的表  
  82.     ///   
  83.     ///   
  84.     private string GetInsertSql(DataTable datInsert, string strTableName)  
  85.     {  
  86.         string strReturnSql = string.Empty;  
  87.         string strHead = " Insert into [" + strTableName + "]";  
  88.         string strCols = "";  
  89.         string strTempCol = string.Empty;  
  90.         if (datInsert == null || datInsert.Rows.Count == 0)  
  91.         {  
  92.             return strReturnSql;  
  93.         }  
  94.         for (int i = 0; i < datInsert.Columns.Count; i++)  
  95.         {  
  96.             strTempCol += "," + datInsert.Columns[i].ColumnName.ToString();  
  97.         }  
  98.         strTempCol = strTempCol.Substring(1);  
  99.         strCols = " (" + strTempCol + ") " + " values ";  
  100.         //int nRowCount = datInsert.Rows.Count;  
  101.         List<string> strArrValues = new List<string>();  
  102.   
  103.         //string[] strArrValues = new string[2] {string.Empty,string.Empty};  
  104.   
  105.         int nReturn = GetInserValue(datInsert, strArrValues);  
  106.         if (nReturn == -1) return string.Empty;  
  107.   
  108.         for (int j = 0; j < strArrValues.Count; j++)  
  109.         {  
  110.             strReturnSql += strHead + strCols + " (" + strArrValues[j] + ") ";  
  111.         }  
  112.         return strReturnSql;  
  113.     }  
  114.     ///   
  115.     ///   
  116.     ///   
  117.     ///   
  118.     ///   
  119.     private int GetInserValue(DataTable datInsert, List<string> strArrValues)  
  120.     {  
  121.         if (datInsert == null || datInsert.Rows.Count == 0)  
  122.         {  
  123.             return -1;  
  124.         }  
  125.         string strRowValue = string.Empty;  
  126.         for (int i = 0; i < datInsert.Rows.Count; i++)  
  127.         {  
  128.             strRowValue = string.Empty;  
  129.             for (int j = 0; j < datInsert.Columns.Count; j++)  
  130.             {  
  131.                 strRowValue += "," + "'" + datInsert.Rows[i][j].ToString() + "'";  
  132.             }  
  133.             strArrValues.Add(strRowValue.Substring(1));  
  134.         }  
  135.         return 0;  
  136.     }  
  137.     #endregion ====== Insert ======  
  138.  
  139.     #region ====== Delete ======  
  140.     ///   
  141.     ///   
  142.     ///   
  143.     ///   
  144.     ///   
  145.     ///   
  146.     public int Delete(SqlParameter[] sqlPara, string strAlreadySql, bool bUseCustomSql)  
  147.     {  
  148.         int nReturn = -1;  
  149.         if (!DBState()) //如果DB连接失败,则返回-1;  
  150.         { return nReturn; }  
  151.         if (bUseCustomSql)  
  152.         {  
  153.             SqlCommand sqlCom = new SqlCommand(strAlreadySql, con);  
  154.             sqlCom.ExecuteNonQuery();  
  155.         }  
  156.         else  
  157.         {  
  158.             SqlCommand sqlCommand = new SqlCommand(strAlreadySql, con);  
  159.             //把防sql注入的sql变量加入sqlCommand对象  
  160.             for (int i = 0; i < sqlPara.Length; i++)  
  161.             {  
  162.                 sqlCommand.Parameters.Add(sqlPara[i]);  
  163.             }  
  164.             sqlCommand.ExecuteNonQuery();  
  165.         }  
  166.         return nReturn;  
  167.     }  
  168.  
  169.     #endregion ====== Delete ======  
  170.   
  171.     #region ====== Update ======  
  172.   
  173.     ///   
  174.     ///   
  175.     ///   
  176.     ///   
  177.     ///   
  178.     ///      
  179.     public int Update(DataTable datUpdate, string strTableName, string[] strCondition)  
  180.     {  
  181.         int nReturn = -1;  
  182.         if (!DBState()) //如果DB连接失败,则返回-1;  
  183.         { return nReturn; }  
  184.         try  
  185.         {  
  186.             //拼update sql  
  187.             string strUpdateSql = GetUpdateSql(datUpdate, strTableName, strCondition);  
  188.             if (string.IsNullOrEmpty(strUpdateSql)) { return -1; }  
  189.             SqlCommand com = new SqlCommand(strUpdateSql, con);  
  190.             com.ExecuteNonQuery();  
  191.             nReturn = 0;  
  192.         }  
  193.         catch (Exception)  
  194.         {  
  195.             nReturn = -1;   
  196.         }  
  197.         return nReturn;  
  198.     }  
  199.   
  200.     ///   
  201.     ///   
  202.     ///   
  203.     ///   
  204.     ///   
  205.     private string GetUpdateSql(DataTable datUpdate, string strTableName, string[] strCondition)  
  206.     {  
  207.         string strReturn = string.Empty;  
  208.         string strStart = " Update " + strTableName;  
  209.         List<string> strSetValues = new List<string>();  
  210.         int nReturn = GetSetSql(datUpdate, strSetValues);  
  211.         if (nReturn == -1)  
  212.         { return string.Empty; }  
  213.         for (int i = 0; i < strSetValues.Count; i++)  
  214.         {  
  215.             strReturn += strStart + strSetValues[i] + strCondition[i];   
  216.         }  
  217.         return strReturn;  
  218.     }  
  219.   
  220.     ///   
  221.     ///   
  222.     ///   
  223.     ///   
  224.     ///     
  225.     private int GetSetSql(DataTable datUpdate, List<string> strSetValues)  
  226.     {  
  227.         int nReturn = -1;  
  228.         if (datUpdate == null || datUpdate.Rows.Count == 0)  
  229.         {  
  230.             return -1;  
  231.         }  
  232.         try  
  233.         {  
  234.             for (int i = 0; i < datUpdate.Rows.Count; i++)  
  235.             {  
  236.                 string str = string.Empty;  
  237.                 for (int j = 0; j < datUpdate.Columns.Count; j++)  
  238.                 {  
  239.                     str += " , " + datUpdate.Columns[j].ColumnName + "=" + "'" + datUpdate.Rows[i][j].ToString() + "'" ;  
  240.                 }  
  241.                 string strSql = " Set " + "  " + str.Substring(2) + "  ";  
  242.                 strSetValues.Add(strSql);  
  243.             }  
  244.             nReturn = 0;  
  245.         }  
  246.         catch(Exception)  
  247.         {  
  248.             nReturn = -1;   
  249.         }  
  250.         return nReturn;  
  251.     }  
  252.     #endregion ====== Update ======  
  253.  
  254.     #region ====== Search ======  
  255.   
  256.     ///   
  257.     ///   
  258.     ///   
  259.     ///   
  260.     ///   
  261.     ///   
  262.     ///    
  263.     public int Search(out DataTable datReturn, SqlParameter[] sqlPara, string strAlreadySql, bool bUseCustomSql)  
  264.     {  
  265.         datReturn = new DataTable();  
  266.         int nReturn = -1;  
  267.         if (!DBState()) //如果DB连接失败,则返回-1;  
  268.         { return nReturn; }  
  269.         try  
  270.         {  
  271.             if (bUseCustomSql)//如果用自己写的sql  
  272.             {  
  273.                 SqlCommand sqlCom = new SqlCommand(strAlreadySql, con);  
  274.                 SqlDataAdapter dap = new SqlDataAdapter(sqlCom);  
  275.                 dap.Fill(datReturn);  
  276.                 nReturn = 0;  
  277.             }  
  278.             else  //不用自己写的sql  
  279.             {  
  280.                 SqlCommand sqlCommand = new SqlCommand(strAlreadySql, con);  
  281.                 for (int i = 0; i < sqlPara.Length; i++)  
  282.                 {  
  283.                     if (sqlPara[i] != null)  
  284.                     {  
  285.                         sqlCommand.Parameters.Add(sqlPara[i]);  
  286.                     }  
  287.                 }  
  288.                 //取得数据放在表中.  
  289.                 SqlDataAdapter sqlDap = new SqlDataAdapter(sqlCommand);  
  290.                 sqlDap.Fill(datReturn);  
  291.                 nReturn = 0;  
  292.             }  
  293.         }  
  294.         catch(Exception)  
  295.         {  
  296.             nReturn = -1;   
  297.         }  
  298.         return nReturn;  
  299.     }  
  300.  
  301.     #endregion ====== Search ======  


10-31 11:00
查看更多