码迷,mamicode.com
首页 > 数据库 > 详细

复习SqlHelper

时间:2018-02-06 23:12:20      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:ble   turn   log   技术分享   closed   reader   serve   utf8   public   

配置信息:在App.config或Web.config的Configuration节点下添加如下代码

 

1  <connectionStrings>
2     <add name="connStr" connectionString="server=.;uid=sa;pwd=****;database=****;Charset=utf8"/>
3   </connectionStrings>

 

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Data.SqlClient;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 
10 namespace ConsoleApplication1
11 {
12     class SqlHelper
13     {
14         //ADO.net中的SqlHelper
15 
16  public  static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
17 
18         public static SqlConnection CreateConnection()
19         {
20             SqlConnection conn = new SqlConnection(connStr);
21             conn.Open();
22             return conn;
23         }
24 
25         public static int ExecuteNonQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)
26         {
27             using (SqlCommand cmd = conn.CreateCommand())
28             {
29                 cmd.CommandText = sql;
30                 cmd.Parameters.AddRange(parameters);
31                 return cmd.ExecuteNonQuery();
32             }
33         }
34 
35         public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
36         {
37             using (SqlConnection conn = CreateConnection())
38             {
39                 return ExecuteNonQuery(conn, sql, parameters);
40             }
41         }
42 
43         public static object ExecuteScalar(SqlConnection conn, string sql, params SqlParameter[] parameters)
44         {
45             using (SqlCommand cmd = conn.CreateCommand())
46             {
47                 cmd.CommandText = sql;
48                 cmd.Parameters.AddRange(parameters);
49                 return cmd.ExecuteScalar();
50             }
51         }
52 
53         public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
54         {
55             using (SqlConnection conn = CreateConnection())
56             {
57                 return ExecuteScalar(conn, sql, parameters);
58             }
59         }
60 
61         public static DataTable ExecuteQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)
62         {
63             DataTable table = new DataTable();
64             using (SqlCommand cmd = conn.CreateCommand())
65             {
66                 cmd.CommandText = sql;
67                 cmd.Parameters.AddRange(parameters);
68                 using (SqlDataReader reader = cmd.ExecuteReader())
69                 {
70                     table.Load(reader);
71                     return table;
72                 }
73             }
74 
75         }
76 
77         public static DataTable ExecuteQuery(string sql, params SqlParameter[] parameters)
78         {
79             using (SqlConnection conn = CreateConnection())
80             {
81                 return ExecuteQuery(conn, sql, parameters);
82             }
83         }
84     }
85 }
SqlHelper

 

复习SqlHelper

标签:ble   turn   log   技术分享   closed   reader   serve   utf8   public   

原文地址:https://www.cnblogs.com/HuShaoyi/p/8424308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!