Dapper-SQLServer 的封装
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Configuration;
5 using System.Data.SqlClient;
6 using System.Data;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using Dapper;
11
12
13 namespace DapperData
14 {
15 public class SQLServerDapperHelper<T> where T : class
16 {
17 /// <summary>
18 /// 数据库连接字符串
19 /// </summary>
20 private static readonly string connectionString =
21 ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
22
23 /// <summary>
24 /// 查询列表
25 /// </summary>
26 /// <param name="sql">查询的sql</param>
27 /// <param name="param">替换参数</param>
28 /// <returns></returns>
29 public static List<T> Query(string sql, object param)
30 {
31 using (SqlConnection con = new SqlConnection(connectionString))
32 {
33 return con.Query<T>(sql, param).ToList();
34 }
35 }
36
37 /// <summary>
38 /// 查询第一个数据
39 /// </summary>
40 /// <param name="sql"></param>
41 /// <param name="param"></param>
42 /// <returns></returns>
43 public static T QueryFirst(string sql, object param)
44 {
45 using (SqlConnection con = new SqlConnection(connectionString))
46 {
47 return con.QueryFirst<T>(sql, param);
48 }
49 }
50
51 /// <summary>
52 /// 查询第一个数据没有返回默认值
53 /// </summary>
54 /// <param name="sql"></param>
55 /// <param name="param"></param>
56 /// <returns></returns>
57 public static T QueryFirstOrDefault(string sql, object param)
58 {
59 using (SqlConnection con = new SqlConnection(connectionString))
60 {
61 return con.QueryFirstOrDefault<T>(sql, param);
62 }
63 }
64
65 /// <summary>
66 /// 查询单条数据
67 /// </summary>
68 /// <param name="sql"></param>
69 /// <param name="param"></param>
70 /// <returns></returns>
71 public static T QuerySingle(string sql, object param)
72 {
73 using (SqlConnection con = new SqlConnection(connectionString))
74 {
75 return con.QuerySingle<T>(sql, param);
76 }
77 }
78
79 /// <summary>
80 /// 查询单条数据没有返回默认值
81 /// </summary>
82 /// <param name="sql"></param>
83 /// <param name="param"></param>
84 /// <returns></returns>
85 public static T QuerySingleOrDefault(string sql, object param)
86 {
87 using (SqlConnection con = new SqlConnection(connectionString))
88 {
89 return con.QuerySingleOrDefault<T>(sql, param);
90 }
91 }
92
93 /// <summary>
94 /// 增删改
95 /// </summary>
96 /// <param name="sql"></param>
97 /// <param name="param"></param>
98 /// <returns></returns>
99 public static int Execute(string sql, object param)
100 {
101 using (SqlConnection con = new SqlConnection(connectionString))
102 {
103 return con.Execute(sql, param);
104 }
105 }
106
107 /// <summary>
108 /// Reader获取数据
109 /// </summary>
110 /// <param name="sql"></param>
111 /// <param name="param"></param>
112 /// <returns></returns>
113 public static IDataReader ExecuteReader(string sql, object param)
114 {
115 using (SqlConnection con = new SqlConnection(connectionString))
116 {
117 return con.ExecuteReader(sql, param);
118 }
119 }
120
121 /// <summary>
122 /// Scalar获取数据
123 /// </summary>
124 /// <param name="sql"></param>
125 /// <param name="param"></param>
126 /// <returns></returns>
127 public static object ExecuteScalar(string sql, object param)
128 {
129 using (SqlConnection con = new SqlConnection(connectionString))
130 {
131 return con.ExecuteScalar(sql, param);
132 }
133 }
134
135 /// <summary>
136 /// Scalar获取数据
137 /// </summary>
138 /// <param name="sql"></param>
139 /// <param name="param"></param>
140 /// <returns></returns>
141 public static T ExecuteScalarForT(string sql, object param)
142 {
143 using (SqlConnection con = new SqlConnection(connectionString))
144 {
145 return con.ExecuteScalar<T>(sql, param);
146 }
147 }
148
149 /// <summary>
150 /// 带参数的存储过程
151 /// </summary>
152 /// <param name="sql"></param>
153 /// <param name="param"></param>
154 /// <returns></returns>
155 public static List<T> ExecutePro(string proc, object param)
156 {
157 using (SqlConnection con = new SqlConnection(connectionString))
158 {
159 List<T> list = con.Query<T>(proc,
160 param,
161 null,
162 true,
163 null,
164 CommandType.StoredProcedure).ToList();
165 return list;
166 }
167 }
168
169
170 /// <summary>
171 /// 事务1 - 全SQL
172 /// </summary>
173 /// <param name="sqlarr">多条SQL</param>
174 /// <param name="param">param</param>
175 /// <returns></returns>
176 public static int ExecuteTransaction(string[] sqlarr)
177 {
178 using (SqlConnection con = new SqlConnection(connectionString))
179 {
180 using (var transaction = con.BeginTransaction())
181 {
182 try
183 {
184 int result = 0;
185 foreach (var sql in sqlarr)
186 {
187 result += con.Execute(sql, null, transaction);
188 }
189
190 transaction.Commit();
191 return result;
192 }
193 catch (Exception ex)
194 {
195 transaction.Rollback();
196 return 0;
197 }
198 }
199 }
200 }
201
202 /// <summary>
203 /// 事务2 - 声明参数
204 ///demo:
205 ///dic.Add("Insert into Users values (@UserName, @Email, @Address)",
206 /// new { UserName = "jack", Email = "380234234@qq.com", Address = "上海" });
207 /// </summary>
208 /// <param name="Key">多条SQL</param>
209 /// <param name="Value">param</param>
210 /// <returns></returns>
211 public static int ExecuteTransaction(Dictionary<string, object> dic)
212 {
213 using (SqlConnection con = new SqlConnection(connectionString))
214 {
215 using (var transaction = con.BeginTransaction())
216 {
217 try
218 {
219 int result = 0;
220 foreach (var sql in dic)
221 {
222 result += con.Execute(sql.Key, sql.Value, transaction);
223 }
224
225 transaction.Commit();
226 return result;
227 }
228 catch (Exception ex)
229 {
230 transaction.Rollback();
231 return 0;
232 }
233 }
234 }
235 }
236 }
237 }
参照此 https://www.cnblogs.com/tangge/p/9972488.html
再牛逼的梦想,也抵不住我傻逼似的坚持!别在该奋斗的年纪,贪图安逸。 今天多学一些知识,明天开发的速度就更快一下。后天你就会变得更好。