SqlServer DbHelper

 

 

  1     /// 
  2     /// 数据库通用操作类
  3     /// 
  4     public abstract class DbHelper
  5     {
  6         #region " 连接字符串 "
  7         //连接字符串
  8         //public static string ConnString = ConfigurationManager.ConnectionStrings["CommonSqlConnectionString"].ConnectionString;
  9         public static string DefaultConnString = string.Empty;
 10         public const int defaultCommandTimeout = 180;
 11         public const int defaultdbconfig = 1;
 12 
 13         #endregion
 14 
 15         #region " GetSqlCommand "
 16 
 17         /// 
 18         /// 获取初始化好的Command对象
 19         /// 
 20         /// Connection对象
 21         /// 命令文本
 22         /// 命令类型
 23         /// 参数列表
 24         /// 初始化好的Command对象
 25         private static SqlCommand GetSqlCommand(SqlConnection conn, string cmdText, CommandType cmdType, SqlParameter[] parameters)
 26         {
 27             SqlCommand cmd = new SqlCommand(cmdText, conn);
 28             cmd.CommandType = cmdType;
 29             cmd.CommandTimeout = defaultCommandTimeout;
 30             if (parameters != null)
 31             {
 32                 cmd.Parameters.AddRange(parameters);
 33             }
 34 
 35             return cmd;
 36         }
 37 
 38         #endregion
 39 
 40         #region " ExecSqlDataSet "
 41 
 42         public static DataTable ExecSqlDataSet(string strSQL)
 43         {
 44             return ExecSqlDataSet(strSQL, null);
 45         }
 46 
 47         public static DataTable ExecSqlDataSet(string strSQL, SqlParameter[] parameters)
 48         {
 49             return ExecSqlDataSet(strSQL, parameters, DefaultConnString);
 50         }
 51 
 52         public static DataTable ExecSqlDataSet(string strSQL, SqlParameter[] parameters, string connStr)
 53         {
 54             if (string.IsNullOrWhiteSpace(connStr))
 55             {
 56                 return new DataTable();
 57             }
 58             using (SqlConnection conn = new SqlConnection(connStr))
 59             {
 60                 conn.Open();
 61                 SqlCommand cmd = GetSqlCommand(conn, strSQL, CommandType.Text, parameters);
 62                 SqlDataAdapter da = new SqlDataAdapter(cmd);
 63                 DataSet ds = new DataSet();
 64                 da.Fill(ds);
 65                 cmd.Dispose();
 66                 da.Dispose();
 67                 return ds.Tables[0];
 68             }
 69         }
 70 
 71 
 72 
 73         #endregion
 74 
 75         #region " ExecSqlNonQuerry "
 76 
 77         /// 
 78         /// 执行非查询SQL语句
 79         /// 
 80         /// 待执行SQL语句
 81         /// 受影响的行数
 82         public static int ExecSqlNonQuerry(string strSQL)
 83         {
 84             return ExecSqlNonQuerry(strSQL, null);
 85         }
 86 
 87         /// 
 88         /// 执行非查询的带参数的SQL语句
 89         /// 
 90         /// 待执行SQL语句
 91         /// 受影响的行数
 92         public static int ExecSqlNonQuerry(string strSQL, SqlParameter[] parameters)
 93         {
 94             return ExecSqlNonQuerry(strSQL, parameters, DefaultConnString);
 95         }
 96 
 97 
 98 
 99         public static int ExecSqlNonQuerry(string strSQL, SqlParameter[] parameters, string connStr)
100         {
101             using (SqlConnection conn = new SqlConnection(connStr))
102             {
103                 conn.Open();
104                 SqlCommand cmd = GetSqlCommand(conn, strSQL, CommandType.Text, parameters);
105                 cmd.CommandTimeout = 0;
106                 int result = cmd.ExecuteNonQuery();
107                 cmd.Dispose();
108                 return result;
109             }
110         }
111 
112         #endregion
113 
114         #region " ExecSqlScalar "
115 
116         /// 
117         /// 执行统计查询
118         /// 
119         /// 待执行SQL语句
120         /// 执行结果的第1行第1列的值
121         public static object ExecSqlScalar(string strSQL)
122         {
123             return ExecSqlScalar(strSQL, null);
124         }
125 
126         /// 
127         /// 执行带参数的统计查询
128         /// 
129         /// 待执行SQL语句
130         /// 参数数组
131         /// 执行结果的第1行第1列的值
132         public static object ExecSqlScalar(string strSQL, SqlParameter[] parameters)
133         {
134             return ExecSqlScalar(strSQL, parameters, DefaultConnString);
135         }
136 
137         /// 
138         /// 执行带参数的统计查询
139         /// 
140         /// 待执行SQL语句
141         /// 参数数组
142         /// 执行结果的第1行第1列的值
143         public static object ExecSqlScalar(string strSQL, SqlParameter[] parameters, string connStr)
144         {
145             using (SqlConnection conn = new SqlConnection(connStr))
146             {
147                 conn.Open();
148                 SqlCommand cmd = GetSqlCommand(conn, strSQL, CommandType.Text, parameters);
149                 object result = cmd.ExecuteScalar();
150                 cmd.Dispose();
151                 return result;
152             }
153         }
154 
155         #endregion
156 
157         #region " ExecProcDataSet "
158 
159         /// 
160         /// 执行存储过程,返回执行结果
161         /// 
162         /// 待执行存储过程
163         /// 查询结果
164         public static DataSet ExecProcDataSet(string procName)
165         {
166             return ExecProcDataSet(procName, null);
167         }
168         /// 
169         /// 执行带参数的存储过程,返回执行结果
170         /// 
171         /// 待执行存储过程
172         /// 参数数组
173         /// 查询结果
174         public static DataSet ExecProcDataSet(string procName, SqlParameter[] parameters)
175         {
176             return ExecProcDataSet(procName, parameters, DefaultConnString);
177         }
178 
179         /// 
180         /// 执行带参数的存储过程,返回执行结果
181         /// 
182         /// 待执行存储过程
183         /// 参数数组
184         /// 查询结果
185         public static DataSet ExecProcDataSet(string procName, SqlParameter[] parameters, string connStr)
186         {
187             using (SqlConnection conn = new SqlConnection(connStr))
188             {
189                 conn.Open();
190                 SqlCommand cmd = GetSqlCommand(conn, procName, CommandType.StoredProcedure, parameters);
191                 cmd.CommandTimeout = 0;
192                 SqlDataAdapter da = new SqlDataAdapter(cmd);
193                 DataSet ds = new DataSet();
194                 da.Fill(ds);
195                 cmd.Dispose();
196                 return ds;
197             }
198         }
199 
200 
201         #endregion
202 
203         #region " ExecProcDataTable "
204 
205         /// 
206         /// 执行存储过程,返回执行结果
207         /// 
208         /// 待执行存储过程
209         /// 查询结果
210         public static DataTable ExecProcDataTable(string procName)
211         {
212             return ExecProcDataSet(procName).Tables[0];
213         }
214         /// 
215         /// 执行带参数的存储过程,返回执行结果
216         /// 
217         /// 待执行存储过程
218         /// 参数数组
219         /// 查询结果
220         public static DataTable ExecProcDataTable(string procName, SqlParameter[] parameters)
221         {
222             return ExecProcDataSet(procName, parameters).Tables[0];
223         }
224 
225         public static DataTable ExecProcDataTable(string procName, SqlParameter[] parameters, string connStr)
226         {
227             return ExecProcDataSet(procName, parameters, connStr).Tables[0];
228         }
229 
230 
231         #endregion
232 
233         #region " ExecProcNonQuerry "
234 
235         /// 
236         /// 执行非查询存储过程
237         /// 
238         /// 待执行存储过程
239         /// 受影响的行数
240         public static int ExecProcNonQuerry(string procName)
241         {
242             return ExecProcNonQuerry(procName);
243         }
244 
245         /// 
246         /// 执行非查询的带参数的存储过程
247         /// 
248         /// 待执行存储过程
249         /// 受影响的行数
250         public static int ExecProcNonQuerry(string procName, SqlParameter[] parameters)
251         {
252             return ExecProcNonQuerry(procName, parameters, DefaultConnString);
253         }
254 
255 
256         /// 
257         /// 执行非查询的带参数的存储过程
258         /// 
259         /// 待执行存储过程
260         /// 受影响的行数
261         public static int ExecProcNonQuerry(string procName, SqlParameter[] parameters, string connStr)
262         {
263             using (SqlConnection conn = new SqlConnection(connStr))
264             {
265                 conn.Open();
266                 SqlCommand cmd = GetSqlCommand(conn, procName, CommandType.StoredProcedure, parameters);
267                 int result = cmd.ExecuteNonQuery();
268                 cmd.Dispose();
269                 return result;
270             }
271         }
272 
273 
274 
275         #endregion
276 
277         #region " ExecSqlDataReader "
278 
279         /// 
280         /// 执行SQL语句,返回执行结果
281         /// 
282         /// 待执行SQL语句
283         /// 查询结果
284         public static SqlDataReader ExecSqlDataReader(string strSQL)
285         {
286             return ExecSqlDataReader(strSQL, null);
287         }
288 
289         /// 
290         /// 执行带参数的SQL语句,返回执行结果
291         /// 
292         /// 待执行SQL语句
293         /// 参数数组
294         /// 查询结果
295         public static SqlDataReader ExecSqlDataReader(string strSQL, SqlParameter[] parameters)
296         {
297             return ExecSqlDataReader(strSQL, parameters, DefaultConnString);
298         }
299 
300         /// 
301         /// 执行带参数的SQL语句,返回执行结果
302         /// 
303         /// 待执行SQL语句
304         /// 参数数组
305         /// 查询结果
306         public static SqlDataReader ExecSqlDataReader(string strSQL, SqlParameter[] parameters, string connStr)
307         {
308             using (SqlConnection conn = new SqlConnection(connStr))
309             {
310                 conn.Open();
311                 SqlCommand cmd = GetSqlCommand(conn, strSQL, CommandType.Text, parameters);
312                 SqlDataReader result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
313                 cmd.Dispose();
314                 return result;
315             }
316         }
317 
318 
319         #endregion
320 
321         #region " ExecProcDataReader "
322 
323         /// 
324         /// 执行存储过程,返回执行结果
325         /// 
326         /// 待执行存储过程
327         /// 查询结果
328         public static SqlDataReader ExecProcDataReader(string procName)
329         {
330             return ExecProcDataReader(procName, null);
331         }
332 
333         /// 
334         /// 执行带参数的存储过程,返回执行结果
335         /// 
336         /// 待执行存储过程
337         /// 参数数组
338         /// 查询结果
339         public static SqlDataReader ExecProcDataReader(string procName, SqlParameter[] parameters)
340         {
341             return ExecProcDataReader(procName, parameters, DefaultConnString);
342         }
343 
344         /// 
345         /// 执行带参数的存储过程,返回执行结果
346         /// 
347         /// 待执行存储过程
348         /// 参数数组
349         /// 查询结果
350         public static SqlDataReader ExecProcDataReader(string procName, SqlParameter[] parameters, string connStr)
351         {
352             using (SqlConnection conn = new SqlConnection(connStr))
353             {
354                 conn.Open();
355 
356                 SqlCommand cmd = GetSqlCommand(conn, procName, CommandType.StoredProcedure, parameters);
357                 SqlDataReader result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
358                 cmd.Dispose();
359                 return result;
360             }
361         }
362 
363 
364 
365         #endregion
366 
367         #region " DtToSqlServer "
368 
369         /// 
370         /// 将DataTable批量导入SqlServer
371         /// 
372         /// 数据表
373         /// 目标数据表名
374         /// 对应列的数据集
375         public static void DtToSqlServer(DataTable dtExcel, string tableName, DataTable dtColName)
376         {
377             DtToSqlServer(dtExcel, tableName, dtColName, DefaultConnString);
378         }
379 
380         /// 
381         /// 将DataTable批量导入SqlServer
382         /// 
383         /// 数据表
384         /// 目标数据表名
385         /// 对应列的数据集
386         public static void DtToSqlServer(DataTable dtExcel, string tableName, DataTable dtColName, string connStr)
387         {
388             using (SqlConnection conn = new SqlConnection(connStr))
389             {
390                 conn.Open();
391 
392                 using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
393                 {
394                     try
395                     {
396                         bulkCopy.DestinationTableName = tableName;//要插入的表的表名
397                         for (int i = 0; i < dtColName.Rows.Count; i++)
398                         {
399                             bulkCopy.ColumnMappings.Add(dtColName.Rows[i][0].ToString().Trim(), dtColName.Rows[i][1].ToString().Trim());
400 
401                         }
402 
403                         bulkCopy.WriteToServer(dtExcel);
404                     }
405                     catch (Exception ex)
406                     {
407                         Console.WriteLine(ex.Message);
408                     }
409                 }
410             }
411         }
412 
413         #endregion
414 
415         #region
416         /// 
417         /// 
418         /// 
419         /// 目标连接字符
420         /// 目标表
421         /// 源数据
422         public static string SqlBulkCopyByDatatable(string tablename, DataTable table, string connStr, SqlConnection m_clsSqlConn)
423         {
424             string dataBaseStr = "";
425             if (tablename.Contains("."))
426             {
427                 dataBaseStr = tablename.Substring(0, tablename.LastIndexOf(".") + 1);
428                 tablename = tablename.Substring(tablename.LastIndexOf(".") + 1);
429             }
430 
431             try
432             {
433                 string result = "";
434                 SqlBulkCopy sqlBulkCopy = null;
435                 if (m_clsSqlConn != null)
436                 {
437                     sqlBulkCopy = new SqlBulkCopy(m_clsSqlConn);
438                     if (m_clsSqlConn.State == ConnectionState.Closed)
439                     {
440                         m_clsSqlConn.Open();
441                     }
442                 }
443                 else
444                 {
445                     sqlBulkCopy = new SqlBulkCopy(connStr);
446                 }
447 
448                 
449 
450                 sqlBulkCopy.DestinationTableName = dataBaseStr + ((tablename.IndexOf("[") > -1 && tablename.IndexOf("]") > -1) ? tablename : "[" + tablename + "]");
451                 sqlBulkCopy.BulkCopyTimeout = 500;
452                 //sqlBulkCopy.BatchSize = 800;
453 
454                 for (int i = 0; i < table.Columns.Count; i++)
455                 {
456                     sqlBulkCopy.ColumnMappings.Add(table.Columns[i].ColumnName, table.Columns[i].ColumnName);
457                 }
458 
459                 if (table.Rows.Count > 0)
460                 {
461                     sqlBulkCopy.WriteToServer(table);
462                 }
463                 else
464                 {
465                     result = "表为空";
466                 }
467 
468                 sqlBulkCopy.Close();
469                 return result;
470             }
471             catch (Exception e)
472             {
473                 return e.Message;
474             }
475             finally
476             {
477                 try
478                 {
479                     if (m_clsSqlConn != null)
480                     {
481 
482                         try
483                         {
484                             if (m_clsSqlConn.State == ConnectionState.Open)
485                             {
486                                 m_clsSqlConn.Close();
487                             }
488                         }
489                         catch (Exception)
490                         {
491 
492                         }
493                     }
494                 }
495                 catch (Exception)
496                 {
497 
498                 }
499             }
500         }
501 
502         public static string SqlBulkCopyByDatatable(string tablename, DataTable table, SqlConnection m_clsSqlConn)
503         {
504             return SqlBulkCopyByDatatable(tablename, table, string.Empty, m_clsSqlConn);
505 
506         }
507         public static string SqlBulkCopyByDatatable(string tablename, DataTable table, string connStr)
508         {
509             return SqlBulkCopyByDatatable(tablename, table, connStr, null);
510         }
511 
512         public static string SqlBulkCopyByDatatable(string tablename, DataTable table)
513         {
514             return SqlBulkCopyByDatatable(tablename, table, DefaultConnString, null);
515         }
516 
517         public static string CreateTempTable(string tablename, DataTable table, string connStr)
518         {
519             return CreateTempTable(tablename, table, new SqlConnection(connStr));
520         }
521         public static string CreateTempTable(string tablename, DataTable table, SqlConnection connStr)
522         {
523             try
524             {
525 
526                 string sqlstr = "CREATE TABLE [" + tablename + "](";
527                 for (int i = 0; i < table.Columns.Count; i++)
528                 {
529                     switch (table.Columns[i].DataType.FullName)
530                     {
531                         case "System.String":
532                             {
533                                 sqlstr += "[" + table.Columns[i].ColumnName + "] [nvarchar](4000) NULL,";
534                             }
535                             break;
536                         case "System.Int32":
537                             {
538                                 sqlstr += "[" + table.Columns[i].ColumnName + "] [int] NULL,";
539                             }
540                             break;
541                         case "System.Double":
542                             {
543                                 sqlstr += "[" + table.Columns[i].ColumnName + "] [numeric](24,2) NULL,";
544                             }
545                             break;
546                         case "System.DateTime":
547                             {
548                                 sqlstr += "[" + table.Columns[i].ColumnName + "] [datetime] NULL,";
549                             }
550                             break;
551                         default:
552                             {
553                                 sqlstr += "[" + table.Columns[i].ColumnName + "] [nvarchar](4000) NULL,";
554                             }
555                             break;
556                     }
557                 }
558                 sqlstr = sqlstr.Substring(0, sqlstr.Length - 1) + ")";
559 
560                 if (connStr.State != ConnectionState.Open)
561                 {
562                     connStr.Open();
563                 }
564 
565                 SqlCommand cmd = GetSqlCommand(connStr, sqlstr, CommandType.Text, null);
566                 int result = cmd.ExecuteNonQuery();
567                 cmd.Dispose();
568                 return "";
569             }
570             catch (Exception e)
571             {
572                 return e.ToString();
573             }
574         }
575 
576         #endregion
577 
578 
579     }
580   

 

你可能感兴趣的:(SqlServer DbHelper)