三层结构视频中的DBHelper.cs

 

成都创新互联专注于汇川网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供汇川营销型网站建设,汇川网站制作、汇川网页设计、汇川网站官网定制、成都小程序开发服务,打造汇川网络公司原创品牌,更为您提供汇川网站排名全网营销落地服务。

 lyqConnectionString  要换

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using System.Configuration; 
  5. using System.Data.Common; 
  6. using System.Data; 
  7. using System.Data.SqlClient; 
  8.  
  9.  
  10. namespace LyqDAL 
  11. { 
  12.     ///  
  13.     /// 自定义访问通用类 
  14.     ///  
  15.     public class DBHelper 
  16.     { 
  17.         private static readonly string connectionString = ConfigurationManager.ConnectionStrings["lyqConnectionString"].ConnectionString.ToString(); 
  18.         private static readonly string providerName = "System.Data.SqlClient"; 
  19.         private static SqlConnection connection = new SqlConnection(connectionString); 
  20.         ///  
  21.         /// GetConnection 用于获取连接数据库的 connection 对象 
  22.         ///  
  23.         ///  
  24.         private static DbConnection GetConnection() 
  25.         { 
  26.             DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName); 
  27.             DbConnection connection = _factory.CreateConnection(); 
  28.             connection.ConnectionString = connectionString; 
  29.             return connection; 
  30.         } 
  31.         ///  
  32.         /// GetCommand 获取命令参数 command 对象 
  33.         ///  
  34.         ///  
  35.         ///  
  36.         ///  
  37.         ///  
  38.         private static DbCommand GetCommand(string commandText, CommandType commandType, DbConnection connection) 
  39.         { 
  40.             DbCommand command = connection.CreateCommand(); 
  41.             command.CommandText = commandText; 
  42.             command.CommandType = commandType; 
  43.             return command; 
  44.  
  45.  
  46.         } 
  47.         ///  
  48.         /// 读取数据 
  49.         ///  
  50.         /// sql语句 
  51.         /// 参数列表 
  52.         /// DataTable 
  53.         public static DataTable GetDataTable(string sql, params SqlParameter[] param) 
  54.         { 
  55.             DataTable dt = new DataTable(); 
  56.             using (SqlConnection conn = new SqlConnection(connectionString)) 
  57.             { 
  58.                 SqlDataAdapter da = new SqlDataAdapter(sql, conn); 
  59.                 if (param != null) 
  60.                     da.SelectCommand.Parameters.AddRange(param); 
  61.                 da.Fill(dt); 
  62.             } 
  63.             return dt; 
  64.         } 
  65.         ///  
  66.         /// GetCommand 方法重载 
  67.         ///  
  68.         /// sql语句 
  69.         ///  
  70.         ///  
  71.         private static DbCommand GetCommand(string commandText, DbConnection connection) 
  72.         { 
  73.             DbCommand command = connection.CreateCommand(); 
  74.             command.CommandText = commandText; 
  75.             command.CommandType = CommandType.Text; 
  76.             return command; 
  77.         } 
  78.         public DbCommand GetSqlStringCommond(string sqlQuery) 
  79.         { 
  80.             DbCommand dbCommand = connection.CreateCommand(); 
  81.             dbCommand.CommandText = sqlQuery; 
  82.             dbCommand.CommandType = CommandType.Text; 
  83.             return dbCommand; 
  84.         } 
  85.         ///  
  86.         /// GetParameter 用于为命令设置参数 
  87.         ///  
  88.         ///  
  89.         ///  
  90.         ///  
  91.         ///  
  92.         private static DbParameter GetParameter(string paramName, object paramValue, DbCommand command) 
  93.         { 
  94.             DbParameter parameter = command.CreateParameter(); 
  95.             parameter.ParameterName = paramName; 
  96.             parameter.Value = paramValue; 
  97.             return parameter; 
  98.         } 
  99.         ///  
  100.         /// 执行无参的存储过程 
  101.         ///  
  102.         /// 存储过程名 
  103.         ///  
  104.         public static int ExecuteNonQueryProc(string sqlCommand) 
  105.         { 
  106.             int result = 0; 
  107.             using (DbConnection connection = GetConnection()) 
  108.             { 
  109.                 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 
  110.                 connection.Open(); 
  111.                 result = command.ExecuteNonQuery(); 
  112.                 command.Parameters.Clear(); 
  113.             } 
  114.             return result; 
  115.         } 
  116.         ///  
  117.         /// 执行无返回值有参数的存储过程 
  118.         ///  
  119.         /// 存储过程名 
  120.         /// 参数 
  121.         ///  
  122.         public static int ExecuteNonQueryProc(string sqlCommand, Dictionary parameters) 
  123.         { 
  124.             int result = 0; 
  125.             using (DbConnection connection = GetConnection()) 
  126.             { 
  127.                 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 
  128.                 foreach (KeyValuePair p in parameters) 
  129.                 { 
  130.                     command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 
  131.                 } 
  132.                 connection.Open(); 
  133.                 result = command.ExecuteNonQuery(); 
  134.                 command.Parameters.Clear(); 
  135.             } 
  136.             return result; 
  137.         } 
  138.  
  139.         ///  
  140.         /// 执行无返回值的sql语句 
  141.         ///  
  142.         ///  
  143.         ///  
  144.         public static int ExecuteNonQuery(string sqlCommand) 
  145.         { 
  146.             int result = 0; 
  147.             using (DbConnection connection = GetConnection()) 
  148.             { 
  149.                 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 
  150.                 connection.Open(); 
  151.                 result = command.ExecuteNonQuery(); 
  152.                 command.Parameters.Clear(); 
  153.                 return result; 
  154.             } 
  155.         } 
  156.         ///  
  157.         /// 执行有参数的sql语句 
  158.         ///  
  159.         ///  
  160.         ///  
  161.         ///  
  162.         public static int ExecuteNonQuery(string sqlCommand, Dictionary para) 
  163.         { 
  164.             int result = 0; 
  165.             using (DbConnection connection = GetConnection()) 
  166.             { 
  167.                 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 
  168.                 foreach (KeyValuePair p in para) 
  169.                 { 
  170.                     command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 
  171.                 } 
  172.                 connection.Open(); 
  173.                 result = command.ExecuteNonQuery(); 
  174.                 command.Parameters.Clear(); 
  175.                 return result; 
  176.             } 
  177.         } 
  178.         ///  
  179.         /// 执行有返回值无参数的存储过程 
  180.         ///  
  181.         ///  
  182.         ///  
  183.         public static object ExecuteScalarProc(string cmdText) 
  184.         { 
  185.             using (DbConnection connection = GetConnection()) 
  186.             { 
  187.                 DbCommand command = GetCommand(cmdText, CommandType.StoredProcedure, connection); 
  188.                 connection.Open(); 
  189.                 object val = command.ExecuteScalar(); 
  190.                 command.Parameters.Clear(); 
  191.                 return val; 
  192.             } 
  193.         } 
  194.         ///  
  195.         /// 执行有返回值的有参数的存储过程 
  196.         ///  
  197.         /// 存储过程名 
  198.         /// 参数 
  199.         ///  
  200.         public static object ExecuteScalarProc(string cmdText, Dictionary para) 
  201.         { 
  202.             using (DbConnection connection = GetConnection()) 
  203.             { 
  204.                 DbCommand command = GetCommand(cmdText, CommandType.StoredProcedure, connection); 
  205.                 foreach (KeyValuePair p in para) 
  206.                 { 
  207.                     command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 
  208.                 } 
  209.                 connection.Open(); 
  210.                 object val = command.ExecuteScalar(); 
  211.                 command.Parameters.Clear(); 
  212.                 return val; 
  213.             } 
  214.         } 
  215.         ///  
  216.         /// 执行有返回值的sql语句 
  217.         ///  
  218.         ///  
  219.         ///  
  220.         public static object ExecuteScalar(string cmdText) 
  221.         { 
  222.             using (DbConnection connection = GetConnection()) 
  223.             { 
  224.                 DbCommand command = GetCommand(cmdText, CommandType.Text, connection); 
  225.                 connection.Open(); 
  226.                 object val = command.ExecuteScalar(); 
  227.                 command.Parameters.Clear(); 
  228.                 return val; 
  229.             } 
  230.         } 
  231.         ///  
  232.         /// 执行有返回值有参数的sql语句 
  233.         ///  
  234.         ///  
  235.         ///  
  236.         ///  
  237.         public static object ExecuteScalar(string cmdText, Dictionary para) 
  238.         { 
  239.             using (DbConnection connection = GetConnection()) 
  240.             { 
  241.                 DbCommand command = GetCommand(cmdText, CommandType.Text, connection); 
  242.                 foreach (KeyValuePair p in para) 
  243.                 { 
  244.                     command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 
  245.                 } 
  246.                 connection.Open(); 
  247.                 object val = command.ExecuteScalar(); 
  248.                 command.Parameters.Clear(); 
  249.                 return val; 
  250.             } 
  251.         } 
  252.         ///  
  253.         /// 执行无参数的存储过程,返回DbDataReader对象 
  254.         ///  
  255.         /// 存储过程名 
  256.         ///  
  257.         public static DbDataReader GetReaderProc(string sqlCommand) 
  258.         { 
  259.             try 
  260.             { 
  261.                 DbConnection connection = GetConnection(); 
  262.                 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 
  263.                 connection.Open(); 
  264.                 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 
  265.                 return reader; 
  266.             } 
  267.             catch (Exception ex) 
  268.             { 
  269.                 Console.Write("" + ex.Message); 
  270.                 return null; 
  271.             } 
  272.         } 
  273.         ///  
  274.         /// 执行有参数的存储过程,返回DbDataReader对象 
  275.         ///  
  276.         ///  
  277.         ///  
  278.         ///  
  279.         public static DbDataReader GetReaderProc(string sqlCommand, Dictionary parameters) 
  280.         { 
  281.             try 
  282.             { 
  283.                 DbConnection connection = GetConnection(); 
  284.                 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 
  285.                 foreach (KeyValuePair p in parameters) 
  286.                 { 
  287.                     command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 
  288.                 } 
  289.                 connection.Open(); 
  290.                 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 
  291.                 command.Parameters.Clear(); 
  292.                 return reader; 
  293.             } 
  294.             catch 
  295.             { 
  296.                 return null; 
  297.             } 
  298.         } 
  299.         #region 
  300.         ///  
  301.         /// 执行无参数的sql语句,返回DbDataReader对象 
  302.         ///  
  303.         ///  
  304.         ///  
  305.         public static DbDataReader GetReader(string sqlCommand) 
  306.         { 
  307.             try 
  308.             { 
  309.                 DbConnection connection = GetConnection(); 
  310.                 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 
  311.                 connection.Open(); 
  312.                 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 
  313.                 command.Parameters.Clear(); 
  314.                 return reader; 
  315.             } 
  316.             catch (Exception ex) 
  317.             { 
  318.                 Console.Write("" + ex.Message); 
  319.                 return null; 
  320.             } 
  321.         } 
  322.         #endregion 
  323.         ///  
  324.         /// 执行有参数的sql语句,返回DbDataReader对象 
  325.         ///  
  326.         ///  
  327.         ///  
  328.         ///  
  329.         public static DbDataReader GetReader(string sqlCommand, Dictionary parameters) 
  330.         { 
  331.             try 
  332.             { 
  333.                 DbConnection connection = GetConnection(); 
  334.                 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 
  335.                 foreach (KeyValuePair p in parameters) 
  336.                 { 
  337.                     command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 
  338.                 } 
  339.                 connection.Open(); 
  340.                 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 
  341.                 command.Parameters.Clear(); 
  342.                 return reader; 
  343.             } 
  344.             catch (Exception ex) 
  345.             { 
  346.                 Console.Write("" + ex.Message); 
  347.                 return null; 
  348.             } 
  349.         } 
  350.          
  351.         ///  
  352.         /// 返回DataTable对象 
  353.         ///  
  354.         ///  
  355.         ///  
  356.         public static DataTable GetDataSet(string safeSql) 
  357.         { 
  358.             /*DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName); 
  359.             DbConnection connection = GetConnection(); 
  360.             connection.Open(); 
  361.             DbDataAdapter da = _factory.CreateDataAdapter(); 
  362.             da.SelectCommand = connection.CreateCommand(); 
  363.             da.SelectCommand.CommandText = safeSql; 
  364.             DataTable dt = new DataTable(); 
  365.             da.Fill(dt); 
  366.             return dt;*/ 
  367.             using (DbConnection connection = GetConnection()) 
  368.             { 
  369.                 DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName); 
  370.                 DbCommand command = GetCommand(safeSql, CommandType.Text, connection); 
  371.                 connection.Open(); 
  372.                 DbDataAdapter da = _factory.CreateDataAdapter(); 
  373.                 da.SelectCommand = command; 
  374.                 DataTable datatable = new DataTable(); 
  375.                 da.Fill(datatable); 
  376.                 return datatable; 
  377.             } 
  378.         } 
  379.     } 
  380. } 

 


新闻标题:三层结构视频中的DBHelper.cs
网页URL:http://jxruijie.cn/article/jdijio.html