纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

695 lines
24 KiB

3 years ago
  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. namespace NFine.Data.Extensions
  10. {
  11. /// <summary>
  12. /// 数据库访问辅助类,add by ngye, on 2013-08-14.
  13. /// </summary>
  14. public static class SqlHelper
  15. {
  16. #region [ 连接串相关 ]
  17. /// <summary>
  18. /// 数据中心DB的连接字符串
  19. /// </summary>
  20. public static string DataCenterConnString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  21. /// <summary>
  22. /// 获取同步服务器的连接
  23. /// </summary>
  24. /// <returns></returns>
  25. public static SqlConnection GetDataCenterConn()
  26. {
  27. return new SqlConnection(DataCenterConnString);
  28. }
  29. /// <summary>
  30. /// 根据连接串获取连接
  31. /// </summary>
  32. /// <returns></returns>
  33. public static SqlConnection GetConnByString(string conn)
  34. {
  35. return new SqlConnection(conn);
  36. }
  37. /// <summary>
  38. /// 测试连接串是否能正确
  39. /// </summary>
  40. /// <param name="connectionString">连接串</param>
  41. /// <returns></returns>
  42. public static bool TestConnectionString(string connectionString)
  43. {
  44. bool result = true;
  45. try
  46. {
  47. using (SqlConnection conn = GetConnByString(connectionString))
  48. {
  49. try
  50. {
  51. conn.Open();
  52. }
  53. catch (Exception ex)
  54. {
  55. result = false;
  56. }
  57. }
  58. }
  59. catch (Exception)
  60. {
  61. result = false;
  62. }
  63. return result;
  64. }
  65. /// <summary>
  66. /// 测试连接串是否正确
  67. /// </summary>
  68. /// <param name="connectionString">连接串</param>
  69. /// <param name="timeOut">测试的超时秒数</param>
  70. /// <param name="errInfo">错误时输出的错误信息</param>
  71. /// <returns>是否能正常连接</returns>
  72. public static bool TestConnectionString(string connectionString, int timeOut, ref string errInfo)
  73. {
  74. bool result = true;
  75. string[] arr = connectionString.Split(new char[] { ';' });
  76. List<string> list = new List<string>();
  77. foreach (string s in arr)
  78. {
  79. if (s.ToLower().IndexOf("timeout") == -1)
  80. {
  81. list.Add(s);
  82. }
  83. else
  84. {
  85. list.Add(String.Format("Connection Timeout={0}", timeOut));
  86. }
  87. }
  88. SqlConnection con = null;
  89. try
  90. {
  91. con = new SqlConnection(string.Join(";", list.ToArray()));
  92. con.Open();
  93. }
  94. catch (Exception ex)
  95. {
  96. result = false;
  97. errInfo = ex.Message;
  98. }
  99. finally
  100. {
  101. if (con != null)
  102. {
  103. con.Close();
  104. con.Dispose();
  105. con = null;
  106. }
  107. }
  108. return result;
  109. }
  110. #endregion
  111. #region [ 超时设置 ]
  112. public static int CommandTimeout
  113. {
  114. get
  115. {
  116. return 7200;
  117. }
  118. }
  119. #endregion
  120. #region [ ExecuteNonQuery ]
  121. /// <summary>
  122. /// 根据sql语句和参数,返回受影响行数
  123. /// </summary>
  124. /// <param name="sql">sql语句</param>
  125. /// <param name="spArr">可变参数</param>
  126. /// <returns>受影响行数</returns>
  127. public static int ExecuteNonQuery(string sql, params SqlParameter[] spArr)
  128. {
  129. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  130. {
  131. conn.Open();
  132. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  133. if (spArr.Length > 0)
  134. cmd.Parameters.AddRange(spArr.SetDBNull());
  135. return cmd.ExecuteNonQuery();
  136. }
  137. }
  138. public static int ExecuteNonQuery(string connStr, string sql, params SqlParameter[] spArr)
  139. {
  140. using (SqlConnection conn = new SqlConnection(connStr))
  141. {
  142. conn.Open();
  143. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  144. if (spArr.Length > 0)
  145. cmd.Parameters.AddRange(spArr.SetDBNull());
  146. return cmd.ExecuteNonQuery();
  147. }
  148. }
  149. #endregion
  150. #region [ ExecuteScalar_ForProc ]
  151. /// <summary>
  152. /// 根据存储过程和参数,返回Scalar结果
  153. /// </summary>
  154. /// <param name="proc">存储过程</param>
  155. /// <param name="spArr">可变参数</param>
  156. /// <returns>object</returns>
  157. public static object ExecuteScalar_ForProc(string proc, params SqlParameter[] spArr)
  158. {
  159. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  160. {
  161. conn.Open();
  162. SqlCommand cmd = new SqlCommand(proc, conn).AddTimeout();
  163. cmd.CommandType = CommandType.StoredProcedure;
  164. if (spArr.Length > 0)
  165. cmd.Parameters.AddRange(spArr.SetDBNull());
  166. object obj = cmd.ExecuteScalar();
  167. return obj;
  168. }
  169. }
  170. /// <summary>
  171. /// 根据存储过程和参数,返回Scalar结果,但不加上超时设置 ( 避免循环做 AddTimeout )
  172. /// </summary>
  173. /// <param name="proc">存储过程</param>
  174. /// <param name="spArr">可变参数</param>
  175. /// <returns>object</returns>
  176. public static object ExecuteScalar_ForProc_WithoutAddTimeout(string proc, params SqlParameter[] spArr)
  177. {
  178. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  179. {
  180. conn.Open();
  181. SqlCommand cmd = new SqlCommand(proc, conn);
  182. cmd.CommandType = CommandType.StoredProcedure;
  183. if (spArr.Length > 0)
  184. cmd.Parameters.AddRange(spArr.SetDBNull());
  185. object obj = cmd.ExecuteScalar();
  186. return obj;
  187. }
  188. }
  189. #endregion
  190. #region [ ExecuteScalar ]
  191. /// <summary>
  192. /// 根据sql语句和参数,返回Scalar结果
  193. /// </summary>
  194. /// <param name="sql">sql语句</param>
  195. /// <param name="spArr">可变参数</param>
  196. /// <returns>object</returns>
  197. public static object ExecuteScalar(string sql, params SqlParameter[] spArr)
  198. {
  199. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  200. {
  201. conn.Open();
  202. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  203. if (spArr.Length > 0)
  204. cmd.Parameters.AddRange(spArr.SetDBNull());
  205. object obj = cmd.ExecuteScalar();
  206. return obj;
  207. }
  208. }
  209. #endregion
  210. #region [ QueryByProc ]
  211. /// <summary>
  212. /// 根据存储过程及参数,返回DataSet
  213. /// </summary>
  214. /// <returns>DataSet</returns>
  215. public static DataSet GetDataSetByProc(string proc, params SqlParameter[] spArr)
  216. {
  217. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  218. {
  219. conn.Open();
  220. SqlCommand cmd = new SqlCommand(proc, conn).AddTimeout();
  221. cmd.CommandType = CommandType.StoredProcedure;
  222. if (spArr.Length > 0)
  223. cmd.Parameters.AddRange(spArr.SetDBNull());
  224. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  225. DataSet ds = new DataSet();
  226. adapter.Fill(ds);
  227. return ds;
  228. }
  229. }
  230. /// <summary>
  231. /// 根据存储过程及参数,返回DataTable
  232. /// </summary>
  233. /// <returns>DataTable</returns>
  234. public static DataTable GetDataTableByProc(string proc, params SqlParameter[] spArr)
  235. {
  236. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  237. {
  238. conn.Open();
  239. SqlCommand cmd = new SqlCommand(proc, conn).AddTimeout();
  240. cmd.CommandType = CommandType.StoredProcedure;
  241. if (spArr.Length > 0)
  242. cmd.Parameters.AddRange(spArr.SetDBNull());
  243. DataTable dt = cmd.ExecuteDataTable();
  244. return dt;
  245. }
  246. }
  247. /// <summary>
  248. /// 根据sql语句和参数,返回DataRow
  249. /// </summary>
  250. /// <param name="sql">sql语句</param>
  251. /// <param name="spArr">可变参数</param>
  252. /// <returns>DataRow</returns>
  253. public static DataRow GetDataRowByProc(string proc, params SqlParameter[] spArr)
  254. {
  255. DataTable dt = GetDataTableByProc(proc, spArr);
  256. if (dt == null || dt.Rows.Count == 0)
  257. return null;
  258. return dt.Rows[0];
  259. }
  260. #endregion
  261. #region [ Query ]
  262. /// <summary>
  263. /// 根据sql语句和参数,返回DataSet
  264. /// </summary>
  265. /// <param name="sql">sql语句</param>
  266. /// <param name="spArr">可变参数</param>
  267. /// <returns>DataSet</returns>
  268. public static DataSet GetDataSetBySql(string sql, params SqlParameter[] spArr)
  269. {
  270. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  271. {
  272. conn.Open();
  273. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  274. if (spArr.Length > 0)
  275. cmd.Parameters.AddRange(spArr.SetDBNull());
  276. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  277. DataSet ds = new DataSet();
  278. adapter.Fill(ds);
  279. return ds;
  280. }
  281. }
  282. /// <summary>
  283. /// 根据sql语句和参数,返回DataTable
  284. /// </summary>
  285. /// <param name="sql">sql语句</param>
  286. /// <param name="spArr">可变参数</param>
  287. /// <returns>DataTable</returns>
  288. public static DataTable GetDataTableBySql(string sql, params SqlParameter[] spArr)
  289. {
  290. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  291. {
  292. conn.Open();
  293. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  294. if (spArr.Length > 0)
  295. cmd.Parameters.AddRange(spArr.SetDBNull());
  296. DataTable dt = cmd.ExecuteDataTable();
  297. return dt;
  298. }
  299. }
  300. /// <summary>
  301. /// 根据sql语句和参数,返回DataRow
  302. /// </summary>
  303. /// <param name="sql">sql语句</param>
  304. /// <param name="spArr">可变参数</param>
  305. /// <returns>DataRow</returns>
  306. public static DataRow GetDataRowBySql(string sql, params SqlParameter[] spArr)
  307. {
  308. DataTable dt = GetDataTableBySql(sql, spArr);
  309. if (dt == null || dt.Rows.Count == 0)
  310. return null;
  311. return dt.Rows[0];
  312. }
  313. #endregion
  314. #region [ GetValue ]
  315. public static object GetValue(DataRow dr, string fieldName, object replaceValue)
  316. {
  317. if (dr.IsNull(fieldName))
  318. return replaceValue;
  319. return dr[fieldName];
  320. }
  321. public static object GetValue(DataRow dr, int idx, object replaceValue)
  322. {
  323. if (dr.IsNull(idx))
  324. return replaceValue;
  325. return dr[idx];
  326. }
  327. #endregion
  328. #region [ GetString ]
  329. public static string GetString(DataRow dr, string fieldName, string replaceValue)
  330. {
  331. if (dr.IsNull(fieldName))
  332. return replaceValue;
  333. return Convert.ToString(dr[fieldName]);
  334. }
  335. public static string GetString(DataRow dr, int idx, string replaceValue)
  336. {
  337. if (dr.IsNull(idx))
  338. return replaceValue;
  339. return Convert.ToString(dr[idx]);
  340. }
  341. #endregion
  342. #region [ GetDateTime ]
  343. public static DateTime GetDateTime(DataRow dr, string fieldName, DateTime replaceValue)
  344. {
  345. if (dr.IsNull(fieldName))
  346. return replaceValue;
  347. return Convert.ToDateTime(dr[fieldName]);
  348. }
  349. public static DateTime GetDateTime(DataRow dr, int idx, DateTime replaceValue)
  350. {
  351. if (dr.IsNull(idx))
  352. return replaceValue;
  353. return Convert.ToDateTime(dr[idx]);
  354. }
  355. #endregion
  356. #region [ 非数据中心库操作 ]
  357. #region [ ExecuteScalar ]
  358. /// <summary>
  359. /// 根据sql语句和参数,返回Scalar结果
  360. /// </summary>
  361. /// <param name="connString">连接串</param>
  362. /// <param name="sql">sql语句</param>
  363. /// <param name="spArr">可变参数</param>
  364. /// <returns>object</returns>
  365. public static object ExecuteScalarWithConn(string connString, string sql, params SqlParameter[] spArr)
  366. {
  367. using (SqlConnection conn = SqlHelper.GetConnByString(connString))
  368. {
  369. conn.Open();
  370. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  371. if (spArr.Length > 0)
  372. cmd.Parameters.AddRange(spArr.SetDBNull());
  373. object obj = cmd.ExecuteScalar();
  374. return obj;
  375. }
  376. }
  377. #endregion
  378. #region [ ExecuteNonQuery ]
  379. /// <summary>
  380. /// 根据sql语句和参数,返回受影响行数
  381. /// </summary>
  382. /// <param name="connString">连接串</param>
  383. /// <param name="sql">sql语句</param>
  384. /// <param name="spArr">可变参数</param>
  385. /// <returns>受影响行数</returns>
  386. public static int ExecuteNonQueryWithConn(string connString, string sql, params SqlParameter[] spArr)
  387. {
  388. using (SqlConnection conn = SqlHelper.GetConnByString(connString))
  389. {
  390. conn.Open();
  391. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  392. if (spArr.Length > 0)
  393. cmd.Parameters.AddRange(spArr.SetDBNull());
  394. return cmd.ExecuteNonQuery();
  395. }
  396. }
  397. #endregion
  398. #region [ Query ]
  399. /// <summary>
  400. /// 根据sql语句和参数,返回DataTable
  401. /// </summary>
  402. /// <param name="connString">连接串</param>
  403. /// <param name="sql">sql语句</param>
  404. /// <param name="spArr">可变参数</param>
  405. /// <returns>DataTable</returns>
  406. public static DataTable GetDataTableBySqlWithConn(string connString, string sql, params SqlParameter[] spArr)
  407. {
  408. using (SqlConnection conn = SqlHelper.GetConnByString(connString))
  409. {
  410. conn.Open();
  411. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  412. if (spArr.Length > 0)
  413. cmd.Parameters.AddRange(spArr.SetDBNull());
  414. DataTable dt = cmd.ExecuteDataTable();
  415. return dt;
  416. }
  417. }
  418. /// <summary>
  419. /// 根据sql语句和参数,返回DataRow
  420. /// </summary>
  421. /// <param name="connString">连接串</param>
  422. /// <param name="sql">sql语句</param>
  423. /// <param name="spArr">可变参数</param>
  424. /// <returns>DataRow</returns>
  425. public static DataRow GetDataRowBySqlWithConn(string connString, string sql, params SqlParameter[] spArr)
  426. {
  427. DataTable dt = GetDataTableBySqlWithConn(connString, sql, spArr);
  428. if (dt == null || dt.Rows.Count == 0)
  429. return null;
  430. return dt.Rows[0];
  431. }
  432. #endregion
  433. #endregion
  434. #region [ 根据SQL文件路径执行 ]
  435. /// <summary>
  436. /// 执行SQL文件
  437. /// </summary>
  438. /// <param name="connString"></param>
  439. /// <param name="filePath"></param>
  440. /// <returns></returns>
  441. public static bool ExecuteNonQueryWithConnAndSqlFilePath(string connString, string filePath)
  442. {
  443. string sql = System.IO.File.ReadAllText(filePath);
  444. return ExecuteNonQueryWithConnAndGO(connString, sql);
  445. }
  446. #endregion
  447. #region [ 执行带Go语句 ]
  448. /// <summary>
  449. /// 执行带"GO"的SQL,返回最后一条SQL的受影响行数
  450. /// </summary>
  451. /// <param name="connString">连接串</param>
  452. /// <param name="sql">sql语句</param>
  453. /// <returns>返回最后一条SQL的受影响行数</returns>
  454. public static bool ExecuteNonQueryWithConnAndGO(string connString, string sql)
  455. {
  456. bool result = true;
  457. string[] arr = System.Text.RegularExpressions.Regex.Split(sql, @"\bGO\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  458. using (SqlConnection conn = GetConnByString(connString))
  459. {
  460. conn.Open();
  461. SqlCommand cmd = new SqlCommand().AddTimeout();
  462. cmd.Connection = conn;
  463. SqlTransaction tx = conn.BeginTransaction();
  464. cmd.Transaction = tx;
  465. try
  466. {
  467. for (int n = 0; n < arr.Length; n++)
  468. {
  469. string strsql = arr[n];
  470. if (strsql.Trim().Length > 1 && strsql.Trim().Replace(";", "") != "")
  471. {
  472. cmd.CommandText = strsql;
  473. cmd.ExecuteNonQuery();
  474. }
  475. }
  476. tx.Commit();
  477. }
  478. catch (System.Data.SqlClient.SqlException E)
  479. {
  480. tx.Rollback();
  481. result = false;
  482. //return -1;
  483. throw new Exception(E.Message);
  484. }
  485. finally
  486. {
  487. if (conn.State != ConnectionState.Closed)
  488. {
  489. conn.Close();
  490. conn.Dispose();
  491. }
  492. }
  493. }
  494. return result;
  495. }
  496. #endregion
  497. /// <summary>
  498. /// 设置Sql参数
  499. /// </summary>
  500. /// <param name="parameterName"></param>
  501. /// <param name="value"></param>
  502. /// <param name="dbType"></param>
  503. /// <returns></returns>
  504. public static SqlParameter SetSqlParameter(string parameterName, object value, SqlDbType dbType)
  505. {
  506. if (value == null || string.IsNullOrEmpty(value.ToString()))
  507. {
  508. return new SqlParameter(parameterName, DBNull.Value);
  509. }
  510. SqlParameter sp = new SqlParameter(parameterName, value);
  511. sp.SqlDbType = dbType;
  512. return sp;
  513. }
  514. /// <summary>
  515. /// 根据sql及带 in 的id参数列表, 得到DataTable
  516. /// </summary>
  517. /// <param name="sql">带in ( {0} )的sql</param>
  518. /// <param name="ids">以逗号分隔的id字符串</param>
  519. /// <param name="idDbType">id的Sql数据类型</param>
  520. /// <returns>DataTable</returns>
  521. public static DataTable GetDataTableWithIn(string sql, string ids, SqlDbType idDbType)
  522. {
  523. SqlParameter[] spArr = GetWithInSqlParameters(ref sql, ids, idDbType);
  524. return GetDataTableBySql(sql, spArr);
  525. }
  526. /// <summary>
  527. /// 根据sql及带 in 的id参数列表, 得到受影响行数
  528. /// </summary>
  529. /// <param name="sql">带in ( {0} )的sql</param>
  530. /// <param name="ids">以逗号分隔的id字符串</param>
  531. /// <param name="idDbType">id的Sql数据类型</param>
  532. /// <returns>DataTable</returns>
  533. public static int ExecuteNonQueryWithIn(string sql, string ids, SqlDbType idDbType)
  534. {
  535. SqlParameter[] spArr = GetWithInSqlParameters(ref sql, ids, idDbType);
  536. return ExecuteNonQuery(sql, spArr);
  537. }
  538. #region [ 带 in 不确定参数的执行方法 ]
  539. /// <summary>
  540. /// 获取带 in 的sql参数列表
  541. /// </summary>
  542. /// <param name="sql">带in ( {0} )的sql</param>
  543. /// <param name="ids">以逗号分隔的id字符串</param>
  544. /// <param name="idDbType">id的Sql数据类型</param>
  545. /// <returns>sql参数列表</returns>
  546. public static SqlParameter[] GetWithInSqlParameters(ref string sql, string ids, SqlDbType idDbType)
  547. {
  548. if (string.IsNullOrEmpty(ids))
  549. {
  550. return null;
  551. }
  552. string[] idArr = ids.Split(',');
  553. //组建sql在in中的字符串
  554. StringBuilder sbCondition = new StringBuilder();
  555. List<SqlParameter> spList = new List<SqlParameter>();
  556. for (int i = 0; i < idArr.Length; i++)
  557. {
  558. string id = idArr[i];
  559. string spName = string.Format("@id{0}", i);
  560. sbCondition.AppendFormat("{0},", spName);
  561. spList.Add(SetSqlParameter(spName, id, idDbType));
  562. }
  563. //重新构建sql
  564. sql = string.Format(sql, sbCondition.ToString().TrimEnd(','));
  565. return spList.ToArray();
  566. }
  567. #endregion
  568. }//end of class
  569. #region [ SqlHelper 的扩展方法类 ]
  570. /// <summary>
  571. /// 扩展方法类
  572. /// </summary>
  573. public static class SqlHelperExtensionMethods
  574. {
  575. /// <summary>
  576. /// 新建SqlCommand对象时, 自动加上指定的 CommandTimeout. by ngye, on 2013-07-11.
  577. /// </summary>
  578. /// <param name="cmd">SqlCommand对象</param>
  579. /// <returns>SqlCommand对象</returns>
  580. public static SqlCommand AddTimeout(this SqlCommand cmd)
  581. {
  582. cmd.CommandTimeout = SqlHelper.CommandTimeout;
  583. return cmd;
  584. }
  585. /// <summary>
  586. /// 新建SqlBulkCopy对象时, 自动加上指定的 BulkCopyTimeout. by ngye, on 2013-08-30.
  587. /// </summary>
  588. /// <param name="cmd">SqlBulkCopy对象</param>
  589. /// <returns>SqlBulkCopy对象</returns>
  590. public static SqlBulkCopy AddTimeout(this SqlBulkCopy bulkCopy)
  591. {
  592. bulkCopy.BulkCopyTimeout = SqlHelper.CommandTimeout;
  593. return bulkCopy;
  594. }
  595. /// <summary>
  596. /// 执行cmd得到 DataTable. by ngye, on 2013-08-01
  597. /// </summary>
  598. /// <param name="cmd"></param>
  599. /// <returns></returns>
  600. public static DataTable ExecuteDataTable(this SqlCommand cmd)
  601. {
  602. DataTable dt = new DataTable();
  603. using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
  604. {
  605. adapter.Fill(dt);
  606. }
  607. return dt;
  608. }
  609. /// <summary>
  610. /// 为SqlParameter设置参数. by ngye, on 2013-08-15.
  611. /// </summary>
  612. /// <param name="sp"></param>
  613. /// <returns></returns>
  614. public static SqlParameter SetValue(this SqlParameter sp, object value)
  615. {
  616. sp.Value = value;
  617. return sp;
  618. }
  619. /// <summary>
  620. /// 为SqlParameter设置SqlDbType. by ngye, on 2013-09-03.
  621. /// </summary>
  622. /// <param name="sp"></param>
  623. /// <returns></returns>
  624. public static SqlParameter SetSqlDbType(this SqlParameter sp, SqlDbType dbType)
  625. {
  626. sp.SqlDbType = dbType;
  627. return sp;
  628. }
  629. /// <summary>
  630. /// 对可以为空的值作这样的处理,一旦其为空,就设置为DBNull.value.
  631. /// </summary>
  632. /// <param name="sp"></param>
  633. /// <returns></returns>
  634. public static SqlParameter[] SetDBNull(this SqlParameter[] spArr)
  635. {
  636. if (spArr == null || spArr.Length == 0)
  637. return spArr;
  638. for (int i = 0; i < spArr.Length; i++)
  639. {
  640. SqlParameter sp = spArr[i];
  641. if (sp.Value == null)
  642. sp.Value = DBNull.Value;
  643. }
  644. return spArr;
  645. }
  646. }
  647. #endregion
  648. }