收藏 分销(赏)

DBHelper类.doc

上传人:精*** 文档编号:3561795 上传时间:2024-07-09 格式:DOC 页数:16 大小:87KB 下载积分:8 金币
下载 相关
DBHelper类.doc_第1页
第1页 / 共16页
DBHelper类.doc_第2页
第2页 / 共16页


点击查看更多>>
资源描述
1. using System;   2. using System.Collections.Generic;   3. using System.Linq;   4. using System.Text;   5. using System.Data.SqlClient;     6. using System.Collections;   7. using System.Data;   8. using System.Configuration;   9. using System.Web;   10.    11.    12. public sealed class SqlHelper   13. {   14.    15.     public static string connectionString = ConfigurationManager.ConnectionStrings["db_JXCconn"].ConnectionString;   16.    17.     public SqlHelper()   18.     {   19.     }   20.   21.     #region 公用方法   22.     public static int GetMaxID(string FieldName, string TableName)   23.     {   24.         string strsql = "select max(" + FieldName + ")+1 from " + TableName;   25.         object obj = SqlHelper.GetSingle(strsql);   26.         if (obj == null)   27.         {   28.             return 1;   29.         }   30.         else   31.         {   32.             return int.Parse(obj.ToString());   33.         }   34.     }   35.    36.     public static bool Exists(string strSql)   37.     {   38.         object obj = SqlHelper.GetSingle(strSql);   39.         int cmdresult;   40.         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))   41.         {   42.             cmdresult = 0;   43.         }   44.         else   45.         {   46.             cmdresult = int.Parse(obj.ToString());   47.         }   48.         if (cmdresult == 0)   49.         {   50.             return false;   51.         }   52.         else   53.         {   54.             return true;   55.         }   56.     }   57.    58.     public static bool Exists(string strSql, params SqlParameter[] cmdParms)   59.     {   60.         object obj = SqlHelper.GetSingle(strSql, cmdParms);   61.         int cmdresult;   62.         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))   63.         {   64.             cmdresult = 0;   65.         }   66.         else   67.         {   68.             cmdresult = int.Parse(obj.ToString());   69.         }   70.         if (cmdresult == 0)   71.         {   72.             return false;   73.         }   74.         else   75.         {   76.             return true;   77.         }   78.     }   79.     #endregion   80.   81.     #region  执行简单SQL语句   82.     /// <summary>   83.     /// 执行SQL语句,返回影响的记录数   84.     /// </summary>   85.     /// <param name="SQLString">SQL语句</param>   86.     /// <returns>影响的记录数</returns>   87.     public static int ExecuteSql(string SQLString)   88.     {   89.         using (SqlConnection connection = new SqlConnection(connectionString))   90.         {   91.             using (SqlCommand cmd = new SqlCommand(SQLString, connection))   92.             {   93.                 try   94.                 {   95.                     connection.Open();   96.                     int rows = cmd.ExecuteNonQuery();   97.                     return rows;   98.                 }   99.                 catch (System.Data.SqlClient.SqlException E)   100.                 {   101.                     connection.Close();   102.                     throw new Exception(E.Message);   103.                 }   104.             }   105.         }   106.     }   107.    108.     /// <summary>   109.     /// 执行SQL语句,返回影响的记录数 适用于select语句   110.     /// </summary>   111.     /// <param name="SQLString">SQL语句</param>   112.     /// <returns>影响的记录数</returns>   113.     public static int ExecuteSql2(string SQLString)   114.     {   115.         using (SqlConnection connection = new SqlConnection(connectionString))   116.         {   117.             using (SqlCommand cmd = new SqlCommand(SQLString, connection))   118.             {   119.                 try   120.                 {   121.                     connection.Open();   122.                     int rows = Convert.ToInt32(cmd.ExecuteScalar());   123.                     return rows;   124.                 }   125.                 catch (System.Data.SqlClient.SqlException E)   126.                 {   127.                     connection.Close();   128.                     throw new Exception(E.Message);   129.                 }   130.             }   131.         }   132.     }   133.    134.     /// <summary>   135.     /// 执行多条SQL语句,实现数据库事务。   136.     /// </summary>   137.     /// <param name="SQLStringList">多条SQL语句</param>        138.     public static void ExecuteSqlTran(ArrayList SQLStringList)   139.     {   140.         using (SqlConnection conn = new SqlConnection(connectionString))   141.         {   142.             conn.Open();   143.             SqlCommand cmd = new SqlCommand();   144.             cmd.Connection = conn;   145.             SqlTransaction tx = conn.BeginTransaction();   146.             cmd.Transaction = tx;   147.             try   148.             {   149.                 for (int n = 0; n < SQLStringList.Count; n++)   150.                 {   151.                     string strsql = SQLStringList[n].ToString();   152.                     if (strsql.Trim().Length > 1)   153.                     {   154.                         cmd.CommandText = strsql;   155.                         cmd.ExecuteNonQuery();   156.                     }   157.                 }   158.                 tx.Commit();   159.             }   160.             catch (System.Data.SqlClient.SqlException E)   161.             {   162.                 tx.Rollback();   163.                 throw new Exception(E.Message);   164.             }   165.         }   166.     }   167.    168.     /// <summary>   169.     /// 执行带一个存储过程参数的的SQL语句。   170.     /// </summary>   171.     /// <param name="SQLString">SQL语句</param>   172.     /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>   173.     /// <returns>影响的记录数</returns>   174.     public static int ExecuteSql(string SQLString, string content)   175.     {   176.         using (SqlConnection connection = new SqlConnection(connectionString))   177.         {   178.             SqlCommand cmd = new SqlCommand(SQLString, connection);   179.             System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.VarChar);   180.    181.             myParameter.Value = content;   182.             cmd.Parameters.Add(myParameter);   183.             try   184.             {   185.                 connection.Open();   186.                 int rows = cmd.ExecuteNonQuery();   187.                 return rows;   188.             }   189.             catch (System.Data.SqlClient.SqlException E)   190.             {   191.                 throw new Exception(E.Message);   192.             }   193.             finally   194.             {   195.                 cmd.Dispose();   196.                 connection.Close();   197.             }   198.         }   199.     }   200.    201.     /// <summary>   202.     /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)   203.     /// </summary>   204.     /// <param name="strSQL">SQL语句</param>   205.     /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>   206.     /// <returns>影响的记录数</returns>   207.     public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)   208.     {   209.         using (SqlConnection connection = new SqlConnection(connectionString))   210.         {   211.             SqlCommand cmd = new SqlCommand(strSQL, connection);   212.             System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Binary);   213.             myParameter.Value = fs;   214.             cmd.Parameters.Add(myParameter);   215.             try   216.             {   217.                 connection.Open();   218.                 int rows = cmd.ExecuteNonQuery();   219.                 return rows;   220.             }   221.             catch (System.Data.SqlClient.SqlException E)   222.             {   223.                 throw new Exception(E.Message);   224.             }   225.             finally   226.             {   227.                 cmd.Dispose();   228.                 connection.Close();   229.             }   230.         }   231.     }   232.    233.     /// <summary>   234.     /// 执行一条计算查询结果语句,返回查询结果(object)。   235.     /// </summary>   236.     /// <param name="SQLString">计算查询结果语句</param>   237.     /// <returns>查询结果(object)</returns>   238.     public static object GetSingle(string SQLString)   239.     {   240.         using (SqlConnection connection = new SqlConnection(connectionString))   241.         {   242.             using (SqlCommand cmd = new SqlCommand(SQLString, connection))   243.             {   244.                 try   245.                 {   246.                     connection.Open();   247.                     object obj = cmd.ExecuteScalar();   248.                     if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))   249.                     {   250.                         return null;   251.                     }   252.                     else   253.                     {   254.                         return obj;   255.                     }   256.                 }   257.                 catch (System.Data.SqlClient.SqlException e)   258.                 {   259.                     connection.Close();   260.                     throw new Exception(e.Message);   261.                 }   262.             }   263.         }   264.     }   265.    266.     /// <summary>   267.     /// 执行查询语句,返回SqlDataReader   268.     /// </summary>   269.     /// <param name="strSQL">查询语句</param>   270.     /// <returns>SqlDataReader</returns>   271.     public static SqlDataReader ExecuteReader(string strSQL)   272.     {   273.         SqlConnection connection = new SqlConnection(connectionString);   274.         SqlCommand cmd = new SqlCommand(strSQL, connection);   275.         try   276.         {   277.             connection.Open();   278.             SqlDataReader myReader = cmd.ExecuteReader();   279.             return myReader;   280.         }   281.         catch (System.Data.SqlClient.SqlException e)   282.         {   283.             throw new Exception(e.Message);   284.         }   285.    286.     }   287.    288.     /// <summary>   289.     /// 执行查询语句,返回DataSet   290.     /// </summary>   291.     /// <param name="SQLString">查询语句</param>   292.     /// <returns>DataSet</returns>   293.     public static DataSet Query(string SQLString)   294.     {   295.         using (SqlConnection connection = new SqlConnection(connectionString))   296.         {   297.             DataSet ds = new DataSet();   298.             try   299.             {   300.                 connection.Open();   301.                 SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);   302.    303.                 command.Fill(ds, "ds");   304.             }   305.             catch (System.Data.SqlClient.SqlException ex)   306.             {   307.                 throw new Exception(ex.Message);   308.             }   309.             return ds;   310.         }   311.     }   312.    313.     /// <summary>   314.     /// 执行查询语句,返回datatable   315.     /// </summary>   316.     /// <param name="SQLString">查询语句</param>   317.     /// <returns>DataSet</returns>   318.     public static DataTable QueryTable(string SQLString)   319.     {   320.         using (SqlConnection connection = new SqlConnection(connectionString))   321.         {   322.             DataSet ds = new DataSet();   323.             try   324.             {   325.                 connection.Open();   326.                 SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);   327.                 command.Fill(ds, "ds");   328.             }   329.             catch (System.Data.SqlClient.SqlException ex)   330.             {   331.                 throw new Exception(ex.Message);   332.             }   333.             return ds.Tables[0];   334.         }   335.     }   336.     #endregion   337.   338.     #region 执行带参数的SQL语句   339.     /// <summary>   340.     /// 执行SQL语句,返回影响的记录数   341.     /// </summary>   342.     /// <param name="SQLString">SQL语句</param>   343.     /// <returns>影响的记录数</returns>   344.     public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)   345.     {   346.         using (SqlConnection connection = new SqlConnection(connectionString))   347.         {   348.             using (SqlCommand cmd = new SqlCommand())   349.             {   350.                 try   351.                 {   352.                     PrepareCommand(cmd, connection, null, SQLString, cmdParms);   353.                     int rows = cmd.ExecuteNonQuery();   354.                     cmd.Parameters.Clear();   355.                     return rows;   356.                 }   357.                 catch (System.Data.SqlClient.SqlException E)   358.                 {   359.                     throw new Exception(E.Message);   360.                 }   361.             }   362.         }   363.     }   364.    365.     /// <summary>   366.     /// 执行多条SQL语句,实现数据库事务。   367.     /// </summary>   368.     /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>   369.     public static void ExecuteSqlTran(Hashtable SQLStringList)   370.     {   371.         using (SqlConnection conn = new SqlConnection(connectionString))   372.         {   373.             conn.Open();   374.             using (SqlTransaction trans = conn.BeginTransaction())   375.             {   376.                 SqlCommand cmd = new SqlCommand();   377.                 try   378.                 {   379.                     //循环   380.                     foreach (DictionaryEntry myDE in SQLStringList)   381.                     {   382.                         string cmdText = myDE.Key.ToString();   383.                         SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;   384.                         PrepareCommand(cmd, conn, trans, cmdText, cmdParms);   385.                         int val = cmd.ExecuteNonQuery();   386.                         cmd.Parameters.Clear();   387.    388.                         trans.Commit();   389.                     }   390.                 }   391.                 catch   392.                 {   393.                     trans.Rollback();   394.                     throw;   395.                 }   396.             }   397.         }   398.     }   399.    400.     /// <summary>   401.     /// 执行一条计算查询结果语句,返回查询结果(object)。   402.     /// </summary>   403.     /// <param name="SQLString">计
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服