This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20445|Reply: 1

[Source] [DBHelper] Ultimate Optimized Code

[Copy link]
Posted on 5/6/2015 12:03:28 PM | | |
web.config configuration file
  1. <connectionStrings>
  2.     <add name="ApplicationServices"
  3.          connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
  4.          providerName="System.Data.SqlClient" />
  5.           <add name="SQLConn" connectionString="server=.;database=shop_db;integrated security=true;"/>
  6.   </connectionStrings>
Copy code


HBHelper Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. using System.Configuration;

  9. namespace shopDAL
  10. {
  11.     public class DBHelper
  12.     {
  13.         //数据库连接对象
  14.         private static SqlConnection connection;
  15.         public static SqlConnection Connection
  16.         {
  17.             get
  18.             {
  19.                 //在配置web.config文件中获取sqlserver服务器ip账号和密码
  20.                 string connectionString = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
  21.                 if (connection == null)
  22.                 {
  23.                     connection = new SqlConnection(connectionString);
  24.                     connection.Open();
  25.                 }
  26.                 else if (connection.State == System.Data.ConnectionState.Closed)
  27.                 {
  28.                     connection.Open();
  29.                 }
  30.                 else if (connection.State == System.Data.ConnectionState.Broken)
  31.                 {
  32.                     connection.Close();
  33.                     connection.Open();
  34.                 }
  35.                 return connection;
  36.             }
  37.         }

  38.         public static int ExecuteCommand(string safeSql)
  39.         {
  40.             SqlCommand cmd = new SqlCommand(safeSql, Connection);
  41.             int result = cmd.ExecuteNonQuery();
  42.             return result;
  43.         }

  44.         public static int ExecuteCommand(string sql, params SqlParameter[] values)
  45.         {
  46.             SqlCommand cmd = new SqlCommand(sql, Connection);
  47.             cmd.Parameters.AddRange(values);
  48.             return cmd.ExecuteNonQuery();
  49.         }

  50.         public static int GetScalar(string safeSql)
  51.         {
  52.             SqlCommand cmd = new SqlCommand(safeSql, Connection);
  53.             int result = Convert.ToInt32(cmd.ExecuteNonQuery());
  54.             return result;
  55.         }

  56.         public static int GetScalar(string sql, params SqlParameter[] values)
  57.         {
  58.             SqlCommand cmd = new SqlCommand(sql, Connection);
  59.             cmd.Parameters.AddRange(values);
  60.             int result = Convert.ToInt32(cmd.ExecuteNonQuery());
  61.             return result;
  62.         }

  63.         public static SqlDataReader GetReader(string safeSql)
  64.         {
  65.             SqlCommand cmd = new SqlCommand(safeSql, Connection);
  66.             SqlDataReader reader = cmd.ExecuteReader();
  67.             return reader;
  68.         }

  69.         public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
  70.         {
  71.             SqlCommand cmd = new SqlCommand(sql, Connection);
  72.             cmd.Parameters.AddRange(values);
  73.             SqlDataReader reader = cmd.ExecuteReader();
  74.             return reader;
  75.         }

  76.         //private static string strConn = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
  77.         /// <summary>
  78.         /// 执行sql语句
  79.         /// </summary>
  80.         /// <param name="sql">放入sql语句</param>
  81.         public static void setsql(string sql)
  82.         {
  83.             SqlCommand sqlComm = new SqlCommand(sql, Connection);
  84.             sqlComm.ExecuteNonQuery();
  85.         }
  86.         /// <summary>
  87.         /// 查询数据,返回table类型
  88.         /// </summary>
  89.         /// <param name="sql">放入sql语句</param>
  90.         /// <returns></returns>
  91.         public static DataTable gettable(string sql)
  92.         {
  93.             DataTable dt = new DataTable();
  94.             SqlCommand cmd = new SqlCommand(sql, Connection);
  95.             SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
  96.             sqlDA.Fill(dt);
  97.             return dt;
  98.         }
  99.         /// <summary>
  100.         /// 利用反射来返回数据
  101.         /// </summary>
  102.         /// <typeparam name="T">泛型集合</typeparam>
  103.         /// <param name="strSql">放入sql语句</param>
  104.         /// <returns></returns>
  105.         public static List<T> getList<T>(string strSql)
  106.         {
  107.             //委托的实例化,指向packBean方法  
  108.             //PackageBean packageBean = new PackageBean(packBean);
  109.             List<T> list = new List<T>();
  110.             SqlCommand sqlComm = new SqlCommand(strSql, Connection);
  111.             SqlDataReader sqlDR = sqlComm.ExecuteReader();
  112.             while (sqlDR.Read())
  113.             {
  114.                 //得到T的类型
  115.                 Type t = typeof(T); ;

  116.                 //查看类中的属性:
  117.                 PropertyInfo[] pis = t.GetProperties();
  118.                 // 用反射生成对象
  119.                 T model = Activator.CreateInstance<T>();
  120.                 foreach (PropertyInfo pi in pis)
  121.                 {
  122.                     if (pi != null)
  123.                     {
  124.                         //取得特定字段并赋值
  125.                         pi.SetValue(model, sqlDR[pi.Name].ToString(), null);
  126.                     }
  127.                 }
  128.                 list.Add(model);
  129.             }
  130.             return list;
  131.         }
  132.     }
  133. }
Copy code






Previous:asp.net simple three-layer architecture (purchase, sales and inventory management system) project source code
Next:asp.net_linq language integration query example
Posted on 5/6/2015 9:13:18 PM |
A lot
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com