I organized a Dapper Helper class by myself
Link string configuration:
<connectionStrings> <add name="db" connectionString="server=.;database=db;uid=sa;pwd=123456;integrated security=false;"/> </connectionStrings>
DapperHelper.cs :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | using Dapper; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PullChargeData.Helper { public class DapperHelper<T> { /// <summary> /// Database connection string /// </summary> private static readonly string connectionString = ConfigurationManager.ConnectionStrings[ "db" ].ConnectionString; /// <summary> /// Query list (return to DataTable) /// </summary> /// <returns></returns> public static DataTable QueryToDataTable( string sql) { DataTable table = new DataTable( "MyTable" ); using (SqlConnection con = new SqlConnection(connectionString)) { var reader = con.ExecuteReader(sql); table.Load(reader); return table; } } /// <summary> /// Query list /// </summary> /// <param name="sql">query sql</param> /// <param name="param">Replace parameters</param> /// <returns></returns> public static List<T> Query( string sql, object param = null ) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList(); } } /// <summary> /// Query the first data /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QueryFirst( string sql, object param = null ) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList().First(); } } /// <summary> /// The first data in the query did not return the default value /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QueryFirstOrDefault( string sql, object param = null ) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList().FirstOrDefault(); } } /// <summary> /// Query a single data /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QuerySingle( string sql, object param = null ) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList().Single(); } } /// <summary> /// Query a single piece of data does not return the default value /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QuerySingleOrDefault( string sql, object param = null ) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList().SingleOrDefault(); } } /// <summary> /// Addition, deletion and modification /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns>Number of rows affected</returns> public static int Execute( string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Execute(sql, param); } } /// <summary> /// Reader gets data /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static IDataReader ExecuteReader( string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.ExecuteReader(sql, param); } } /// <summary> /// Scalar gets data /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static object ExecuteScalar( string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.ExecuteScalar(sql, param); } } /// <summary> /// Scalar gets data /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T ExecuteScalarForT( string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.ExecuteScalar<T>(sql, param); } } /// <summary> /// Stored procedure with parameters /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static List<T> ExecutePro( string proc, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { List<T> list = con.Query<T>(proc, param, null , true , null , CommandType.StoredProcedure).ToList(); return list; } } /// <summary> /// Transaction 1-Full SQL /// </summary> /// <param name="sqlarr">Multiple SQL</param> /// <param name="param">param</param> /// <returns></returns> public static int ExecuteTransaction( string [] sqlarr) { using (SqlConnection con = new SqlConnection(connectionString)) { using ( var transaction = con.BeginTransaction()) { try { int result = 0; foreach ( var sql in sqlarr) { result += con.Execute(sql, null , transaction); } transaction.Commit(); return result; } catch (Exception ex) { transaction.Rollback(); return 0; } } } } /// <summary> /// Transaction 2-declare parameters ///demo: ///dic.Add("Insert into Users values (@UserName, @Email, @Address)", /// new {UserName = "jack", Email = "380234234@qq.com", Address = "Shanghai" }); /// </summary> /// <param name="Key">Multiple SQL</param> /// <param name="Value">param</param> /// <returns></returns> public static int ExecuteTransaction(Dictionary< string , object > dic) { using (SqlConnection con = new SqlConnection(connectionString)) { using ( var transaction = con.BeginTransaction()) { try { int result = 0; foreach ( var sql in dic) { result += con.Execute(sql.Key, sql.Value, transaction); } transaction.Commit(); return result; } catch (Exception ex) { transaction.Rollback(); return 0; } } } } } } |
Call method
//No parameters var list = DapperHelper<T_User>.Query("select * from T_User ").ToList(); //Check with parameters var list = DapperHelper<T_User>.Query("select * from T_User where uid=@uid", new { uid = 1, }).ToList(); //increase int ins = DapperHelper<T_User>.Execute("insert into T_User (uid,username) value(@uid,@username)", new { uid = 1, username = "Zhang San" }); //change int upd = DapperHelper<T_User>.Execute("update T_User set username=@username where uid=@uid", new { username = "Li Si", uid = 1}); //delete int del = DapperHelper<T_User>.Execute("delete from T_User where uid=@uid", new { uid = 1 });
참조 : https://programmerall.com/article/7102414933/