复习SqlHelper

Posted 西科之巅、不可能

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复习SqlHelper相关的知识,希望对你有一定的参考价值。

配置信息:在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的主要内容,如果未能解决你的问题,请参考以下文章

JDBC代码库-sqlhelper

SqlHelper——仅仅由于在人群中多看了你一眼

C#实现较为实用的SQLhelper

SQLHelper助手

2016-11-2坚持学习Day17微软 推出的SQLHelper

单步执行SqlHelper.ExecuteNonQuery();弹出"没有可用于当前位置的源代码",请问是啥原因啊