数据库访问类

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Specialized;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Configuration;  
  7.   
  8. namespace RedGlove.DBUtility  
  9. {  
  10.     public class SqlServerHelper  
  11.     {  
  12.         //数据库连接字符串(web.config来配置),可以动态更改SQLString支持多数据库.          
  13.         public static string connectionString = ConfigurationManager.AppSettings["SQLString"];  
  14.   
  15.         public SqlServerHelper() { }  
  16.  
  17.         #region 公用方法  
  18.   
  19.         public static int GetMaxID(string FieldName, string TableName)  
  20.         {  
  21.             string strsql = "select max(" + FieldName + ")+1 from " + TableName;  
  22.             object obj = GetSingle(strsql);  
  23.             if (obj == null)  
  24.             {  
  25.                 return 1;  
  26.             }  
  27.             else  
  28.             {  
  29.                 return int.Parse(obj.ToString());  
  30.             }  
  31.         }  
  32.   
  33.         public static bool Exists(string strSql)  
  34.         {  
  35.             object obj = GetSingle(strSql);  
  36.             int cmdresult;  
  37.             if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))  
  38.             {  
  39.                 cmdresult = 0;  
  40.             }  
  41.             else  
  42.             {  
  43.                 cmdresult = int.Parse(obj.ToString());  
  44.             }  
  45.             if (cmdresult == 0)  
  46.             {  
  47.                 return false;  
  48.             }  
  49.             else  
  50.             {  
  51.                 return true;  
  52.             }  
  53.         }  
  54.   
  55.         public static bool Exists(string strSql, params SqlParameter[] cmdParms)  
  56.         {  
  57.             object obj = GetSingle(strSql, cmdParms);  
  58.             int cmdresult;  
  59.             if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))  
  60.             {  
  61.                 cmdresult = 0;  
  62.             }  
  63.             else  
  64.             {  
  65.                 cmdresult = int.Parse(obj.ToString());  
  66.             }  
  67.             if (cmdresult == 0)  
  68.             {  
  69.                 return false;  
  70.             }  
  71.             else  
  72.             {  
  73.                 return true;  
  74.             }  
  75.         }  
  76.  
  77.         #endregion  
  78.  
  79.         #region  执行简单SQL语句  
  80.   
  81.         /// <summary>  
  82.         /// 执行SQL语句,返回影响的记录数  
  83.         /// </summary>  
  84.         /// <param name="SQLString">SQL语句</param>  
  85.         /// <returns>影响的记录数</returns>  
  86.         public static int ExecuteSql(string SQLString)  
  87.         {  
  88.             using (SqlConnection connection = new SqlConnection(connectionString))  
  89.             {  
  90.                 using (SqlCommand cmd = new SqlCommand(SQLString, connection))  
  91.                 {  
  92.                     try  
  93.                     {  
  94.                         connection.Open();  
  95.                         int rows = cmd.ExecuteNonQuery();  
  96.                         return rows;  
  97.                     }  
  98.                     catch (System.Data.SqlClient.SqlException E)  
  99.                     {  
  100.                         connection.Close();  
  101.                         throw new Exception(E.Message);  
  102.                     }  
  103.                 }  
  104.             }  
  105.         }  
  106.   
  107.         /// <summary>  
  108.         /// 执行SQL语句,设置命令的执行等待时间  
  109.         /// </summary>  
  110.         /// <param name="SQLString"></param>  
  111.         /// <param name="Times"></param>  
  112.         /// <returns></returns>  
  113.         public static int ExecuteSqlByTime(string SQLString, int Times)  
  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.                         cmd.CommandTimeout = Times;  
  123.                         int rows = cmd.ExecuteNonQuery();  
  124.                         return rows;  
  125.                     }  
  126.                     catch (System.Data.SqlClient.SqlException E)  
  127.                     {  
  128.                         connection.Close();  
  129.                         throw new Exception(E.Message);  
  130.                     }  
  131.                 }  
  132.             }  
  133.         }  
  134.   
  135.         /// <summary>  
  136.         /// 执行多条SQL语句,实现数据库事务。  
  137.         /// </summary>  
  138.         /// <param name="SQLStringList">多条SQL语句</param>       
  139.         public static void ExecuteSqlTran(ArrayList SQLStringList)  
  140.         {  
  141.             using (SqlConnection conn = new SqlConnection(connectionString))  
  142.             {  
  143.                 conn.Open();  
  144.                 SqlCommand cmd = new SqlCommand();  
  145.                 cmd.Connection = conn;  
  146.                 SqlTransaction tx = conn.BeginTransaction();  
  147.                 cmd.Transaction = tx;  
  148.                 try  
  149.                 {  
  150.                     for (int n = 0; n < SQLStringList.Count; n++)  
  151.                     {  
  152.                         string strsql = SQLStringList[n].ToString();  
  153.                         if (strsql.Trim().Length > 1)  
  154.                         {  
  155.                             cmd.CommandText = strsql;  
  156.                             cmd.ExecuteNonQuery();  
  157.                         }  
  158.                     }  
  159.                     tx.Commit();  
  160.                 }  
  161.                 catch (System.Data.SqlClient.SqlException E)  
  162.                 {  
  163.                     tx.Rollback();  
  164.                     throw new Exception(E.Message);  
  165.                 }  
  166.             }  
  167.         }  
  168.   
  169.         /// <summary>  
  170.         /// 执行带一个存储过程参数的的SQL语句。  
  171.         /// </summary>  
  172.         /// <param name="SQLString">SQL语句</param>  
  173.         /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>  
  174.         /// <returns>影响的记录数</returns>  
  175.         public static int ExecuteSql(string SQLString, string content)  
  176.         {  
  177.             using (SqlConnection connection = new SqlConnection(connectionString))  
  178.             {  
  179.                 SqlCommand cmd = new SqlCommand(SQLString, connection);  
  180.                 System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);  
  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.         /// 执行带一个存储过程参数的的SQL语句。  
  203.         /// </summary>  
  204.         /// <param name="SQLString">SQL语句</param>  
  205.         /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>  
  206.         /// <returns>影响的记录数</returns>  
  207.         public static object ExecuteSqlGet(string SQLString, string content)  
  208.         {  
  209.             using (SqlConnection connection = new SqlConnection(connectionString))  
  210.             {  
  211.                 SqlCommand cmd = new SqlCommand(SQLString, connection);  
  212.                 System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);  
  213.                 myParameter.Value = content;  
  214.                 cmd.Parameters.Add(myParameter);  
  215.                 try  
  216.                 {  
  217.                     connection.Open();  
  218.                     object obj = cmd.ExecuteScalar();  
  219.                     if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))  
  220.                     {  
  221.                         return null;  
  222.                     }  
  223.                     else  
  224.                     {  
  225.                         return obj;  
  226.                     }  
  227.                 }  
  228.                 catch (System.Data.SqlClient.SqlException E)  
  229.                 {  
  230.                     throw new Exception(E.Message);  
  231.                 }  
  232.                 finally  
  233.                 {  
  234.                     cmd.Dispose();  
  235.                     connection.Close();  
  236.                 }  
  237.             }  
  238.         }  
  239.   
  240.         /// <summary>  
  241.         /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)  
  242.         /// </summary>  
  243.         /// <param name="strSQL">SQL语句</param>  
  244.         /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>  
  245.         /// <returns>影响的记录数</returns>  
  246.         public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)  
  247.         {  
  248.             using (SqlConnection connection = new SqlConnection(connectionString))  
  249.             {  
  250.                 SqlCommand cmd = new SqlCommand(strSQL, connection);  
  251.                 System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);  
  252.                 myParameter.Value = fs;  
  253.                 cmd.Parameters.Add(myParameter);  
  254.                 try  
  255.                 {  
  256.                     connection.Open();  
  257.                     int rows = cmd.ExecuteNonQuery();  
  258.                     return rows;  
  259.                 }  
  260.                 catch (System.Data.SqlClient.SqlException E)  
  261.                 {  
  262.                     throw new Exception(E.Message);  
  263.                 }  
  264.                 finally  
  265.                 {  
  266.                     cmd.Dispose();  
  267.                     connection.Close();  
  268.                 }  
  269.             }  
  270.         }  
  271.   
  272.         /// <summary>  
  273.         /// 执行一条计算查询结果语句,返回查询结果(object)。  
  274.         /// </summary>  
  275.         /// <param name="SQLString">计算查询结果语句</param>  
  276.         /// <returns>查询结果(object)</returns>  
  277.         public static object GetSingle(string SQLString)  
  278.         {  
  279.             using (SqlConnection connection = new SqlConnection(connectionString))  
  280.             {  
  281.                 using (SqlCommand cmd = new SqlCommand(SQLString, connection))  
  282.                 {  
  283.                     try  
  284.                     {  
  285.                         connection.Open();  
  286.                         object obj = cmd.ExecuteScalar();  
  287.                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))  
  288.                         {  
  289.                             return null;  
  290.                         }  
  291.                         else  
  292.                         {  
  293.                             return obj;  
  294.                         }  
  295.                     }  
  296.                     catch (System.Data.SqlClient.SqlException e)  
  297.                     {  
  298.                         connection.Close();  
  299.                         throw new Exception(e.Message);  
  300.                     }  
  301.                 }  
  302.             }  
  303.         }  
  304.   
  305.         /// <summary>  
  306.         /// 执行查询语句,返回SqlDataReader(使用该方法切记要手工关闭SqlDataReader和连接)  
  307.         /// </summary>  
  308.         /// <param name="strSQL">查询语句</param>  
  309.         /// <returns>SqlDataReader</returns>  
  310.         public static SqlDataReader ExecuteReader(string strSQL)  
  311.         {  
  312.             SqlConnection connection = new SqlConnection(connectionString);  
  313.             SqlCommand cmd = new SqlCommand(strSQL, connection);  
  314.             try  
  315.             {  
  316.                 connection.Open();  
  317.                 SqlDataReader myReader = cmd.ExecuteReader();  
  318.                 return myReader;  
  319.             }  
  320.             catch (System.Data.SqlClient.SqlException e)  
  321.             {  
  322.                 throw new Exception(e.Message);  
  323.             }  
  324.             //finally //不能在此关闭,否则,返回的对象将无法使用  
  325.             //{  
  326.             //  cmd.Dispose();  
  327.             //  connection.Close();  
  328.             //}   
  329.         }  
  330.   
  331.         /// <summary>  
  332.         /// 执行查询语句,返回DataSet  
  333.         /// </summary>  
  334.         /// <param name="SQLString">查询语句</param>  
  335.         /// <returns>DataSet</returns>  
  336.         public static DataSet Query(string SQLString)  
  337.         {  
  338.             using (SqlConnection connection = new SqlConnection(connectionString))  
  339.             {  
  340.                 DataSet ds = new DataSet();  
  341.                 try  
  342.                 {  
  343.                     connection.Open();  
  344.                     SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);  
  345.                     command.Fill(ds, "ds");  
  346.                 }  
  347.                 catch (System.Data.SqlClient.SqlException ex)  
  348.                 {  
  349.                     throw new Exception(ex.Message);  
  350.                 }  
  351.                 return ds;  
  352.             }  
  353.         }  
  354.   
  355.         public static DataSet Query(string SQLString, string TableName)  
  356.         {  
  357.             using (SqlConnection connection = new SqlConnection(connectionString))  
  358.             {  
  359.                 DataSet ds = new DataSet();  
  360.                 try  
  361.                 {  
  362.                     connection.Open();  
  363.                     SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);  
  364.                     command.Fill(ds, TableName);  
  365.                 }  
  366.                 catch (System.Data.SqlClient.SqlException ex)  
  367.                 {  
  368.                     throw new Exception(ex.Message);  
  369.                 }  
  370.                 return ds;  
  371.             }  
  372.         }  
  373.   
  374.         /// <summary>  
  375.         /// 执行查询语句,返回DataSet,设置命令的执行等待时间  
  376.         /// </summary>  
  377.         /// <param name="SQLString"></param>  
  378.         /// <param name="Times"></param>  
  379.         /// <returns></returns>  
  380.         public static DataSet Query(string SQLString, int Times)  
  381.         {  
  382.             using (SqlConnection connection = new SqlConnection(connectionString))  
  383.             {  
  384.                 DataSet ds = new DataSet();  
  385.                 try  
  386.                 {  
  387.                     connection.Open();  
  388.                     SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);  
  389.                     command.SelectCommand.CommandTimeout = Times;  
  390.                     command.Fill(ds, "ds");  
  391.                 }  
  392.                 catch (System.Data.SqlClient.SqlException ex)  
  393.                 {  
  394.                     throw new Exception(ex.Message);  
  395.                 }  
  396.                 return ds;  
  397.             }  
  398.         }  
  399.  
  400.         #endregion  
  401.  
  402.         #region 执行带参数的SQL语句  
  403.   
  404.         /// <summary>  
  405.         /// 执行SQL语句,返回影响的记录数  
  406.         /// </summary>  
  407.         /// <param name="SQLString">SQL语句</param>  
  408.         /// <returns>影响的记录数</returns>  
  409.         public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)  
  410.         {  
  411.             using (SqlConnection connection = new SqlConnection(connectionString))  
  412.             {  
  413.                 using (SqlCommand cmd = new SqlCommand())  
  414.                 {  
  415.                     try  
  416.                     {  
  417.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);  
  418.                         int rows = cmd.ExecuteNonQuery();  
  419.                         cmd.Parameters.Clear();  
  420.                         return rows;  
  421.                     }  
  422.                     catch (System.Data.SqlClient.SqlException E)  
  423.                     {  
  424.                         throw new Exception(E.Message);  
  425.                     }  
  426.                 }  
  427.             }  
  428.         }  
  429.   
  430.         /// <summary>  
  431.         /// 执行多条SQL语句,实现数据库事务。  
  432.         /// </summary>  
  433.         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>  
  434.         public static void ExecuteSqlTran(Hashtable SQLStringList)  
  435.         {  
  436.             using (SqlConnection conn = new SqlConnection(connectionString))  
  437.             {  
  438.                 conn.Open();  
  439.                 using (SqlTransaction trans = conn.BeginTransaction())  
  440.                 {  
  441.                     SqlCommand cmd = new SqlCommand();  
  442.                     try  
  443.                     {  
  444.                         //循环  
  445.                         foreach (DictionaryEntry myDE in SQLStringList)  
  446.                         {  
  447.                             string cmdText = myDE.Key.ToString();  
  448.                             SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;  
  449.                             PrepareCommand(cmd, conn, trans, cmdText, cmdParms);  
  450.                             int val = cmd.ExecuteNonQuery();  
  451.                             cmd.Parameters.Clear();  
  452.   
  453.                             trans.Commit();  
  454.                         }  
  455.                     }  
  456.                     catch  
  457.                     {  
  458.                         trans.Rollback();  
  459.                         throw;  
  460.                     }  
  461.                 }  
  462.             }  
  463.         }  
  464.   
  465.         /// <summary>  
  466.         /// 执行一条计算查询结果语句,返回查询结果(object)。  
  467.         /// </summary>  
  468.         /// <param name="SQLString">计算查询结果语句</param>  
  469.         /// <returns>查询结果(object)</returns>  
  470.         public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)  
  471.         {  
  472.             using (SqlConnection connection = new SqlConnection(connectionString))  
  473.             {  
  474.                 using (SqlCommand cmd = new SqlCommand())  
  475.                 {  
  476.                     try  
  477.                     {  
  478.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);  
  479.                         object obj = cmd.ExecuteScalar();  
  480.                         cmd.Parameters.Clear();  
  481.                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))  
  482.                         {  
  483.                             return null;  
  484.                         }  
  485.                         else  
  486.                         {  
  487.                             return obj;  
  488.                         }  
  489.                     }  
  490.                     catch (System.Data.SqlClient.SqlException e)  
  491.                     {  
  492.                         throw new Exception(e.Message);  
  493.                     }  
  494.                 }  
  495.             }  
  496.         }  
  497.   
  498.         /// <summary>  
  499.         /// 执行查询语句,返回SqlDataReader (使用该方法切记要手工关闭SqlDataReader和连接)  
  500.         /// </summary>  
  501.         /// <param name="strSQL">查询语句</param>  
  502.         /// <returns>SqlDataReader</returns>  
  503.         public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)  
  504.         {  
  505.             SqlConnection connection = new SqlConnection(connectionString);  
  506.             SqlCommand cmd = new SqlCommand();  
  507.             try  
  508.             {  
  509.                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);  
  510.                 SqlDataReader myReader = cmd.ExecuteReader();  
  511.                 cmd.Parameters.Clear();  
  512.                 return myReader;  
  513.             }  
  514.             catch (System.Data.SqlClient.SqlException e)  
  515.             {  
  516.                 throw new Exception(e.Message);  
  517.             }  
  518.             //finally //不能在此关闭,否则,返回的对象将无法使用  
  519.             //{  
  520.             //  cmd.Dispose();  
  521.             //  connection.Close();  
  522.             //}   
  523.   
  524.         }  
  525.   
  526.         /// <summary>  
  527.         /// 执行查询语句,返回DataSet  
  528.         /// </summary>  
  529.         /// <param name="SQLString">查询语句</param>  
  530.         /// <returns>DataSet</returns>  
  531.         public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)  
  532.         {  
  533.             using (SqlConnection connection = new SqlConnection(connectionString))  
  534.             {  
  535.                 SqlCommand cmd = new SqlCommand();  
  536.                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);  
  537.                 using (SqlDataAdapter da = new SqlDataAdapter(cmd))  
  538.                 {  
  539.                     DataSet ds = new DataSet();  
  540.                     try  
  541.                     {  
  542.                         da.Fill(ds, "ds");  
  543.                         cmd.Parameters.Clear();  
  544.                     }  
  545.                     catch (System.Data.SqlClient.SqlException ex)  
  546.                     {  
  547.                         throw new Exception(ex.Message);  
  548.                     }  
  549.                     return ds;  
  550.                 }  
  551.             }  
  552.         }  
  553.   
  554.         public static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)  
  555.         {  
  556.             if (conn.State != ConnectionState.Open)  
  557.                 conn.Open();  
  558.             cmd.Connection = conn;  
  559.             cmd.CommandText = cmdText;  
  560.             if (trans != null)  
  561.                 cmd.Transaction = trans;  
  562.             cmd.CommandType = CommandType.Text;//cmdType;  
  563.             if (cmdParms != null)  
  564.             {  
  565.   
  566.   
  567.                 foreach (SqlParameter parameter in cmdParms)  
  568.                 {  
  569.                     if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&  
  570.                         (parameter.Value == null))  
  571.                     {  
  572.                         parameter.Value = DBNull.Value;  
  573.                     }  
  574.                     cmd.Parameters.Add(parameter);  
  575.                 }  
  576.             }  
  577.         }  
  578.  
  579.         #endregion  
  580.  
  581.         #region 存储过程操作  
  582.   
  583.         /// <summary>  
  584.         /// 执行存储过程  (使用该方法切记要手工关闭SqlDataReader和连接)  
  585.         /// </summary>  
  586.         /// <param name="storedProcName">存储过程名</param>  
  587.         /// <param name="parameters">存储过程参数</param>  
  588.         /// <returns>SqlDataReader</returns>  
  589.         public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)  
  590.         {  
  591.             SqlConnection connection = new SqlConnection(connectionString);  
  592.             SqlDataReader returnReader;  
  593.             connection.Open();  
  594.             SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);  
  595.             command.CommandType = CommandType.StoredProcedure;  
  596.             returnReader = command.ExecuteReader();  
  597.             //Connection.Close(); 不能在此关闭,否则,返回的对象将无法使用              
  598.             return returnReader;  
  599.   
  600.         }  
  601.   
  602.         /// <summary>  
  603.         /// 执行存储过程  
  604.         /// </summary>  
  605.         /// <param name="storedProcName">存储过程名</param>  
  606.         /// <param name="parameters">存储过程参数</param>  
  607.         /// <returns>结果中第一行第一列</returns>  
  608.         public static string RunProc(string storedProcName, IDataParameter[] parameters)  
  609.         {  
  610.             using (SqlConnection connection = new SqlConnection(connectionString))  
  611.             {  
  612.                 string StrValue;  
  613.                 connection.Open();  
  614.                 SqlCommand cmd;  
  615.                 cmd = BuildQueryCommand(connection, storedProcName, parameters);  
  616.                 StrValue = cmd.ExecuteScalar().ToString();  
  617.                 connection.Close();  
  618.                 return StrValue;  
  619.             }  
  620.         }  
  621.   
  622.         /// <summary>  
  623.         /// 执行存储过程  
  624.         /// </summary>  
  625.         /// <param name="storedProcName">存储过程名</param>  
  626.         /// <param name="parameters">存储过程参数</param>  
  627.         /// <param name="tableName">DataSet结果中的表名</param>  
  628.         /// <returns>DataSet</returns>  
  629.         public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)  
  630.         {  
  631.             using (SqlConnection connection = new SqlConnection(connectionString))  
  632.             {  
  633.                 DataSet dataSet = new DataSet();  
  634.                 connection.Open();  
  635.                 SqlDataAdapter sqlDA = new SqlDataAdapter();  
  636.                 sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);  
  637.                 sqlDA.Fill(dataSet, tableName);  
  638.                 connection.Close();  
  639.                 return dataSet;  
  640.             }  
  641.         }  
  642.   
  643.         public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)  
  644.         {  
  645.             using (SqlConnection connection = new SqlConnection(connectionString))  
  646.             {  
  647.                 DataSet dataSet = new DataSet();  
  648.                 connection.Open();  
  649.                 SqlDataAdapter sqlDA = new SqlDataAdapter();  
  650.                 sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);  
  651.                 sqlDA.SelectCommand.CommandTimeout = Times;  
  652.                 sqlDA.Fill(dataSet, tableName);  
  653.                 connection.Close();  
  654.                 return dataSet;  
  655.             }  
  656.         }  
  657.   
  658.         /// <summary>  
  659.         /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)  
  660.         /// </summary>  
  661.         /// <param name="connection">数据库连接</param>  
  662.         /// <param name="storedProcName">存储过程名</param>  
  663.         /// <param name="parameters">存储过程参数</param>  
  664.         /// <returns>SqlCommand</returns>  
  665.         public static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)  
  666.         {  
  667.             SqlCommand command = new SqlCommand(storedProcName, connection);  
  668.             command.CommandType = CommandType.StoredProcedure;  
  669.             foreach (SqlParameter parameter in parameters)  
  670.             {  
  671.                 if (parameter != null)  
  672.                 {  
  673.                     // 检查未分配值的输出参数,将其分配以DBNull.Value.  
  674.                     if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&  
  675.                         (parameter.Value == null))  
  676.                     {  
  677.                         parameter.Value = DBNull.Value;  
  678.                     }  
  679.                     command.Parameters.Add(parameter);  
  680.                 }  
  681.             }  
  682.   
  683.             return command;  
  684.         }  
  685.   
  686.         /// <summary>  
  687.         /// 执行存储过程,返回影响的行数        
  688.         /// </summary>  
  689.         /// <param name="storedProcName">存储过程名</param>  
  690.         /// <param name="parameters">存储过程参数</param>  
  691.         /// <param name="rowsAffected">影响的行数</param>  
  692.         /// <returns></returns>  
  693.         public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)  
  694.         {  
  695.             using (SqlConnection connection = new SqlConnection(connectionString))  
  696.             {  
  697.                 int result;  
  698.                 connection.Open();  
  699.                 SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);  
  700.                 rowsAffected = command.ExecuteNonQuery();  
  701.                 result = (int)command.Parameters["ReturnValue"].Value;  
  702.                 //Connection.Close();  
  703.                 return result;  
  704.             }  
  705.         }  
  706.   
  707.         /// <summary>  
  708.         /// 创建 SqlCommand 对象实例(用来返回一个整数值)     
  709.         /// </summary>  
  710.         /// <param name="storedProcName">存储过程名</param>  
  711.         /// <param name="parameters">存储过程参数</param>  
  712.         /// <returns>SqlCommand 对象实例</returns>  
  713.         public static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)  
  714.         {  
  715.             SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);  
  716.             command.Parameters.Add(new SqlParameter("ReturnValue",  
  717.                 SqlDbType.Int, 4, ParameterDirection.ReturnValue,  
  718.                 false, 0, 0, string.Empty, DataRowVersion.Default, null));  
  719.             return command;  
  720.         }  
  721.   
  722.         /// <summary>  
  723.         /// 执行SQL语句  
  724.         /// </summary>  
  725.         /// <param name="storedProcName">存储过程名</param>  
  726.         /// <param name="parameters">存储过程参数</param>  
  727.         /// <returns>结果中第一行第一列</returns>  
  728.         public static string RunSql(string query)  
  729.         {  
  730.             string str;  
  731.             using (SqlConnection connection = new SqlConnection(connectionString))  
  732.             {  
  733.                 using (SqlCommand cmd = new SqlCommand(query, connection))  
  734.                 {  
  735.                     try  
  736.                     {  
  737.                         connection.Open();  
  738.                         str = (cmd.ExecuteScalar().ToString() == "") ? "" : cmd.ExecuteScalar().ToString();  
  739.                         return str;  
  740.                     }  
  741.                     catch (System.Data.SqlClient.SqlException E)  
  742.                     {  
  743.                         connection.Close();  
  744.                         throw new Exception(E.Message);  
  745.                     }  
  746.                 }  
  747.             }  
  748.         }  
  749.  
  750.         #endregion  
  751.   
  752.     }  
  753. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值