1. using System;   
  2. using System.Collections;   
  3. using System.Collections.Specialized;   
  4. using System.Runtime.Remoting.Messaging;   
  5. using System.Data;   
  6. using System.Data.SqlClient;   
  7. using System.Configuration;   
  8. namespace LTP.SQLServerDAL   
  9. {   
  10.  /// <SUMMARY>   
  11.  /// ADO.NET数据库操作基础类。   
  12.  /// </SUMMARY>   
  13.  public abstract class DbManagerSQL   
  14.  {   
  15.   //数据库连接字符串   
  16.   protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];    
  17.   public DbManagerSQL()   
  18.   {   
  19.    //   
  20.    // TODO: 在此处添加构造函数逻辑   
  21.    //   
  22.   }   
  23.   /// <SUMMARY>   
  24.   /// 执行SQL语句,返回影响的记录数   
  25.   /// </SUMMARY>   
  26.   /// <PARAM name="SQLString"></PARAM>   
  27.   /// <RETURNS></RETURNS>   
  28.   public static int ExecuteSql(string SQLString)   
  29.   {   
  30.    using (SqlConnection connection = new SqlConnection(connectionString))   
  31.    {       
  32.     using (SqlCommand cmd = new SqlCommand(SQLString,connection))   
  33.     {   
  34.      try  
  35.      {     
  36.       connection.Open();   
  37.       int rows=cmd.ExecuteNonQuery();   
  38.       return rows;   
  39.      }   
  40.      catch(System.Data.SqlClient.SqlException E)   
  41.      {       
  42.       throw new Exception(E.Message);   
  43.      }   
  44.     }       
  45.    }   
  46.   }   
  47.   /// <SUMMARY>   
  48.   /// 执行两条SQL语句,实现数据库事务。   
  49.   /// </SUMMARY>   
  50.   /// <PARAM name="SQLString1"></PARAM>   
  51.   /// <PARAM name="SQLString2"></PARAM>   
  52.   public static void ExecuteSqlTran(string SQLString1,string SQLString2)   
  53.   {   
  54.    using (SqlConnection connection = new SqlConnection(connectionString))   
  55.    {   
  56.     connection.Open();   
  57.     SqlCommand cmd = new SqlCommand();   
  58.     cmd.Connection=connection;       
  59.     SqlTransaction tx=connection.BeginTransaction();      
  60.     cmd.Transaction=tx;       
  61.     try  
  62.     {        
  63.      cmd.CommandText=SQLString1;   
  64.      cmd.ExecuteNonQuery();   
  65.      cmd.CommandText=SQLString2;   
  66.      cmd.ExecuteNonQuery();        
  67.      tx.Commit();        
  68.     }   
  69.     catch(System.Data.SqlClient.SqlException E)   
  70.     {     
  71.      tx.Rollback();   
  72.      throw new Exception(E.Message);   
  73.     }   
  74.     finally  
  75.     {   
  76.      cmd.Dispose();   
  77.      connection.Close();   
  78.     }    
  79.    }   
  80.   }    
  81.   /// <SUMMARY>   
  82.   /// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。   
  83.   /// </SUMMARY>   
  84.   /// <PARAM name="SQLStringList"></PARAM>   
  85.   public static void ExecuteSqlTran(string SQLStringList)   
  86.   {   
  87.    using (OdbcConnection conn = new OdbcConnection(connectionString))   
  88.    {   
  89.     conn.Open();   
  90.     OdbcCommand cmd = new OdbcCommand();   
  91.     cmd.Connection=conn;       
  92.     OdbcTransaction tx=conn.BeginTransaction();      
  93.     cmd.Transaction=tx;       
  94.     try  
  95.     {      
  96.      string [] split= SQLStringList.Split(new Char [] { &apos;;&apos;});   
  97.      foreach (string strsql in split)    
  98.      {   
  99.       if (strsql.Trim()!="")   
  100.       {   
  101.        cmd.CommandText=strsql;   
  102.        cmd.ExecuteNonQuery();   
  103.       }   
  104.      }   
  105. tx.Commit();        
  106.     }   
  107.     catch(System.Data.Odbc.OdbcException E)   
  108.     {     
  109.      tx.Rollback();   
  110.      throw new Exception(E.Message);   
  111.     }   
  112.    }   
  113.   }   
  114.   /// <SUMMARY>   
  115.   /// 执行带一个存储过程参数的的SQL语句。   
  116.   /// </SUMMARY>   
  117.   /// <PARAM name="SQLString"></PARAM>   
  118.   /// <PARAM name="content"></PARAM>   
  119.   /// <RETURNS></RETURNS>   
  120.   public static int ExecuteSql(string SQLString,string content)   
  121.   {       
  122.    using (SqlConnection connection = new SqlConnection(connectionString))   
  123.    {   
  124.     SqlCommand cmd = new SqlCommand(SQLString,connection);     
  125.     System.Data.SqlClient.SqlParameter  myParameter = new System.Data.SqlClient.SqlParameter ( "@content", SqlDbType.NText);   
  126.     myParameter.Value = content ;   
  127.     cmd.Parameters.Add(myParameter);   
  128.     try  
  129.     {   
  130.      connection.Open();   
  131.      int rows=cmd.ExecuteNonQuery();   
  132.      return rows;   
  133.     }   
  134.     catch(System.Data.SqlClient.SqlException E)   
  135.     {       
  136.      throw new Exception(E.Message);   
  137.     }   
  138.     finally  
  139.     {   
  140.      cmd.Dispose();   
  141.      connection.Close();   
  142.     }    
  143.    }   
  144.   }     
  145.   /// <SUMMARY>   
  146.   /// 向数据库里插入图像格式的字段   
  147.   /// </SUMMARY>   
  148.   /// <PARAM name="strSQL"></PARAM>   
  149.   /// <PARAM name="fs"></PARAM>   
  150.   /// <RETURNS></RETURNS>   
  151.   public static int ExecuteSqlInsertImg(string strSQL,byte[] fs)   
  152.   {     
  153.    using (SqlConnection connection = new SqlConnection(connectionString))   
  154.    {   
  155.     SqlCommand cmd = new SqlCommand(strSQL,connection);    
  156.     System.Data.SqlClient.SqlParameter  myParameter = new System.Data.SqlClient.SqlParameter ( "@fs", SqlDbType.Image);   
  157.     myParameter.Value = fs ;   
  158.     cmd.Parameters.Add(myParameter);   
  159.     try  
  160.     {   
  161.      connection.Open();   
  162.      int rows=cmd.ExecuteNonQuery();   
  163.      return rows;   
  164.     }   
  165.     catch(System.Data.SqlClient.SqlException E)   
  166.     {       
  167.      throw new Exception(E.Message);   
  168.     }   
  169.     finally  
  170.     {   
  171.      cmd.Dispose();   
  172.      connection.Close();   
  173.     }    
  174.        
  175.    }   
  176.   }   
  177.   /// <SUMMARY>   
  178.   /// 执行一条计算查询结果语句,返回查询结果(整数)。   
  179.   /// </SUMMARY>   
  180.   /// <PARAM name="strSQL"></PARAM>   
  181.   /// <RETURNS></RETURNS>   
  182.   public static int GetCount(string strSQL)   
  183.   {   
  184.    using (SqlConnection connection = new SqlConnection(connectionString))   
  185.    {   
  186.     SqlCommand cmd = new SqlCommand(strSQL,connection);       
  187.     try  
  188.     {   
  189.      connection.Open();   
  190.      SqlDataReader result = cmd.ExecuteReader();   
  191.      int i=0;   
  192.      while(result.Read())   
  193.      {   
  194.       i=result.GetInt32(0);   
  195.      }   
  196.      result.Close();       
  197.      return i;   
  198.     }   
  199.     catch(System.Data.SqlClient.SqlException e)   
  200.     {           
  201.      throw new Exception(e.Message);   
  202.     }    
  203.     finally  
  204.     {   
  205. cmd.Dispose();   
  206.      connection.Close();   
  207.     }   
  208.    }   
  209.   }    
  210.   /// <SUMMARY>   
  211.   /// 执行一条计算查询结果语句,返回查询结果(object)。   
  212.   /// </SUMMARY>   
  213.   /// <PARAM name="SQLString"></PARAM>   
  214.   /// <RETURNS></RETURNS>   
  215.   public static object GetSingle(string SQLString)   
  216.   {   
  217.    using (SqlConnection connection = new SqlConnection(connectionString))   
  218.    {   
  219.     SqlCommand cmd = new SqlCommand(SQLString,connection);     
  220.     try  
  221.     {   
  222.      connection.Open();   
  223.      object obj = cmd.ExecuteScalar();   
  224.      if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))   
  225.      {        
  226.       return null;   
  227.      }   
  228.      else  
  229.      {   
  230.       return obj;   
  231.      }       
  232.     }   
  233.     catch(System.Data.SqlClient.SqlException e)   
  234.     {       
  235.      throw new Exception(e.Message);   
  236.     }   
  237.     finally  
  238.     {   
  239.      cmd.Dispose();   
  240.      connection.Close();   
  241.     }   
  242.    }   
  243.   }   
  244.   /// <SUMMARY>   
  245.   /// 执行查询语句,返回SqlDataReader   
  246.   /// </SUMMARY>   
  247.   /// <PARAM name="strSQL"></PARAM>   
  248.   /// <RETURNS></RETURNS>   
  249.   public static SqlDataReader ExecuteReader(string strSQL)   
  250.   {   
  251.    using (SqlConnection connection = new SqlConnection(connectionString))   
  252.    {   
  253.     SqlCommand cmd = new SqlCommand(strSQL,connection);    
  254.     SqlDataReader myReader;      
  255.     try  
  256.     {   
  257.      connection.Open();    
  258.      myReader = cmd.ExecuteReader();   
  259.      return myReader;   
  260.     }   
  261.     catch(System.Data.SqlClient.SqlException e)   
  262.     {           
  263.      throw new Exception(e.Message);   
  264.     }     
  265.     finally  
  266.     {   
  267.      cmd.Dispose();   
  268.      connection.Close();   
  269.     }    
  270.    }   
  271.   }     
  272.   /// <SUMMARY>   
  273.   /// 执行查询语句,返回DataSet   
  274.   /// </SUMMARY>   
  275.   /// <PARAM name="SQLString"></PARAM>   
  276.   /// <RETURNS></RETURNS>   
  277.   public static DataSet Query(string SQLString)   
  278.   {   
  279.    using (SqlConnection connection = new SqlConnection(connectionString))   
  280.    {   
  281.     DataSet ds = new DataSet();   
  282.     try  
  283.     {   
  284.      connection.Open();   
  285.      SqlDataAdapter command = new SqlDataAdapter(SQLString,connection);       
  286.      command.Fill(ds,"ds");   
  287.     }   
  288.     catch(System.Data.SqlClient.SqlException ex)   
  289.     {       
  290.      throw new Exception(ex.Message);   
  291.     }      
  292.     return ds;   
  293.    }   
  294.       
  295.   }  
  296.  
  297.  
  298.   #region 存储过程操作   
  299.   
  300.   /// <SUMMARY>   
  301.   /// 运行存储过程   
  302.   /// </SUMMARY>   
  303.   /// <PARAM name="storedProcName"></PARAM>   
  304.   /// <PARAM name="parameters"></PARAM>   
  305.   /// <RETURNS></RETURNS>   
  306.   public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )   
  307.   {   
  308.    using (SqlConnection connection = new SqlConnection(connectionString))   
  309.    {   
  310.     SqlDataReader returnReader;   
  311.     connection.Open();   
  312.     SqlCommand command = BuildQueryCom   
  313.   
  314. mand( connection,storedProcName, parameters );   
  315.     command.CommandType = CommandType.StoredProcedure;   
  316.   
  317.     returnReader = command.ExecuteReader();   
  318.     //Connection.Close();   
  319.     return returnReader;   
  320.    }   
  321.   }   
  322.   private static SqlCommand BuildQueryCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)   
  323.   {   
  324.       
  325.     SqlCommand command = new SqlCommand( storedProcName, connection );   
  326.     command.CommandType = CommandType.StoredProcedure;   
  327.     foreach (SqlParameter parameter in parameters)   
  328.     {   
  329.      command.Parameters.Add( parameter );   
  330.     }   
  331.     return command;   
  332.       
  333.   }     
  334.   public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )   
  335.   {   
  336.    using (SqlConnection connection = new SqlConnection(connectionString))   
  337.    {   
  338.     DataSet dataSet = new DataSet();   
  339.     connection.Open();   
  340.     SqlDataAdapter sqlDA = new SqlDataAdapter();   
  341.     sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters );   
  342.     sqlDA.Fill( dataSet, tableName );   
  343.     connection.Close();   
  344.   
  345.     return dataSet;   
  346.    }   
  347.   }  
  348.  
  349.   #endregion    
  350.  }   
  351. }  
posted on 2008-12-05 15:38  cean  阅读(241)  评论(0编辑  收藏  举报