DataAccess.cs 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using Common.Model;
  7. namespace ZcPeng.PublicLibrary
  8. {
  9. /// <summary>
  10. /// DataAccess
  11. /// 功能:SQL SERVER数据库访问类。
  12. /// 作者:彭昭成
  13. /// 时间:2018年12月10日
  14. /// 注意:在使用前,需要设置connectionString。
  15. /// </summary>
  16. public static class DataAccess
  17. {
  18. //静态字段
  19. /// <summary>
  20. /// 将NText字段的尺寸设定为100M
  21. /// </summary>
  22. public static int NTextSize = 100000000;
  23. /// <summary>
  24. /// 默认的SQL SERVER数据库连接字符串,在使用前需要设定
  25. /// </summary>
  26. public static string connectionString;
  27. /// <summary>
  28. /// 默认BulkCopy操作的操时时间设定为60秒
  29. /// </summary>
  30. public static int BulkCopyTimeout = 60;
  31. /// <summary>
  32. /// 获取SELECT语句返回的数据表
  33. /// </summary>
  34. /// <param name="selectCommandText">SELECT语句</param>
  35. /// <param name="dt">返回得到的数据表</param>
  36. /// <param name="result">返回操作的结果(输出参数)</param>
  37. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  38. public static bool GetDataTable(string selectCommandText, DataTable dt, out string result)
  39. {
  40. bool bSuccessed = false;
  41. result = "";
  42. if (selectCommandText == "")
  43. {
  44. bSuccessed = false;
  45. result = "参数错误:SELECT语句不能为空。";
  46. }
  47. else
  48. {
  49. if (dt == null)
  50. {
  51. bSuccessed = false;
  52. result = "参数错误:dt不能为空。";
  53. }
  54. else
  55. {
  56. SqlConnection conn = new SqlConnection(connectionString);
  57. SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
  58. try
  59. {
  60. conn.Open();
  61. da.SelectCommand.CommandType = CommandType.Text;
  62. da.Fill(dt);
  63. bSuccessed = true;
  64. result = "已经成功的获取了数据表。";
  65. }
  66. catch (Exception e)
  67. {
  68. bSuccessed = false;
  69. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  70. }
  71. finally
  72. {
  73. if (conn != null)
  74. {
  75. if (conn.State != ConnectionState.Closed)
  76. conn.Close();
  77. }
  78. }
  79. }
  80. }
  81. return bSuccessed;
  82. }
  83. /// <summary>
  84. /// 从指定的服务器获取SELECT语句返回的数据表
  85. /// </summary>
  86. /// <param name="strConn">数据库连接字符串</param>
  87. /// <param name="selectCommandText">SELECT语句</param>
  88. /// <param name="dt">返回得到的数据表</param>
  89. /// <param name="parameters">跟该SQL语句相对应的参数数组</param>
  90. /// <param name="result">返回操作的结果(输出参数)</param>
  91. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  92. public static bool GetDataTable(string strConn, string selectCommandText, DataTable dt, SqlParameter[] parameters, out string result)
  93. {
  94. bool bSuccessed = false;
  95. result = "";
  96. if (selectCommandText == "")
  97. {
  98. bSuccessed = false;
  99. result = "参数错误:SELECT语句不能为空。";
  100. }
  101. else
  102. {
  103. if (dt == null)
  104. {
  105. bSuccessed = false;
  106. result = "参数错误:dt不能为空。";
  107. }
  108. else
  109. {
  110. SqlConnection conn = new SqlConnection(strConn);
  111. SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
  112. try
  113. {
  114. conn.Open();
  115. da.SelectCommand.CommandType = CommandType.Text;
  116. if (parameters != null && parameters.Length > 0)
  117. da.SelectCommand.Parameters.AddRange(parameters);
  118. da.Fill(dt);
  119. bSuccessed = true;
  120. result = "已经成功的获取了数据表。";
  121. }
  122. catch (Exception e)
  123. {
  124. bSuccessed = false;
  125. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  126. }
  127. finally
  128. {
  129. if (da != null && da.SelectCommand != null && da.SelectCommand.Parameters.Count > 0)
  130. da.SelectCommand.Parameters.Clear();
  131. if (conn != null)
  132. {
  133. if (conn.State != ConnectionState.Closed)
  134. conn.Close();
  135. }
  136. }
  137. }
  138. }
  139. return bSuccessed;
  140. }
  141. /// <summary>
  142. /// 从指定的多个服务器获取SELECT语句返回的数据表,并合并数据表。
  143. /// 注意:在执行过程中忽略单台服务器的错误;该方法可能会将dt置为null。
  144. /// </summary>
  145. /// <param name="connectionStringList">数据库连接字符串</param>
  146. /// <param name="selectCommandText">SELECT语句</param>
  147. /// <param name="dt">返回得到的数据表</param>
  148. /// <param name="parameters">跟该SQL语句相对应的参数数组</param>
  149. /// <param name="result">返回操作的结果(输出参数)</param>
  150. /// <returns>返回成功获取到数据的服务器数目。</returns>
  151. public static int GetDataTable(List<string> connectionStringList, string selectCommandText, ref DataTable dt, List<SqlParameter> parameters, out string result)
  152. {
  153. int successedServerCount = 0;
  154. result = "";
  155. if (selectCommandText == "")
  156. {
  157. result = "参数错误:SELECT语句不能为空。";
  158. }
  159. else
  160. {
  161. if (connectionStringList == null || connectionStringList.Count == 0)
  162. {
  163. result = "数据库连接字符串列表不能为空。";
  164. }
  165. else
  166. {
  167. dt = null;
  168. foreach (string connectionString in connectionStringList)
  169. {
  170. DataTable dtTemp = new DataTable();
  171. if (GetDataTable(connectionString, selectCommandText, dtTemp, (parameters != null && parameters.Count > 0) ? parameters.ToArray() : null, out result))
  172. {
  173. if (dtTemp != null)
  174. {
  175. if (DataTableEx.Merge(ref dt, dtTemp))
  176. successedServerCount++;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. return successedServerCount;
  183. }
  184. /// <summary>
  185. /// 从指定的多个服务器获取SELECT语句返回的数据表,并汇总数据表。
  186. /// 注意:在执行过程中忽略单台服务器的错误;该方法可能会将dt置为null;除了关键列之外,要求其他列都是整型。
  187. /// </summary>
  188. /// <param name="connectionStringList">数据库连接字符串</param>
  189. /// <param name="selectCommandText">SELECT语句</param>
  190. /// <param name="dt">返回得到的数据表</param>
  191. /// <param name="parameters">跟该SQL语句相对应的参数数组</param>
  192. /// <param name="result">返回操作的结果(输出参数)</param>
  193. /// <param name="keyColumnName">关键列名</param>
  194. /// <returns>返回成功获取到数据的服务器数目。</returns>
  195. public static int GetDataTable(List<string> connectionStringList, string selectCommandText, ref DataTable dt, List<SqlParameter> parameters, out string result, string keyColumnName)
  196. {
  197. List<string> keyColumnNames = new List<string>(1);
  198. keyColumnNames.Add(keyColumnName);
  199. return GetDataTable(connectionStringList, selectCommandText, ref dt, parameters, out result, keyColumnNames);
  200. }
  201. /// <summary>
  202. /// 从指定的多个服务器获取SELECT语句返回的数据表,并汇总数据表。
  203. /// 注意:在执行过程中忽略单台服务器的错误;该方法可能会将dt置为null;除了关键列之外,要求其他列都是整型。
  204. /// </summary>
  205. /// <param name="connectionStringList">数据库连接字符串</param>
  206. /// <param name="selectCommandText">SELECT语句</param>
  207. /// <param name="dt">返回得到的数据表</param>
  208. /// <param name="parameters">跟该SQL语句相对应的参数数组</param>
  209. /// <param name="result">返回操作的结果(输出参数)</param>
  210. /// <param name="keyColumnNames">关键列名</param>
  211. /// <returns>返回成功获取到数据的服务器数目。</returns>
  212. public static int GetDataTable(List<string> connectionStringList, string selectCommandText, ref DataTable dt, List<SqlParameter> parameters, out string result, List<string> keyColumnNames)
  213. {
  214. int successedServerCount = 0;
  215. result = "";
  216. if (selectCommandText == "")
  217. {
  218. result = "参数错误:SELECT语句不能为空。";
  219. }
  220. else
  221. {
  222. if (connectionStringList == null || connectionStringList.Count == 0)
  223. {
  224. result = "数据库连接字符串列表不能为空。";
  225. }
  226. else if (keyColumnNames == null || keyColumnNames.Count == 0)
  227. {
  228. result = "关键列列表不能为空。";
  229. }
  230. else
  231. {
  232. dt = null;
  233. foreach (string connectionString in connectionStringList)
  234. {
  235. DataTable dtTemp = new DataTable();
  236. if (GetDataTable(connectionString, selectCommandText, dtTemp, (parameters != null && parameters.Count > 0) ? parameters.ToArray() : null, out result))
  237. {
  238. if (dtTemp != null)
  239. {
  240. if (DataTableEx.Summarize(ref dt, dtTemp, keyColumnNames))
  241. successedServerCount++;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. return successedServerCount;
  248. }
  249. /// <summary>
  250. /// 获取SELECT语句返回的数据表
  251. /// </summary>
  252. /// <param name="selectCommandText">SELECT语句</param>
  253. /// <param name="dt">返回得到的数据表</param>
  254. /// <param name="parameters">跟该SQL语句相对应的参数数组</param>
  255. /// <param name="result">返回操作的结果(输出参数)</param>
  256. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  257. public static bool GetDataTable(string selectCommandText, DataTable dt, SqlParameter[] parameters, out string result)
  258. {
  259. bool bSuccessed = false;
  260. result = "";
  261. if (selectCommandText == "")
  262. {
  263. bSuccessed = false;
  264. result = "参数错误:SELECT语句不能为空。";
  265. }
  266. else
  267. {
  268. if (dt == null)
  269. {
  270. bSuccessed = false;
  271. result = "参数错误:dt不能为空。";
  272. }
  273. else
  274. {
  275. SqlConnection conn = new SqlConnection(connectionString);
  276. SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
  277. try
  278. {
  279. conn.Open();
  280. da.SelectCommand.CommandType = CommandType.Text;
  281. if (parameters != null && parameters.Length > 0)
  282. da.SelectCommand.Parameters.AddRange(parameters);
  283. da.Fill(dt);
  284. bSuccessed = true;
  285. result = "已经成功的获取了数据表。";
  286. }
  287. catch (Exception e)
  288. {
  289. bSuccessed = false;
  290. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  291. }
  292. finally
  293. {
  294. if (da != null && da.SelectCommand != null && da.SelectCommand.Parameters.Count > 0)
  295. da.SelectCommand.Parameters.Clear();
  296. if (conn != null)
  297. {
  298. if (conn.State != ConnectionState.Closed)
  299. conn.Close();
  300. }
  301. }
  302. }
  303. }
  304. return bSuccessed;
  305. }
  306. /// <summary>
  307. /// 获取SELECT语句返回的数据表
  308. /// </summary>
  309. /// <param name="selectCommandText">SELECT语句</param>
  310. /// <param name="dt">返回得到的数据表</param>
  311. /// <param name="parameters">跟该SQL语句相对应的参数列表</param>
  312. /// <param name="result">返回操作的结果(输出参数)</param>
  313. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  314. public static bool GetDataTable(string selectCommandText, DataTable dt, List<SqlParameter> parameters, out string result)
  315. {
  316. return GetDataTable(selectCommandText, dt, parameters.ToArray(), out result);
  317. }
  318. /// <summary>
  319. /// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
  320. /// ,SqlConnection conn = null, SqlTransaction trans 输出值
  321. /// </summary>
  322. /// <param name="selectCommandText">SELECT语句</param>
  323. /// <param name="objValue">返回的数据</param>
  324. /// <param name="result">返回操作的结果</param>
  325. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  326. public static bool GetOneValue(string selectCommandText, out object objValue, out string result,SqlConnection conn = null, SqlTransaction trans = null, bool needTransOut = false)
  327. {
  328. bool bSuccessed = false;
  329. objValue = null;
  330. result = "";
  331. if (selectCommandText == "")
  332. {
  333. bSuccessed = false;
  334. result = "参数错误:SELECT语句不能为空。";
  335. }
  336. else
  337. {
  338. conn = new SqlConnection(connectionString);
  339. conn.Open();
  340. trans = conn.BeginTransaction();
  341. SqlCommand cmd = new SqlCommand(selectCommandText, conn, trans);
  342. try
  343. {
  344. cmd.CommandType = CommandType.Text;
  345. objValue = cmd.ExecuteScalar();
  346. bSuccessed = true;
  347. result = "已经成功的获取了数据。";
  348. if(!needTransOut)
  349. trans.Commit();
  350. }
  351. catch (Exception e)
  352. {
  353. trans.Rollback();
  354. bSuccessed = false;
  355. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  356. }
  357. finally
  358. {
  359. if (needTransOut)
  360. {
  361. }
  362. else {
  363. if (conn != null)
  364. {
  365. if (conn.State != ConnectionState.Closed)
  366. conn.Close();
  367. }
  368. }
  369. }
  370. }
  371. return bSuccessed;
  372. }
  373. /// <summary>
  374. /// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
  375. /// </summary>
  376. /// <param name="selectCommandText">SELECT语句</param>
  377. /// <param name="parameters">命令参数数组</param>
  378. /// <param name="objValue">返回的数据</param>
  379. /// <param name="result">返回操作的结果</param>
  380. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  381. public static bool GetOneValue(string selectCommandText, SqlParameter[] parameters, out object objValue, out string result)
  382. {
  383. bool bSuccessed = false;
  384. objValue = null;
  385. result = "";
  386. if (selectCommandText == "")
  387. {
  388. bSuccessed = false;
  389. result = "参数错误:SELECT语句不能为空。";
  390. }
  391. else
  392. {
  393. SqlConnection conn = new SqlConnection(connectionString);
  394. conn.Open();
  395. SqlTransaction trans = conn.BeginTransaction();
  396. SqlCommand cmd = new SqlCommand(selectCommandText, conn, trans);
  397. try
  398. {
  399. cmd.CommandType = CommandType.Text;
  400. if (parameters != null && parameters.Length > 0)
  401. cmd.Parameters.AddRange(parameters);
  402. objValue = cmd.ExecuteScalar();
  403. bSuccessed = true;
  404. result = "已经成功的获取了数据。";
  405. trans.Commit();
  406. }
  407. catch (Exception e)
  408. {
  409. trans.Rollback();
  410. bSuccessed = false;
  411. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  412. }
  413. finally
  414. {
  415. if (cmd != null && cmd.Parameters.Count > 0)
  416. cmd.Parameters.Clear();
  417. if (conn != null)
  418. {
  419. if (conn.State != ConnectionState.Closed)
  420. conn.Close();
  421. }
  422. }
  423. }
  424. return bSuccessed;
  425. }
  426. /// <summary>
  427. /// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
  428. /// </summary>
  429. /// <param name="selectCommandText">SELECT语句</param>
  430. /// <param name="parameters">命令参数数组</param>
  431. /// <param name="objValue">返回的数据</param>
  432. /// <param name="result">返回操作的结果</param>
  433. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  434. public static bool GetOneValue(string selectCommandText, SqlParameter[] parameters, out object objValue, out string result, out SqlConnection conn, out SqlTransaction trans, bool needTransOut = false)
  435. {
  436. bool bSuccessed = false;
  437. objValue = null;
  438. result = "";
  439. conn = null;
  440. trans = null;
  441. if (selectCommandText == "")
  442. {
  443. bSuccessed = false;
  444. result = "参数错误:SELECT语句不能为空。";
  445. }
  446. else
  447. {
  448. conn = new SqlConnection(connectionString);
  449. conn.Open();
  450. trans = conn.BeginTransaction();
  451. SqlCommand cmd = new SqlCommand(selectCommandText, conn, trans);
  452. try
  453. {
  454. cmd.CommandType = CommandType.Text;
  455. if (parameters != null && parameters.Length > 0)
  456. cmd.Parameters.AddRange(parameters);
  457. objValue = cmd.ExecuteScalar();
  458. bSuccessed = true;
  459. result = "已经成功的获取了数据。";
  460. if (!needTransOut)
  461. trans.Commit();
  462. }
  463. catch (Exception e)
  464. {
  465. trans.Rollback();
  466. bSuccessed = false;
  467. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  468. }
  469. finally
  470. {
  471. if (cmd != null && cmd.Parameters.Count > 0)
  472. cmd.Parameters.Clear();
  473. if (needTransOut)
  474. {
  475. }
  476. else
  477. {
  478. if (conn != null)
  479. {
  480. if (conn.State != ConnectionState.Closed)
  481. conn.Close();
  482. }
  483. }
  484. }
  485. }
  486. return bSuccessed;
  487. }
  488. /// <summary>
  489. /// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
  490. /// </summary>
  491. /// <param name="strConn">数据库连接字符串</param>
  492. /// <param name="selectCommandText">SELECT语句</param>
  493. /// <param name="parameters">命令参数数组</param>
  494. /// <param name="objValue">返回的数据</param>
  495. /// <param name="result">返回操作的结果</param>
  496. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  497. public static bool GetOneValue(string strConn, string selectCommandText, SqlParameter[] parameters, out object objValue, out string result)
  498. {
  499. bool bSuccessed = false;
  500. objValue = null;
  501. result = "";
  502. if (selectCommandText == "")
  503. {
  504. bSuccessed = false;
  505. result = "参数错误:SELECT语句不能为空。";
  506. }
  507. else
  508. {
  509. SqlConnection conn = new SqlConnection(strConn);
  510. SqlCommand cmd = new SqlCommand(selectCommandText, conn);
  511. try
  512. {
  513. conn.Open();
  514. cmd.CommandType = CommandType.Text;
  515. if (parameters != null && parameters.Length > 0)
  516. cmd.Parameters.AddRange(parameters);
  517. objValue = cmd.ExecuteScalar();
  518. bSuccessed = true;
  519. result = "已经成功的获取了数据。";
  520. }
  521. catch (Exception e)
  522. {
  523. bSuccessed = false;
  524. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  525. }
  526. finally
  527. {
  528. if (cmd != null && cmd.Parameters.Count > 0)
  529. cmd.Parameters.Clear();
  530. if (conn != null)
  531. {
  532. if (conn.State != ConnectionState.Closed)
  533. conn.Close();
  534. }
  535. }
  536. }
  537. return bSuccessed;
  538. }
  539. /// <summary>
  540. /// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
  541. /// </summary>
  542. /// <param name="selectCommandText">SELECT语句</param>
  543. /// <param name="parameters">命令参数列表</param>
  544. /// <param name="objValue">返回的数据</param>
  545. /// <param name="result">返回操作的结果</param>
  546. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  547. public static bool GetOneValue(string selectCommandText, List<SqlParameter> parameters, out object objValue, out string result)
  548. {
  549. return GetOneValue(selectCommandText, parameters.ToArray(), out objValue, out result);
  550. }
  551. /// <summary>
  552. /// 执行SQL语句,并返回受影响的行数。
  553. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
  554. /// </summary>
  555. /// <param name="commandText">SQL语句</param>
  556. /// <param name="result">返回操作的结果</param>
  557. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  558. public static int ExecuteCommand(string connectionString,string commandText, out string result)
  559. {
  560. int recordsAffected = 0; //受影响的行数
  561. result = "";
  562. if (commandText == "")
  563. {
  564. result = "参数错误:SQL语句不能为空。";
  565. }
  566. else
  567. {
  568. SqlConnection conn = new SqlConnection(connectionString);
  569. SqlCommand cmd = new SqlCommand(commandText, conn);
  570. try
  571. {
  572. conn.Open();
  573. cmd.CommandType = CommandType.Text;
  574. recordsAffected = cmd.ExecuteNonQuery();
  575. if (recordsAffected > 0)
  576. result = "已经成功的影响了" + recordsAffected + "条记录。";
  577. }
  578. catch (Exception e)
  579. {
  580. result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  581. }
  582. finally
  583. {
  584. if (cmd != null && cmd.Parameters.Count > 0)
  585. cmd.Parameters.Clear();
  586. if (conn != null)
  587. {
  588. if (conn.State != ConnectionState.Closed)
  589. conn.Close();
  590. }
  591. }
  592. }
  593. return recordsAffected;
  594. }
  595. /// <summary>
  596. /// 执行SQL语句,并返回受影响的行数。
  597. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
  598. /// </summary>
  599. /// <param name="commandText">SQL语句</param>
  600. /// <param name="result">返回操作的结果</param>
  601. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  602. public static int ExecuteCommand( string commandText, out string result)
  603. {
  604. return ExecuteCommand(connectionString,commandText, out result);
  605. }
  606. /// <summary>
  607. /// 执行SQL语句,并返回受影响的行数。
  608. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
  609. /// </summary>
  610. /// <param name="commandText">SQL语句</param>
  611. /// <param name="parameters">参数列表</param>
  612. /// <param name="result">返回操作的结果</param>
  613. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  614. public static int ExecuteCommand(string connectionString,string commandText, List<SqlParameter> parameters, out string result)
  615. {
  616. int recordsAffected = 0; //受影响的行数
  617. result = "";
  618. if (commandText == "")
  619. {
  620. result = "参数错误:SQL语句不能为空。";
  621. }
  622. else
  623. {
  624. SqlConnection conn = new SqlConnection(connectionString);
  625. SqlCommand cmd = new SqlCommand(commandText, conn);
  626. try
  627. {
  628. conn.Open();
  629. cmd.CommandType = CommandType.Text;
  630. if (parameters != null && parameters.Count > 0)
  631. cmd.Parameters.AddRange(parameters.ToArray());
  632. recordsAffected = cmd.ExecuteNonQuery();
  633. if (recordsAffected > 0)
  634. result = "已经成功的影响了" + recordsAffected + "条记录。";
  635. }
  636. catch (Exception e)
  637. {
  638. result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  639. }
  640. finally
  641. {
  642. if (cmd != null && cmd.Parameters.Count > 0)
  643. cmd.Parameters.Clear();
  644. if (conn != null)
  645. {
  646. if (conn.State != ConnectionState.Closed)
  647. conn.Close();
  648. }
  649. }
  650. }
  651. return recordsAffected;
  652. }
  653. /// <summary>
  654. /// 执行SQL语句,并返回受影响的行数。
  655. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
  656. /// </summary>
  657. /// <param name="commandText">SQL语句</param>
  658. /// <param name="parameters">参数列表</param>
  659. /// <param name="result">返回操作的结果</param>
  660. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  661. public static int ExecuteCommand(string commandText, List<SqlParameter> parameters, out string result)
  662. {
  663. return ExecuteCommand(connectionString,commandText, parameters,out result);
  664. }
  665. /// <summary>
  666. /// 执行SQL语句,并返回受影响的行数。
  667. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
  668. /// </summary>
  669. /// <param name="commandText">SQL语句</param>
  670. /// <param name="parameters">参数数组</param>
  671. /// <param name="result">返回操作的结果</param>
  672. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  673. public static int ExecuteCommand(string commandText, SqlParameter[] parameters, out string result)
  674. {
  675. return ExecuteCommand(commandText, new List<SqlParameter>(parameters), out result);
  676. }
  677. /// <summary>
  678. /// 执行SQL语句,并返回受影响的行数。
  679. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE等。
  680. /// </summary>
  681. /// <param name="commandText">SQL语句</param>
  682. /// <param name="parameters">参数数组</param>
  683. /// <param name="result">返回操作的结果</param>
  684. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  685. public static int ExecuteCommand(string connectionString, string commandText, SqlParameter[] parameters, out string result)
  686. {
  687. return ExecuteCommand(connectionString,commandText, new List<SqlParameter>(parameters), out result);
  688. }
  689. /// <summary>
  690. /// 执行SQL语句,并返回受影响的行数。
  691. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
  692. /// </summary>
  693. /// <param name="commandText">SQL语句或者存储过程名</param>
  694. /// <param name="parameters">参数列表</param>
  695. /// <param name="result">返回操作的结果</param>
  696. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  697. public static int ExecuteCommand(string commandText, List<SqlParameter> parameters, CommandType commandType, out string result)
  698. {
  699. int recordsAffected = 0; //受影响的行数
  700. result = "";
  701. if (commandText == "")
  702. {
  703. result = "参数错误:SQL语句不能为空。";
  704. }
  705. else
  706. {
  707. SqlConnection conn = new SqlConnection(connectionString);
  708. SqlCommand cmd = new SqlCommand(commandText, conn);
  709. try
  710. {
  711. conn.Open();
  712. cmd.CommandType = commandType;
  713. if (parameters != null && parameters.Count > 0)
  714. cmd.Parameters.AddRange(parameters.ToArray());
  715. recordsAffected = cmd.ExecuteNonQuery();
  716. if (recordsAffected > 0)
  717. result = "已经成功的影响了" + recordsAffected + "条记录。";
  718. }
  719. catch (Exception e)
  720. {
  721. result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  722. }
  723. finally
  724. {
  725. if (cmd != null && cmd.Parameters.Count > 0)
  726. cmd.Parameters.Clear();
  727. if (conn != null)
  728. {
  729. if (conn.State != ConnectionState.Closed)
  730. conn.Close();
  731. }
  732. }
  733. }
  734. return recordsAffected;
  735. }
  736. /// <summary>
  737. /// 执行SQL语句,并返回受影响的行数。
  738. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
  739. /// </summary>
  740. /// <param name="strConn">数据库连接字符串</param>
  741. /// <param name="commandText">SQL语句或者存储过程名</param>
  742. /// <param name="parameters">参数列表</param>
  743. /// <param name="commandType">命令类型</param>
  744. /// <param name="result">返回操作的结果</param>
  745. /// <returns>返回操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  746. public static int ExecuteCommand(string strConn, string commandText, List<SqlParameter> parameters, CommandType commandType, out string result)
  747. {
  748. int recordsAffected = 0; //受影响的行数
  749. result = "";
  750. if (commandText == "")
  751. {
  752. result = "参数错误:SQL语句不能为空。";
  753. }
  754. else
  755. {
  756. SqlConnection conn = new SqlConnection(strConn);
  757. SqlCommand cmd = new SqlCommand(commandText, conn);
  758. try
  759. {
  760. conn.Open();
  761. cmd.CommandType = commandType;
  762. if (parameters != null && parameters.Count > 0)
  763. cmd.Parameters.AddRange(parameters.ToArray());
  764. recordsAffected = cmd.ExecuteNonQuery();
  765. if (recordsAffected > 0)
  766. result = "已经成功的影响了" + recordsAffected + "条记录。";
  767. }
  768. catch (Exception e)
  769. {
  770. result = "在执行数据操作时发生错误。\r\nSQL语句:" + commandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  771. }
  772. finally
  773. {
  774. if (cmd != null && cmd.Parameters.Count > 0)
  775. cmd.Parameters.Clear();
  776. if (conn != null)
  777. {
  778. if (conn.State != ConnectionState.Closed)
  779. conn.Close();
  780. }
  781. }
  782. }
  783. return recordsAffected;
  784. }
  785. /// <summary>
  786. /// 执行SQL语句,并返回受影响的行数。
  787. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
  788. /// </summary>
  789. /// <param name="connectionStringList">数据库连接字符串列表</param>
  790. /// <param name="dac">命令</param>
  791. /// <param name="needAllDone">是否需要所有服务器都执行成功才提交事务</param>
  792. /// <param name="result">返回操作的结果</param>
  793. /// <returns>返回在每个数据库上操作所影响的行数,如果成功,行数大于0;如果失败,行数为0。</returns>
  794. public static List<int> ExecuteCommand(List<string> connectionStringList, DataAccessCommand dac, bool needAllDone, out string result)
  795. {
  796. result = "";
  797. if (connectionStringList == null || connectionStringList.Count == 0)
  798. {
  799. result = "数据库连接字符串列表为空。";
  800. return null;
  801. }
  802. //定义变量
  803. int serverCount = connectionStringList.Count;
  804. List<SqlConnection> connList = new List<SqlConnection>(serverCount);
  805. List<SqlTransaction> transList = new List<SqlTransaction>(serverCount);
  806. List<SqlCommand> cmdList = new List<SqlCommand>(serverCount);
  807. List<int> recordsAffectedList = new List<int>(serverCount);
  808. bool transCommit = true; //是否提交事务
  809. //分别在每台服务器上执行命令
  810. for (int i = 0; i < serverCount; i++)
  811. {
  812. connList.Add(null);
  813. transList.Add(null);
  814. cmdList.Add(null);
  815. recordsAffectedList.Add(0);
  816. try
  817. {
  818. if (string.IsNullOrEmpty(connectionStringList[i]))
  819. {
  820. result = string.Format("第{0}个数据库连接字符串为空。", i);
  821. }
  822. else
  823. {
  824. connList[i] = new SqlConnection(connectionStringList[i]);
  825. connList[i].Open();
  826. transList[i] = connList[i].BeginTransaction();
  827. cmdList[i] = new SqlCommand(dac.CommandText, connList[i], transList[i]);
  828. cmdList[i].CommandType = dac.CommandType;
  829. if (dac.ParamList != null && dac.ParamList.Count > 0)
  830. cmdList[i].Parameters.AddRange(dac.ParamList.ToArray());
  831. recordsAffectedList[i] = cmdList[i].ExecuteNonQuery();
  832. }
  833. }
  834. catch (Exception e)
  835. {
  836. result = "在服务器(" + connectionStringList[i] + ")执行数据操作时发生错误。\r\nSQL语句:" + dac.CommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  837. }
  838. finally
  839. {
  840. if (cmdList[i] != null && cmdList[i].Parameters.Count > 0)
  841. cmdList[i].Parameters.Clear(); //为了反复利用参数,某个命令对象在用完之后需要清除参数
  842. }
  843. if (needAllDone)
  844. {
  845. if (dac.RecordsAffectedLargeThanZeroIsSuccessful && recordsAffectedList[i] <= 0)
  846. {
  847. transCommit = false;
  848. break;
  849. }
  850. }
  851. }
  852. //提交还是回滚事务
  853. foreach (SqlTransaction trans in transList)
  854. {
  855. if (trans != null)
  856. {
  857. if (transCommit)
  858. trans.Commit();
  859. else
  860. trans.Rollback();
  861. }
  862. }
  863. foreach (SqlConnection conn in connList)
  864. {
  865. if (conn != null && conn.State != ConnectionState.Closed)
  866. conn.Close();
  867. }
  868. return recordsAffectedList;
  869. }
  870. /// <summary>
  871. /// 在事务内处理一组命令
  872. /// SqlConnection conn1 = null, SqlTransaction trans1 输入参数
  873. /// </summary>
  874. /// <param name="commands">命令列表</param>
  875. /// <param name="result">输出参数,如果失败,返回错误提示</param>
  876. /// <returns>返回是否执行成功</returns>
  877. public static bool ExecuteBatchCommands(List<DataAccessCommand> commands, out string result, SqlConnection conn1 = null, SqlTransaction trans1 = null,bool needCommitNow = true)
  878. {
  879. bool successed = false;
  880. result = "";
  881. SqlConnection conn;
  882. SqlTransaction trans = null;
  883. if (conn1 == null) {
  884. conn = new SqlConnection(connectionString);
  885. }else
  886. {
  887. conn = conn1;
  888. }
  889. //SqlTransaction trans = null;
  890. SqlCommand cmd = new SqlCommand("", conn,trans);
  891. try
  892. {
  893. if (conn1 == null)
  894. {
  895. conn.Open();
  896. }
  897. if (trans1 == null)
  898. {
  899. trans = conn.BeginTransaction();
  900. }
  901. else
  902. {
  903. trans = trans1;
  904. }
  905. cmd.Transaction = trans;
  906. int executedCommands = 0;
  907. int i = 1;
  908. foreach (DataAccessCommand command in commands)
  909. {
  910. cmd.CommandType = command.CommandType;
  911. cmd.CommandText = command.CommandText;
  912. cmd.Parameters.Clear();
  913. if (command.ParamList != null && command.ParamList.Count > 0)
  914. cmd.Parameters.AddRange(command.ParamList.ToArray());
  915. int recordsAffected = cmd.ExecuteNonQuery();
  916. if (recordsAffected <= 0)
  917. result += $"在执行第{i}条返回false。\r\n recordsAffected:" + recordsAffected +"/n ";
  918. i++;
  919. if (command.RecordsAffectedLargeThanZeroIsSuccessful)
  920. {
  921. //命令影响的行数大于0才算成功
  922. if (recordsAffected > 0)
  923. executedCommands++;
  924. else
  925. break;
  926. }
  927. else
  928. executedCommands++;
  929. }
  930. if (needCommitNow)
  931. {
  932. if (executedCommands == commands.Count)
  933. {
  934. trans.Commit();
  935. successed = true;
  936. }
  937. else
  938. trans.Rollback();
  939. }
  940. result += $"需要执行{commands.Count}条 /n";
  941. result += $"共执行了{executedCommands}条 /n";
  942. }
  943. catch (Exception e)
  944. {
  945. result = "在执行数据操作时发生错误。\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  946. try
  947. {
  948. if (needCommitNow)
  949. {
  950. trans.Rollback();
  951. }
  952. }
  953. catch
  954. {
  955. }
  956. }
  957. finally
  958. {
  959. if (cmd != null && cmd.Parameters.Count > 0)
  960. cmd.Parameters.Clear();
  961. if (conn != null)
  962. {
  963. if (needCommitNow)
  964. {
  965. if (conn.State != ConnectionState.Closed)
  966. conn.Close();
  967. }
  968. }
  969. }
  970. return successed;
  971. }
  972. /// <summary>
  973. /// 在事务内处理一组命令
  974. /// </summary>
  975. /// <param name="strConn">数据库连接字符串</param>
  976. /// <param name="commands">命令列表</param>
  977. /// <param name="result">输出参数,如果失败,返回错误提示</param>
  978. /// <returns>返回是否执行成功</returns>
  979. public static bool ExecuteBatchCommands(string strConn, List<DataAccessCommand> commands, out string result)
  980. {
  981. bool successed = false;
  982. result = "";
  983. SqlConnection conn = new SqlConnection(strConn);
  984. SqlTransaction trans = null;
  985. SqlCommand cmd = new SqlCommand("", conn);
  986. try
  987. {
  988. conn.Open();
  989. trans = conn.BeginTransaction();
  990. cmd.Transaction = trans;
  991. int executedCommands = 0;
  992. foreach (DataAccessCommand command in commands)
  993. {
  994. cmd.CommandType = command.CommandType;
  995. cmd.CommandText = command.CommandText;
  996. cmd.Parameters.Clear();
  997. if (command.ParamList != null && command.ParamList.Count > 0)
  998. cmd.Parameters.AddRange(command.ParamList.ToArray());
  999. int recordsAffected = cmd.ExecuteNonQuery();
  1000. if (command.RecordsAffectedLargeThanZeroIsSuccessful)
  1001. {
  1002. //命令影响的行数大于0才算成功
  1003. if (recordsAffected > 0)
  1004. executedCommands++;
  1005. else
  1006. break;
  1007. }
  1008. else
  1009. executedCommands++;
  1010. }
  1011. if (executedCommands == commands.Count)
  1012. {
  1013. trans.Commit();
  1014. successed = true;
  1015. }
  1016. else
  1017. trans.Rollback();
  1018. }
  1019. catch (Exception e)
  1020. {
  1021. result = "在执行数据操作时发生错误。\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  1022. try
  1023. {
  1024. trans.Rollback();
  1025. }
  1026. catch
  1027. {
  1028. }
  1029. }
  1030. finally
  1031. {
  1032. if (cmd != null && cmd.Parameters.Count > 0)
  1033. cmd.Parameters.Clear();
  1034. if (conn != null)
  1035. {
  1036. if (conn.State != ConnectionState.Closed)
  1037. conn.Close();
  1038. }
  1039. }
  1040. return successed;
  1041. }
  1042. /// <summary>
  1043. /// 在多个服务器上执行一组命令。
  1044. /// 注意:这里常见的SQL语句包括INSERT、UPDATE、DELETE和存储过程。
  1045. /// </summary>
  1046. /// <param name="connectionStringList">数据库连接字符串列表</param>
  1047. /// <param name="dacList">命令列表</param>
  1048. /// <param name="needAllDone">是否需要所有服务器都执行成功才提交事务</param>
  1049. /// <param name="result">返回操作的结果</param>
  1050. /// <returns>返回在每个服务器上是否执行成功。</returns>
  1051. public static List<bool> ExecuteBatchCommands(List<string> connectionStringList, List<DataAccessCommand> dacList, bool needAllDone, out string result)
  1052. {
  1053. result = "";
  1054. if (connectionStringList == null || connectionStringList.Count == 0)
  1055. {
  1056. result = "数据库连接字符串列表为空。";
  1057. return null;
  1058. }
  1059. if (dacList == null || dacList.Count == 0)
  1060. {
  1061. result = "命令列表为空。";
  1062. return null;
  1063. }
  1064. //定义变量
  1065. int serverCount = connectionStringList.Count;
  1066. List<SqlConnection> connList = new List<SqlConnection>(serverCount);
  1067. List<SqlTransaction> transList = new List<SqlTransaction>(serverCount);
  1068. List<SqlCommand> cmdList = new List<SqlCommand>(serverCount);
  1069. List<bool> successList = new List<bool>(serverCount);
  1070. List<bool> transCommitList = new List<bool>(serverCount); //是否在某个服务器提交事务
  1071. bool allDone = true; //是否所有的服务器都执行成功
  1072. //分别在每台服务器上执行命令
  1073. for (int i = 0; i < serverCount; i++)
  1074. {
  1075. connList.Add(null);
  1076. transList.Add(null);
  1077. cmdList.Add(null);
  1078. successList.Add(false);
  1079. transCommitList.Add(false);
  1080. int executedCommands = 0;
  1081. successList[i] = false;
  1082. try
  1083. {
  1084. if (string.IsNullOrEmpty(connectionStringList[i]))
  1085. {
  1086. result = string.Format("第{0}个数据库连接字符串为空。", i);
  1087. }
  1088. else
  1089. {
  1090. connList[i] = new SqlConnection(connectionStringList[i]);
  1091. connList[i].Open();
  1092. transList[i] = connList[i].BeginTransaction();
  1093. cmdList[i] = new SqlCommand("", connList[i], transList[i]);
  1094. foreach (DataAccessCommand dac in dacList)
  1095. {
  1096. cmdList[i].CommandText = dac.CommandText;
  1097. cmdList[i].CommandType = dac.CommandType;
  1098. if (dac.ParamList != null && dac.ParamList.Count > 0)
  1099. {
  1100. cmdList[i].Parameters.Clear();
  1101. cmdList[i].Parameters.AddRange(dac.ParamList.ToArray());
  1102. }
  1103. int recordsAffected = cmdList[i].ExecuteNonQuery();
  1104. if (dac.RecordsAffectedLargeThanZeroIsSuccessful)
  1105. {
  1106. //命令影响的行数大于0才算成功
  1107. if (recordsAffected > 0)
  1108. executedCommands++;
  1109. else
  1110. break;
  1111. }
  1112. else
  1113. executedCommands++;
  1114. }
  1115. if (executedCommands == dacList.Count)
  1116. {
  1117. transCommitList[i] = true;
  1118. successList[i] = true;
  1119. }
  1120. else
  1121. {
  1122. transCommitList[i] = false;
  1123. successList[i] = false;
  1124. }
  1125. }
  1126. }
  1127. catch (Exception e)
  1128. {
  1129. result = "在服务器(" + connectionStringList[i] + ")执行数据操作时发生错误。\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  1130. }
  1131. finally
  1132. {
  1133. if (cmdList[i] != null && cmdList[i].Parameters.Count > 0)
  1134. cmdList[i].Parameters.Clear(); //为了反复利用参数,某个命令对象在用完之后需要清除参数
  1135. }
  1136. if (needAllDone)
  1137. {
  1138. if (successList[i] == false)
  1139. {
  1140. allDone = false;
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. //提交还是回滚事务
  1146. for (int i = 0; i < connList.Count; i++)
  1147. {
  1148. if (needAllDone && allDone == false)
  1149. transCommitList[i] = false;
  1150. if (transList[i] != null)
  1151. {
  1152. if (transCommitList[i])
  1153. transList[i].Commit();
  1154. else
  1155. transList[i].Rollback();
  1156. }
  1157. if (connList[i] != null && connList[i].State != ConnectionState.Closed)
  1158. {
  1159. connList[i].Close();
  1160. connList[i] = null;
  1161. }
  1162. }
  1163. return successList;
  1164. }
  1165. /// <summary>
  1166. /// 用BulkCopy的形式将数据表的内容全部保存到数据库中去
  1167. /// </summary>
  1168. /// <param name="strConn">数据库连接字符串</param>
  1169. /// <param name="dtSrc">源数据表</param>
  1170. /// <param name="destTableName">数据库中的目标表名</param>
  1171. /// <param name="columnMappings">列映射列表,如果为null或者Count为0,表示使用默认列映射</param>
  1172. /// <param name="transType">事务类型</param>
  1173. /// <param name="options">选项</param>
  1174. /// <param name="timeout">操时时间(单位:秒)</param>
  1175. /// <returns>返回保存是否成功</returns>
  1176. public static bool BulkCopy(string strConn, DataTable dtSrc, string destTableName, List<SqlBulkCopyColumnMapping> columnMappings, BulkCopyTransType transType, SqlBulkCopyOptions options, int timeout)
  1177. {
  1178. bool bSuccessed = false;
  1179. if (dtSrc != null && dtSrc.Rows != null && dtSrc.Rows.Count > 0 && destTableName != null && destTableName != "" && timeout > 0)
  1180. {
  1181. SqlConnection conn = null;
  1182. SqlTransaction trans = null;
  1183. SqlBulkCopy sbc = null;
  1184. try
  1185. {
  1186. //对不同的事务类型使用不同的构造函数
  1187. if (transType == BulkCopyTransType.None)
  1188. {
  1189. sbc = new SqlBulkCopy(strConn, options);
  1190. }
  1191. else if (transType == BulkCopyTransType.Internal)
  1192. {
  1193. sbc = new SqlBulkCopy(strConn, options | SqlBulkCopyOptions.UseInternalTransaction);
  1194. }
  1195. else
  1196. {
  1197. conn = new SqlConnection(strConn);
  1198. conn.Open();
  1199. trans = conn.BeginTransaction();
  1200. sbc = new SqlBulkCopy(conn, options, trans);
  1201. }
  1202. //设置操时时间
  1203. sbc.BulkCopyTimeout = timeout;
  1204. //设置数据库中的目标表名
  1205. sbc.DestinationTableName = destTableName;
  1206. //设置列映射
  1207. if (columnMappings != null && columnMappings.Count > 0)
  1208. {
  1209. foreach (SqlBulkCopyColumnMapping columnMapping in columnMappings)
  1210. {
  1211. sbc.ColumnMappings.Add(columnMapping);
  1212. }
  1213. }
  1214. sbc.WriteToServer(dtSrc);
  1215. if (trans != null)
  1216. trans.Commit();
  1217. bSuccessed = true;
  1218. }
  1219. catch
  1220. {
  1221. bSuccessed = false;
  1222. if (trans != null)
  1223. trans.Rollback();
  1224. }
  1225. finally
  1226. {
  1227. if (sbc != null)
  1228. sbc.Close();
  1229. if (conn != null)
  1230. {
  1231. if (conn.State != ConnectionState.Closed)
  1232. conn.Close();
  1233. }
  1234. }
  1235. }
  1236. return bSuccessed;
  1237. }
  1238. /// <summary>
  1239. /// 用BulkCopy的形式将数据表的内容全部保存到数据库中去
  1240. /// </summary>
  1241. /// <param name="dtSrc">源数据表</param>
  1242. /// <param name="destTableName">数据库中的目标表名</param>
  1243. /// <param name="columnMappings">列映射列表,如果为null或者Count为0,表示使用默认列映射</param>
  1244. /// <param name="transType">事务类型</param>
  1245. /// <param name="options">选项</param>
  1246. /// <param name="timeout">操时时间(单位:秒)</param>
  1247. /// <returns>返回保存是否成功</returns>
  1248. public static bool BulkCopy(DataTable dtSrc, string destTableName, List<SqlBulkCopyColumnMapping> columnMappings, BulkCopyTransType transType, SqlBulkCopyOptions options, int timeout)
  1249. {
  1250. bool bSuccessed = false;
  1251. if (dtSrc != null && dtSrc.Rows != null && dtSrc.Rows.Count > 0 && destTableName != null && destTableName != "" && timeout > 0)
  1252. {
  1253. SqlConnection conn = null;
  1254. SqlTransaction trans = null;
  1255. SqlBulkCopy sbc = null;
  1256. try
  1257. {
  1258. //对不同的事务类型使用不同的构造函数
  1259. if (transType == BulkCopyTransType.None)
  1260. {
  1261. sbc = new SqlBulkCopy(DataAccess.connectionString, options);
  1262. }
  1263. else if (transType == BulkCopyTransType.Internal)
  1264. {
  1265. sbc = new SqlBulkCopy(DataAccess.connectionString, options | SqlBulkCopyOptions.UseInternalTransaction);
  1266. }
  1267. else
  1268. {
  1269. conn = new SqlConnection(DataAccess.connectionString);
  1270. conn.Open();
  1271. trans = conn.BeginTransaction();
  1272. sbc = new SqlBulkCopy(conn, options, trans);
  1273. }
  1274. //设置操时时间
  1275. sbc.BulkCopyTimeout = timeout;
  1276. //设置数据库中的目标表名
  1277. sbc.DestinationTableName = destTableName;
  1278. //设置列映射
  1279. if (columnMappings != null && columnMappings.Count > 0)
  1280. {
  1281. foreach (SqlBulkCopyColumnMapping columnMapping in columnMappings)
  1282. {
  1283. sbc.ColumnMappings.Add(columnMapping);
  1284. }
  1285. }
  1286. sbc.WriteToServer(dtSrc);
  1287. if (trans != null)
  1288. trans.Commit();
  1289. bSuccessed = true;
  1290. }
  1291. catch
  1292. {
  1293. bSuccessed = false;
  1294. if (trans != null)
  1295. trans.Rollback();
  1296. }
  1297. finally
  1298. {
  1299. if (sbc != null)
  1300. sbc.Close();
  1301. if (conn != null)
  1302. {
  1303. if (conn.State != ConnectionState.Closed)
  1304. conn.Close();
  1305. }
  1306. }
  1307. }
  1308. return bSuccessed;
  1309. }
  1310. //以下是几个BulkCopy的不同重载
  1311. public static bool BulkCopy(DataTable dtSrc, string destTableName)
  1312. {
  1313. return BulkCopy(dtSrc, destTableName, null, BulkCopyTransType.None, SqlBulkCopyOptions.Default, BulkCopyTimeout);
  1314. }
  1315. public static bool BulkCopy(DataTable dtSrc, string destTableName, List<SqlBulkCopyColumnMapping> columnMappings)
  1316. {
  1317. return BulkCopy(dtSrc, destTableName, columnMappings, BulkCopyTransType.None, SqlBulkCopyOptions.Default, BulkCopyTimeout);
  1318. }
  1319. public static bool BulkCopy(DataTable dtSrc, string destTableName, List<SqlBulkCopyColumnMapping> columnMappings, BulkCopyTransType transType)
  1320. {
  1321. return BulkCopy(dtSrc, destTableName, columnMappings, transType, SqlBulkCopyOptions.Default, BulkCopyTimeout);
  1322. }
  1323. public static bool BulkCopy(DataTable dtSrc, string destTableName, List<SqlBulkCopyColumnMapping> columnMappings, BulkCopyTransType transType, SqlBulkCopyOptions options)
  1324. {
  1325. return BulkCopy(dtSrc, destTableName, columnMappings, transType, options, BulkCopyTimeout);
  1326. }
  1327. /// <summary>
  1328. /// 得到某个表中符合条件的记录数目
  1329. /// </summary>
  1330. /// <param name="tableName">表名</param>
  1331. /// <param name="filter">筛选条件</param>
  1332. /// <param name="distinct">是否只统计不重复的记录</param>
  1333. /// <param name="result">输出参数,返回错误信息</param>
  1334. /// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1335. public static int GetRowCount(string tableName, string filter, bool distinct, out string result)
  1336. {
  1337. int rowCount = -1;
  1338. string selectCommandText = "SELECT " + (distinct ? "DISTINCT" : "") + " COUNT(*) FROM [" + tableName + "]";
  1339. if (filter != "")
  1340. selectCommandText += " WHERE " + filter;
  1341. object objValue;
  1342. if (DataAccess.GetOneValue(selectCommandText, out objValue, out result))
  1343. {
  1344. if (!Convert.IsDBNull(objValue))
  1345. rowCount = (int)objValue;
  1346. }
  1347. return rowCount;
  1348. }
  1349. public static int GetRowCount(string tableName, string filter, out string result)
  1350. {
  1351. return GetRowCount(tableName, filter, false, out result);
  1352. }
  1353. public static int GetRowCount(string tableName, string filter)
  1354. {
  1355. string result;
  1356. return GetRowCount(tableName, filter, false, out result);
  1357. }
  1358. public static int GetRowCount(string tableName)
  1359. {
  1360. string result;
  1361. return GetRowCount(tableName, "", false, out result);
  1362. }
  1363. /// <summary>
  1364. /// 使用分页存储过程获取符合条件的分页数据表
  1365. /// </summary>
  1366. /// <param name="tableName">表名</param>
  1367. /// <param name="primaryKey">主键字段名</param>
  1368. /// <param name="fields">字段列表</param>
  1369. /// <param name="filter">过滤条件(不包含WHERE)</param>
  1370. /// <param name="group">分组字段列表(不包含GROUP BY)</param>
  1371. /// <param name="sort">排序字段(不包含ORDER BY)</param>
  1372. /// <param name="pageNumber">页号</param>
  1373. /// <param name="pageSize">页大小</param>
  1374. /// <returns>返回数据表;如果获取失败,返回null。</returns>
  1375. public static DataTable GetDataTable(string tableName, string primaryKey, string fields, string filter, string group, string sort, int pageNumber, int pageSize,out string result)
  1376. {
  1377. //查询语句:这里使用分页查询存储过程
  1378. string commandText = "EXECUTE SP_Pagination @Tables,@PrimaryKey,@Sort,@CurrentPage,@PageSize,@Fields,@Filter,@Group";
  1379. //参数列表
  1380. List<SqlParameter> paramList = new List<SqlParameter>(5);
  1381. SqlParameter p;
  1382. p = new SqlParameter("@Tables", SqlDbType.NVarChar, 1000);
  1383. p.Value = tableName;
  1384. paramList.Add(p);
  1385. p = new SqlParameter("@PrimaryKey", SqlDbType.NVarChar, 100);
  1386. p.Value = primaryKey;
  1387. paramList.Add(p);
  1388. p = new SqlParameter("@Sort", SqlDbType.NVarChar, 200);
  1389. p.Value = sort;
  1390. paramList.Add(p);
  1391. p = new SqlParameter("@CurrentPage", SqlDbType.Int, 4);
  1392. p.Value = pageNumber;
  1393. paramList.Add(p);
  1394. p = new SqlParameter("@PageSize", SqlDbType.Int, 4);
  1395. p.Value = pageSize;
  1396. paramList.Add(p);
  1397. p = new SqlParameter("@Fields", SqlDbType.NVarChar, 1000);
  1398. p.Value = fields;
  1399. paramList.Add(p);
  1400. p = new SqlParameter("@Filter", SqlDbType.NVarChar, 1000);
  1401. p.Value = filter;
  1402. paramList.Add(p);
  1403. p = new SqlParameter("@Group", SqlDbType.NVarChar, 1000);
  1404. p.Value = group;
  1405. paramList.Add(p);
  1406. //定义其他变量
  1407. //string result;
  1408. DataTable dt = new DataTable();
  1409. //执行查询
  1410. if (!DataAccess.GetDataTable(commandText, dt, paramList, out result))
  1411. dt = null;
  1412. return dt;
  1413. }
  1414. /// <summary>
  1415. /// 使用分页存储过程获取符合条件的分页数据表
  1416. /// </summary>
  1417. /// <param name="strConn">数据库连接字符串</param>
  1418. /// <param name="tableName">表名</param>
  1419. /// <param name="primaryKey">主键字段名</param>
  1420. /// <param name="fields">字段列表</param>
  1421. /// <param name="filter">过滤条件(不包含WHERE)</param>
  1422. /// <param name="group">分组字段列表(不包含GROUP BY)</param>
  1423. /// <param name="sort">排序字段(不包含ORDER BY)</param>
  1424. /// <param name="pageNumber">页号</param>
  1425. /// <param name="pageSize">页大小</param>
  1426. /// <returns>返回数据表;如果获取失败,返回null。</returns>
  1427. public static DataTable GetDataTable(string strConn, string tableName, string primaryKey, string fields, string filter, string group, string sort, int pageNumber, int pageSize)
  1428. {
  1429. //查询语句:这里使用分页查询存储过程
  1430. string commandText = "EXECUTE SP_Pagination @Tables,@PrimaryKey,@Sort,@CurrentPage,@PageSize,@Fields,@Filter,@Group";
  1431. //参数列表
  1432. List<SqlParameter> paramList = new List<SqlParameter>(5);
  1433. SqlParameter p;
  1434. p = new SqlParameter("@Tables", SqlDbType.NVarChar, 1000);
  1435. p.Value = tableName;
  1436. paramList.Add(p);
  1437. p = new SqlParameter("@PrimaryKey", SqlDbType.NVarChar, 100);
  1438. p.Value = primaryKey;
  1439. paramList.Add(p);
  1440. p = new SqlParameter("@Sort", SqlDbType.NVarChar, 200);
  1441. p.Value = sort;
  1442. paramList.Add(p);
  1443. p = new SqlParameter("@CurrentPage", SqlDbType.Int, 4);
  1444. p.Value = pageNumber;
  1445. paramList.Add(p);
  1446. p = new SqlParameter("@PageSize", SqlDbType.Int, 4);
  1447. p.Value = pageSize;
  1448. paramList.Add(p);
  1449. p = new SqlParameter("@Fields", SqlDbType.NVarChar, 1000);
  1450. p.Value = fields;
  1451. paramList.Add(p);
  1452. p = new SqlParameter("@Filter", SqlDbType.NVarChar, 1000);
  1453. p.Value = filter;
  1454. paramList.Add(p);
  1455. p = new SqlParameter("@Group", SqlDbType.NVarChar, 1000);
  1456. p.Value = group;
  1457. paramList.Add(p);
  1458. //定义其他变量
  1459. string result;
  1460. DataTable dt = new DataTable();
  1461. //执行查询
  1462. if (!DataAccess.GetDataTable(strConn, commandText, dt, null, out result))
  1463. dt = null;
  1464. return dt;
  1465. }
  1466. /// <summary>
  1467. /// 返回参数列表
  1468. /// </summary>
  1469. /// <returns></returns>
  1470. public static List<SqlParameter> ToParameters(List<List<Object>> plist)
  1471. {
  1472. List<SqlParameter> parameters = new List<SqlParameter>(plist.Count);
  1473. foreach (List<object> value in plist)
  1474. {
  1475. if (value[1] == null)
  1476. {
  1477. SqlParameter p = new SqlParameter("@" + value[0], DBNull.Value);
  1478. parameters.Add(p);
  1479. }
  1480. else
  1481. switch (value[1].GetType().ToString())
  1482. {
  1483. case "System.Guid":
  1484. SqlParameter p = new SqlParameter("@" + value[0], SqlDbType.UniqueIdentifier, 16);
  1485. p.Value = value[1];
  1486. parameters.Add(p);
  1487. break;
  1488. case "System.Int64":
  1489. //p = new SqlParameter("@" + value[0], SqlDbType.Int, 8);
  1490. p = new SqlParameter("@" + value[0], SqlDbType.BigInt);
  1491. p.Value = (Int64)value[1];
  1492. parameters.Add(p);
  1493. break;
  1494. case "System.Int32":
  1495. p = new SqlParameter("@" + value[0], SqlDbType.Int, 4);
  1496. p.Value = (Int32)value[1];
  1497. parameters.Add(p);
  1498. break;
  1499. case "System.Int16":
  1500. p = new SqlParameter("@" + value[0], SqlDbType.Int, 2);
  1501. p.Value = (Int16)value[1];
  1502. parameters.Add(p);
  1503. break;
  1504. case "System.Boolean":
  1505. p = new SqlParameter("@" + value[0], SqlDbType.Bit);
  1506. p.Value = (Boolean)value[1];
  1507. parameters.Add(p);
  1508. break;
  1509. case "System.Char":
  1510. p = new SqlParameter("@" + value[0], SqlDbType.Char);
  1511. p.Value = value[1];
  1512. parameters.Add(p);
  1513. break;
  1514. case "System.String":
  1515. p = new SqlParameter("@" + value[0], SqlDbType.NVarChar);
  1516. p.Value = value[1];
  1517. parameters.Add(p);
  1518. break;
  1519. case "System.DateTime":
  1520. p = new SqlParameter("@" + value[0], SqlDbType.DateTime, 8);
  1521. p.Value = value[1];
  1522. parameters.Add(p);
  1523. break;
  1524. case "System.Decimal":
  1525. p = new SqlParameter("@" + value[0], SqlDbType.Decimal);
  1526. p.Value = value[1];
  1527. parameters.Add(p);
  1528. break;
  1529. case "System.Long":
  1530. p = new SqlParameter("@" + value[0], SqlDbType.BigInt);
  1531. p.Value = (long)value[1];
  1532. parameters.Add(p);
  1533. break;
  1534. }
  1535. }
  1536. return parameters;
  1537. }
  1538. public static bool GetValues(string connectionString, string selectCommandText, ref DataTable dt, SqlParameter[] parameters, out string result)
  1539. {
  1540. bool bSuccessed = false;
  1541. result = "";
  1542. if (selectCommandText == "")
  1543. {
  1544. bSuccessed = false;
  1545. result = "参数错误:SELECT语句不能为空。";
  1546. }
  1547. else
  1548. {
  1549. if (dt == null)
  1550. {
  1551. bSuccessed = false;
  1552. result = "参数错误:dt不能为空。";
  1553. }
  1554. else
  1555. {
  1556. SqlConnection conn = new SqlConnection(connectionString);
  1557. SqlDataAdapter da = new SqlDataAdapter(selectCommandText, conn);
  1558. try
  1559. {
  1560. conn.Open();
  1561. da.SelectCommand.CommandType = CommandType.Text;
  1562. da.SelectCommand.CommandTimeout = 300;
  1563. if (parameters != null && parameters.Length > 0)
  1564. da.SelectCommand.Parameters.AddRange(parameters);
  1565. da.Fill(dt);
  1566. DataTable ds = dt;
  1567. //using (SqlDataReader reader = da.SelectCommand.ExecuteReader())
  1568. //{
  1569. // Console.WriteLine("数据库中有以下用户名:");
  1570. // while (reader.Read()) //循环读取数据库中的数据,直到最后一条记录的最后
  1571. // {
  1572. // //这里需要指定某列的序号
  1573. // Console.WriteLine(reader.GetString(1));
  1574. // //而使用下列的方法GetOrdinal可以直接指定某列的名称,而不用把列序号写死在程序里,跟以上效果一样
  1575. // Console.WriteLine(reader.GetString(reader.GetOrdinal("username")));
  1576. // }
  1577. //}
  1578. bSuccessed = true;
  1579. result = "已经成功的获取了数据。";
  1580. }
  1581. catch (Exception e)
  1582. {
  1583. bSuccessed = false;
  1584. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  1585. }
  1586. finally
  1587. {
  1588. if (da != null && da.SelectCommand != null && da.SelectCommand.Parameters.Count > 0)
  1589. da.SelectCommand.Parameters.Clear();
  1590. if (conn != null)
  1591. {
  1592. if (conn.State != ConnectionState.Closed)
  1593. conn.Close();
  1594. }
  1595. }
  1596. }
  1597. }
  1598. return bSuccessed;
  1599. }
  1600. public static bool GetValues(string selectCommandText, ref DataTable dt, SqlParameter[] parameters, out string result)
  1601. {
  1602. return GetValues(connectionString, selectCommandText, ref dt, parameters, out result);
  1603. }
  1604. public static List<T> GetValues<T>(string selectCommandText, List<SqlParameter> parameters, out string result) where T : new()
  1605. {
  1606. DataTable dt = new DataTable();
  1607. List<T> list = new List<T>();
  1608. GetValues(selectCommandText, ref dt, parameters.ToArray(), out result);
  1609. if (dt != null && dt.Rows.Count > 0)
  1610. {
  1611. list = (List<T>)ModelConvertHelper<T>.ConvertToModel(dt);
  1612. }
  1613. return list;
  1614. }
  1615. /// <summary>
  1616. /// 获取数据库中的某一个值(该方法只返回结果集中的第一个数据)
  1617. /// </summary>
  1618. /// <param name="selectCommandText">SELECT语句</param>
  1619. /// <param name="objValue">返回的数据</param>
  1620. /// <param name="result">返回操作的结果</param>
  1621. /// <returns>返回操作是否成功,如果成功,返回true;否则返回false。</returns>
  1622. public static bool GetOneValue(string connectionString, string selectCommandText, out object objValue, out string result)
  1623. {
  1624. bool bSuccessed = false;
  1625. objValue = null;
  1626. result = "";
  1627. if (selectCommandText == "")
  1628. {
  1629. bSuccessed = false;
  1630. result = "参数错误:SELECT语句不能为空。";
  1631. }
  1632. else
  1633. {
  1634. SqlConnection conn = new SqlConnection(connectionString);
  1635. SqlCommand cmd = new SqlCommand(selectCommandText, conn);
  1636. try
  1637. {
  1638. conn.Open();
  1639. cmd.CommandType = CommandType.Text;
  1640. objValue = cmd.ExecuteScalar();
  1641. bSuccessed = true;
  1642. result = "已经成功的获取了数据。";
  1643. }
  1644. catch (Exception e)
  1645. {
  1646. bSuccessed = false;
  1647. result = "在访问数据时发生错误。\r\nSELECT语句:" + selectCommandText + "\r\n错误描述:" + e.Message + "\r\n错误源:" + e.Source;
  1648. }
  1649. finally
  1650. {
  1651. if (conn != null)
  1652. {
  1653. if (conn.State != ConnectionState.Closed)
  1654. conn.Close();
  1655. }
  1656. }
  1657. }
  1658. return bSuccessed;
  1659. }
  1660. /// <summary>
  1661. /// 得到某个表中符合条件的记录数目
  1662. /// </summary>
  1663. /// <param name="tableName">表名</param>
  1664. /// <param name="filter">筛选条件</param>
  1665. /// <param name="distinct">是否只统计不重复的记录</param>
  1666. /// <param name="result">输出参数,返回错误信息</param>
  1667. /// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1668. public static int GetRowCount(string connectionString, string tableName, string filter, bool distinct, out string result)
  1669. {
  1670. int rowCount = -1;
  1671. string selectCommandText = "SELECT " + (distinct ? "DISTINCT" : "") + " COUNT(*) FROM [" + tableName + "]";
  1672. if (filter != "")
  1673. selectCommandText += " WHERE " + filter;
  1674. object objValue;
  1675. //
  1676. if (DataAccess.GetOneValue(connectionString,selectCommandText, out objValue, out result))
  1677. {
  1678. if (!Convert.IsDBNull(objValue))
  1679. rowCount = (int)objValue;
  1680. }
  1681. return rowCount;
  1682. }
  1683. /// <summary>
  1684. /// 得到某个表中符合条件的记录数目
  1685. /// </summary>
  1686. /// <param name="result">输出参数,返回错误信息</param>
  1687. /// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1688. public static Int64 GetRowCountDefine(string selectCommandText, out string result)
  1689. {
  1690. Int64 rowCount = -1;
  1691. object objValue;
  1692. //
  1693. if (DataAccess.GetOneValue(connectionString, selectCommandText, out objValue, out result))
  1694. {
  1695. if (!Convert.IsDBNull(objValue))
  1696. rowCount = Convert.ToInt64(objValue);
  1697. }
  1698. return rowCount;
  1699. }
  1700. /// <summary>
  1701. /// 得到某个表中符合条件的记录数目
  1702. /// </summary>
  1703. /// <param name="result">输出参数,返回错误信息</param>
  1704. /// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1705. public static Int64 GetRowCountDefine(string selectCommandText,SqlParameter[] param, out string result)
  1706. {
  1707. Int64 rowCount = -1;
  1708. object objValue;
  1709. //
  1710. if (DataAccess.GetOneValue(connectionString, selectCommandText, param, out objValue, out result))
  1711. {
  1712. if (!Convert.IsDBNull(objValue))
  1713. rowCount = Convert.ToInt64(objValue);
  1714. }
  1715. return rowCount;
  1716. }
  1717. public static bool GetValuesV2(string selectCommandText, ref DataTable dt, SqlParameter[] parameters, out string result)
  1718. {
  1719. return GetValues("Data Source=219.128.77.96;Initial Catalog=Mall;Persist Security Info=True;User ID=caixukun;Password=cxk@membercenter!@#;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30", selectCommandText, ref dt, parameters, out result);
  1720. }
  1721. public static Int64 GetRowCountDefineV2(string selectCommandText, SqlParameter[] param, out string result)
  1722. {
  1723. Int64 rowCount = -1;
  1724. object objValue;
  1725. //
  1726. if (DataAccess.GetOneValue("Data Source=219.128.77.96;Initial Catalog=Mall;Persist Security Info=True;User ID=caixukun;Password=cxk@membercenter!@#;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30", selectCommandText, param, out objValue, out result))
  1727. {
  1728. if (!Convert.IsDBNull(objValue))
  1729. rowCount = Convert.ToInt64(objValue);
  1730. }
  1731. return rowCount;
  1732. }
  1733. public static int ExecuteCommandv2(string commandText, List<SqlParameter> parameters, out string result)
  1734. {
  1735. return ExecuteCommand("Data Source=219.128.77.96;Initial Catalog=Mall;Persist Security Info=True;User ID=caixukun;Password=cxk@membercenter!@#;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30", commandText, parameters, out result);
  1736. }
  1737. /// <summary>
  1738. /// 得到某个表中符合条件的记录数目
  1739. /// </summary>
  1740. /// <param name="result">输出参数,返回错误信息</param>
  1741. /// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1742. public static int GetRowCountDefine(string connectionString, string selectCommandText, out string result)
  1743. {
  1744. int rowCount = -1;
  1745. object objValue;
  1746. //
  1747. if (DataAccess.GetOneValue(connectionString, selectCommandText, out objValue, out result))
  1748. {
  1749. if (!Convert.IsDBNull(objValue))
  1750. rowCount = (int)objValue;
  1751. }
  1752. return rowCount;
  1753. }
  1754. /// <summary>
  1755. /// 得到某个表中符合条件的记录数目
  1756. /// </summary>
  1757. /// <param name="result">输出参数,返回错误信息</param>
  1758. /// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1759. public static int GetRowCountDefine(string connectionString, string selectCommandText, SqlParameter[] param, out string result)
  1760. {
  1761. int rowCount = -1;
  1762. object objValue;
  1763. //
  1764. if (DataAccess.GetOneValue(connectionString, selectCommandText, param, out objValue, out result))
  1765. {
  1766. if (!Convert.IsDBNull(objValue))
  1767. rowCount = (int)objValue;
  1768. }
  1769. return rowCount;
  1770. }
  1771. ///// <summary>
  1772. ///// 得到某个表中符合条件的记录数目
  1773. ///// </summary>
  1774. ///// <param name="tableName">表名</param>
  1775. ///// <param name="filter">筛选条件</param>
  1776. ///// <param name="distinct">是否只统计不重复的记录</param>
  1777. ///// <param name="result">输出参数,返回错误信息</param>
  1778. ///// <returns>返回记录数目;如果发生错误,返回-1。</returns>
  1779. //public static Int64 GetRowCountDefine(string selectCommandText, SqlParameter[] parameters, out string result)
  1780. //{
  1781. // Int64 rowCount = -1;
  1782. // object objValue;
  1783. // //
  1784. // if (DataAccess.GetOneValue(connectionString, selectCommandText, parameters,out objValue, out result))
  1785. // {
  1786. // if (!Convert.IsDBNull(objValue))
  1787. // rowCount = Convert.ToInt64(objValue);
  1788. // }
  1789. // return rowCount;
  1790. //}
  1791. }
  1792. /// <summary>
  1793. /// BulkCopy操作的事务类型
  1794. /// </summary>
  1795. public enum BulkCopyTransType
  1796. {
  1797. None, //不使用事务
  1798. Internal, //使用内部事务:对每一个复制批次使用事务
  1799. External //使用外部事务:对整个复制使用批次
  1800. }
  1801. /// <summary>
  1802. /// DataAccessCommand命令:包括命令语句和命令所用的参数列表。
  1803. /// </summary>
  1804. public class DataAccessCommand
  1805. {
  1806. //公共字段
  1807. public string CommandText; //命令语句
  1808. public List<SqlParameter> ParamList; //参数列表
  1809. public CommandType CommandType; //命令类型
  1810. public bool RecordsAffectedLargeThanZeroIsSuccessful; //执行命令所影响的记录数大于0才成功吗?
  1811. //构造函数
  1812. public DataAccessCommand(string commandText, List<SqlParameter> paramList, CommandType commandType, bool recordsAffectedLargeThanZeroIsSuccessful)
  1813. {
  1814. CommandText = commandText;
  1815. ParamList = paramList;
  1816. this.CommandType = commandType;
  1817. RecordsAffectedLargeThanZeroIsSuccessful = recordsAffectedLargeThanZeroIsSuccessful;
  1818. }
  1819. public DataAccessCommand(string commandText, List<SqlParameter> paramList)
  1820. : this(commandText, paramList, CommandType.Text, true)
  1821. {
  1822. }
  1823. public DataAccessCommand(string commandText)
  1824. : this(commandText, null)
  1825. {
  1826. }
  1827. }
  1828. }