af7e7c3ec3a35016b3b321996ff70a3ceae292a3.svn-base 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Data.OleDb;
  6. using System.Data.OracleClient;
  7. using System.Xml;
  8. using System.IO;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Runtime.Remoting.Lifetime;
  12. using System.Data.Common;
  13. namespace STMes
  14. {
  15. /// <summary>
  16. /// DBProxy 的摘要说明。
  17. /// </summary>
  18. public class DBProxy : MarshalByRefObject, IDisposable
  19. {
  20. private static Hashtable _dataProviderNameList = Hashtable.Synchronized(new Hashtable(5));
  21. private static Hashtable _dataProviderTypeList = Hashtable.Synchronized(new Hashtable(5));
  22. private CoreWriteLogFile cwl = new CoreWriteLogFile();
  23. public DBProxy()
  24. {
  25. AddDataProvider(new DataProvider.SqlDataProvider());
  26. AddDataProvider(new DataProvider.OleDbDataProvider());
  27. AddDataProvider(new DataProvider.OdbcDataProvider());
  28. AddDataProvider(new DataProvider.OracleDataProvider());
  29. AddDataProvider(new DataProvider.ODPDataProvider());
  30. Console.WriteLine("创建数据库代理DBProxy...\n");
  31. //从数据库配置文件中读取数据库连接信息
  32. ReadDbConnectionInfo(@"MesDB.cfg");
  33. //预先创建所有连接...
  34. CreateAllConnections();
  35. _connLifeTimeA = int.Parse(System.Configuration.ConfigurationManager.AppSettings["connLifeTimeA"]);
  36. _connLifeTimeB = int.Parse(System.Configuration.ConfigurationManager.AppSettings["connLifeTimeB"]);
  37. _connLifeTimeC = int.Parse(System.Configuration.ConfigurationManager.AppSettings["connLifeTimeC"]);
  38. //创建一个线程,用来定时清除已经断开连接的DBManager
  39. #region 原来代码 maliang 2011-10-19
  40. /*
  41. _checkConnThread = new Thread(new ThreadStart(CheckConnActive));
  42. _checkConnThread.Start();
  43. _fixConnThread = new Thread(new ThreadStart(FixConnections));
  44. _fixConnThread.Start();
  45. */
  46. #endregion
  47. #region 变更代码 maliang 2011-10-19
  48. _ConnectionsListenerThread = new Thread(new ThreadStart(ConnectionsListener));
  49. _ConnectionsListenerThread.Start();
  50. #endregion
  51. _init = true;
  52. _reference++;
  53. }
  54. //没有显示调用Dispose()时调用
  55. ~DBProxy()
  56. {
  57. Dispose(false);
  58. }
  59. #region IDisposable 成员
  60. public void Dispose()
  61. {
  62. Dispose(true);
  63. GC.Collect();
  64. }
  65. protected virtual void Dispose(bool disposing)
  66. {
  67. _reference--;
  68. if (disposing == true)
  69. {
  70. //释放受控资源
  71. }
  72. //释放未受控资源
  73. //回滚所有DBManager事务
  74. foreach (DictionaryEntry it in _transList)
  75. {
  76. TransInfo info = (TransInfo)it.Value;
  77. IDbConnection conn = info.dbConnection;
  78. try
  79. {
  80. info.dbTransaction.Rollback();
  81. }
  82. catch (Exception)
  83. {
  84. }
  85. if (_reference == 0)
  86. {
  87. //关闭数据库连接
  88. foreach (DictionaryEntry it1 in _connList)
  89. {
  90. Hashtable ht1 = (Hashtable)it1.Value;
  91. foreach (DictionaryEntry it2 in ht1)
  92. {
  93. Hashtable ht2 = (Hashtable)it2.Value;
  94. foreach (DictionaryEntry it3 in ht2)
  95. {
  96. ArrayList arr = it3.Value as ArrayList;
  97. foreach (object obj in arr)
  98. {
  99. IDbConnection db = (IDbConnection)obj;
  100. db.Close();
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. // 关闭线程
  108. try
  109. {
  110. if (_checkConnThread != null)
  111. _checkConnThread.Abort();
  112. }
  113. catch (Exception) { }
  114. try
  115. {
  116. if (_fixConnThread != null)
  117. _fixConnThread.Abort();
  118. }
  119. catch (Exception) { }
  120. }
  121. #endregion
  122. //返回由DBProxy为每个DBManager对象分配的ID
  123. public long GetDBManagerID()
  124. {
  125. lock (_dbManagerID)
  126. {
  127. ++_dbManagerID[0];
  128. if (_dbManagerID[0] > 3000000)
  129. _dbManagerID[0] = 1;
  130. return _dbManagerID[0];
  131. }
  132. }
  133. //设置DBManager对应的数据库信息
  134. public void SetDbInfo(long dbManagerID, string dbName, string priority)
  135. {
  136. if (_dbList.Contains(dbManagerID))
  137. {
  138. _dbList.Remove(dbManagerID);
  139. }
  140. ArrayList arr = new ArrayList();
  141. arr.Add(dbName);
  142. arr.Add(priority);
  143. _dbList.Add(dbManagerID, arr);
  144. }
  145. //获取数据库类型,用字符串"SQLServer","Oracle", "OleDb"分别表示不同类型的数据库
  146. public string GetDbType(string name, out string err)
  147. {
  148. err = "";
  149. //未找到该数据库对应的配置信息
  150. if (_dbConfigInfo.Contains(name) == false)
  151. {
  152. err = "未找到指定的数据库";
  153. return "";
  154. }
  155. //获取该数据库对应优先级的配置信息
  156. DBConnectionInfo dbInfo = (DBConnectionInfo)_dbConfigInfo[name];
  157. return dbInfo.dbType;
  158. }
  159. #region"数据操作"
  160. //执行带参数的SQL语句,返回DataSet
  161. //事务处理
  162. public DataSet ExecuteQuery(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err)
  163. {
  164. return ExecuteQueryBase(dbName, priority, guid, Sql, cmdType, Params, Values, out err);
  165. }
  166. private DataSet ExecuteQueryBase(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err)
  167. {
  168. err = "";
  169. DataSet ds = new DataSet();
  170. //获取数据库连接...
  171. IDbConnection conn = null;
  172. IDbTransaction trans = null;
  173. if (guid != null)
  174. {
  175. //如果是事务
  176. if (_transList.Contains(guid))
  177. {
  178. TransInfo info = (TransInfo)_transList[guid];
  179. conn = info.dbConnection;
  180. trans = info.dbTransaction;
  181. //如果事务已经失效,则返回
  182. if (trans.Connection == null)
  183. {
  184. err = "事务已经失效!";
  185. return ds;
  186. }
  187. }
  188. else
  189. {
  190. err = "未找到请求的事务!";
  191. cwl.WriteLog("未找到请求的事务", LogInfoLevel.Error, "ExecuteQuery", "ExecuteQueryBase");
  192. return ds;
  193. }
  194. }
  195. if (conn == null)
  196. {
  197. err = "获取数据库连接失败!";
  198. cwl.WriteLog("获取数据库连接失败", LogInfoLevel.Error, "ExecuteQuery", "ExecuteQueryBase");
  199. return ds;
  200. }
  201. try
  202. {
  203. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  204. IDbCommand command = conn.CreateCommand();
  205. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  206. ((IDbDataAdapter)adapter).SelectCommand = command;
  207. command.CommandText = Sql;
  208. command.CommandType = cmdType;
  209. //设置事务
  210. if (trans != null)
  211. {
  212. command.Transaction = trans;
  213. }
  214. //给所有的参数赋值
  215. if (Params != null && Values != null)
  216. {
  217. for (int i = 0; i < Params.Length; i++)
  218. {
  219. command.Parameters.Add(Params[i]);
  220. ((IDbDataParameter)command.Parameters[i]).Value = Values[i];
  221. }
  222. }
  223. adapter.Fill(ds);
  224. }
  225. catch (Exception ex)
  226. {
  227. err = ex.Message;
  228. cwl.WriteLog(err, LogInfoLevel.Error, "ExecuteQuery", "ExecuteQueryBase");
  229. }
  230. return ds;
  231. }
  232. public DataSet ExecuteQuery(string dbName, string priority, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err)
  233. {
  234. return this.ExecuteQuery(dbName, priority, Sql, cmdType, Params, Values, out err, false);
  235. }
  236. //非事务处理
  237. public DataSet ExecuteQuery(string dbName, string priority, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err, bool ifDBLink)
  238. {
  239. if (ifDBLink)
  240. {
  241. return ExecuteQueryDBLink(dbName, priority, Sql, cmdType, Params, Values, out err);
  242. }
  243. else
  244. {
  245. err = "";
  246. DataSet ds = new DataSet();
  247. //获取数据库连接...
  248. IDbConnection conn = GetDbConnection(dbName, priority, Sql);
  249. if (conn == null)
  250. {
  251. err = "获取数据库连接失败!";
  252. return ds;
  253. }
  254. try
  255. {
  256. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  257. IDbCommand command = conn.CreateCommand();
  258. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  259. ((IDbDataAdapter)adapter).SelectCommand = command;
  260. command.CommandText = Sql;
  261. command.CommandType = cmdType;
  262. //给所有的参数赋值
  263. if (Params != null && Values != null)
  264. {
  265. for (int i = 0; i < Params.Length; i++)
  266. {
  267. command.Parameters.Add(Params[i]);
  268. ((IDbDataParameter)command.Parameters[i]).Value = Values[i];
  269. }
  270. }
  271. adapter.Fill(ds);
  272. }
  273. catch (Exception ex)
  274. {
  275. err = ex.Message;
  276. }
  277. finally
  278. {
  279. ReleaseDbConnection(dbName, priority, conn, Sql);
  280. }
  281. return ds;
  282. }
  283. }
  284. //非事务处理
  285. private DataSet ExecuteQueryDBLink(string dbName, string priority, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err)
  286. {
  287. err = "";
  288. DataSet ds = new DataSet();
  289. //获取数据库连接...
  290. IDbConnection conn = null;
  291. IDbTransaction trans = null;
  292. conn = GetDbConnection(dbName, priority, Sql);
  293. if (conn == null)
  294. {
  295. err = "获取数据库连接失败!";
  296. return ds;
  297. }
  298. try
  299. {
  300. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  301. IDbCommand command = conn.CreateCommand();
  302. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  303. ((IDbDataAdapter)adapter).SelectCommand = command;
  304. trans = conn.BeginTransaction();
  305. command.CommandText = Sql;
  306. command.CommandType = cmdType;
  307. command.Transaction = trans;
  308. //给所有的参数赋值
  309. if (Params != null && Values != null)
  310. {
  311. for (int i = 0; i < Params.Length; i++)
  312. {
  313. command.Parameters.Add(Params[i]);
  314. ((IDbDataParameter)command.Parameters[i]).Value = Values[i];
  315. }
  316. }
  317. adapter.Fill(ds);
  318. command.Transaction.Commit();
  319. command.CommandText = "begin dbms_session.close_database_link('XGCX');end;";
  320. command.ExecuteNonQuery();
  321. }
  322. catch (Exception ex)
  323. {
  324. err = ex.Message;
  325. if (trans != null)
  326. {
  327. trans.Rollback();
  328. }
  329. }
  330. finally
  331. {
  332. ReleaseDbConnection(dbName, priority, conn, Sql);
  333. }
  334. return ds;
  335. }
  336. //执行带参数的SQL语句,返回DataSet
  337. //事务处理
  338. public DataSet ExecuteQuery(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, object[] Values, ref ArrayList OutputValues, out string err)
  339. {
  340. err = "";
  341. DataSet ds = null;
  342. ds = new DataSet();
  343. //获取数据库连接...
  344. IDbConnection conn = null;
  345. IDbTransaction trans = null;
  346. //如果是事务
  347. if (_transList.Contains(guid))
  348. {
  349. //Console.WriteLine("事务:执行数据库操作{0}...\n", Sql);
  350. TransInfo info = (TransInfo)_transList[guid];
  351. conn = info.dbConnection;
  352. trans = info.dbTransaction;
  353. //如果事务已经失效,则返回
  354. if (trans.Connection == null)
  355. {
  356. err = "事务已经失效!";
  357. return ds;
  358. }
  359. }
  360. else
  361. {
  362. err = "未找到请求的事务!";
  363. return ds;
  364. }
  365. if (conn == null)
  366. {
  367. err = "获取数据库连接失败!";
  368. return ds;
  369. }
  370. try
  371. {
  372. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  373. IDbCommand command = conn.CreateCommand();
  374. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  375. ((IDbDataAdapter)adapter).SelectCommand = command;
  376. command.CommandText = Sql;
  377. command.CommandType = cmdType;
  378. //设置事务
  379. if (trans != null)
  380. {
  381. command.Transaction = trans;
  382. }
  383. //给所有的参数赋值
  384. if (ParameterNames != null)
  385. {
  386. for (int i = 0; i < ParameterNames.Length; i++)
  387. {
  388. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  389. parameter.ParameterName = ParameterNames[i];
  390. if (conn is System.Data.OracleClient.OracleConnection)
  391. {
  392. try
  393. {
  394. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  395. }
  396. catch
  397. {
  398. parameter.DbType = DbTypes[i];
  399. }
  400. }
  401. else
  402. {
  403. parameter.DbType = DbTypes[i];
  404. }
  405. if (parameter.Direction != System.Data.ParameterDirection.Input)
  406. parameter.Size = 200;
  407. parameter.Value = Values[i];
  408. parameter.Direction = Directions[i];
  409. // parameter.IsNullable = IsNullables[i];
  410. // parameter.SourceColumn = SourceColumns[i];
  411. // parameter.SourceVersion = SourceVersions[i];
  412. command.Parameters.Add(parameter);
  413. }
  414. }
  415. adapter.Fill(ds);
  416. //返回输出参数值
  417. if (OutputValues != null)
  418. {
  419. for (int k = 0; k < command.Parameters.Count; k++)
  420. {
  421. if (((IDbDataParameter)command.Parameters[k]).Direction != ParameterDirection.Input)
  422. {
  423. OutputValues.Add(((IDbDataParameter)command.Parameters[k]).Value);
  424. }
  425. }
  426. }
  427. }
  428. catch (Exception ex)
  429. {
  430. err = ex.Message;
  431. trans.Rollback();
  432. }
  433. return ds;
  434. }
  435. public DataSet ExecuteQuery(string dbName, string priority, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, object[] Values, ref ArrayList OutputValues, out string err)
  436. {
  437. return ExecuteQuery(dbName, priority, Sql, cmdType, ParameterNames, DbTypes, Directions, IsNullables, SourceColumns, SourceVersions, Values, ref OutputValues, out err, false);
  438. }
  439. //非事务处理
  440. public DataSet ExecuteQuery(string dbName, string priority, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, object[] Values, ref ArrayList OutputValues, out string err, bool ifDBLink)
  441. {
  442. err = "";
  443. DataSet ds = null;
  444. ds = new DataSet();
  445. //获取数据库连接...
  446. IDbConnection conn = null;
  447. //获取数据库连接
  448. conn = GetDbConnection(dbName, priority, Sql);
  449. if (conn == null)
  450. {
  451. err = "获取数据库连接失败!";
  452. return ds;
  453. }
  454. try
  455. {
  456. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  457. IDbCommand command = conn.CreateCommand();
  458. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  459. ((IDbDataAdapter)adapter).SelectCommand = command;
  460. command.CommandText = Sql;
  461. command.CommandType = cmdType;
  462. //给所有的参数赋值
  463. if (ParameterNames != null)
  464. {
  465. for (int i = 0; i < ParameterNames.Length; i++)
  466. {
  467. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  468. parameter.ParameterName = ParameterNames[i];
  469. if (conn is System.Data.OracleClient.OracleConnection)
  470. {
  471. try
  472. {
  473. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  474. }
  475. catch
  476. {
  477. parameter.DbType = DbTypes[i];
  478. }
  479. }
  480. else
  481. {
  482. parameter.DbType = DbTypes[i];
  483. }
  484. if (parameter.Direction != System.Data.ParameterDirection.Input)
  485. parameter.Size = 200; parameter.Value = Values[i];
  486. parameter.Direction = Directions[i];
  487. // parameter.IsNullable = IsNullables[i];
  488. // parameter.SourceColumn = SourceColumns[i];
  489. // parameter.SourceVersion = SourceVersions[i];
  490. command.Parameters.Add(parameter);
  491. }
  492. }
  493. adapter.Fill(ds);
  494. //返回输出参数值
  495. if (OutputValues != null)
  496. {
  497. for (int k = 0; k < command.Parameters.Count; k++)
  498. {
  499. if (((IDbDataParameter)command.Parameters[k]).Direction != ParameterDirection.Input)
  500. {
  501. OutputValues.Add(((IDbDataParameter)command.Parameters[k]).Value);
  502. }
  503. }
  504. }
  505. }
  506. catch (Exception ex)
  507. {
  508. err = ex.Message;
  509. }
  510. finally
  511. {
  512. //释放连接
  513. ReleaseDbConnection(dbName, priority, conn, Sql);
  514. }
  515. return ds;
  516. }
  517. //不通过DBServer时使用
  518. //事务处理
  519. public int ExecuteNonQuery_NoDBSever(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, IDataParameter[] Params, out string err)
  520. {
  521. err = "";
  522. int lines = -1;//受影响的行数
  523. //获取数据库连接...
  524. IDbConnection conn = null;
  525. IDbTransaction trans = null;
  526. //如果是事务
  527. if (_transList.Contains(guid))
  528. {
  529. //Console.WriteLine("事务:执行数据库操作{0}...\n", Sql);
  530. TransInfo info = (TransInfo)_transList[guid];
  531. conn = info.dbConnection;
  532. trans = info.dbTransaction;
  533. //如果事务已经失效,则返回
  534. if (trans.Connection == null)
  535. {
  536. err = "事务已经失效!";
  537. return lines;
  538. }
  539. }
  540. else
  541. {
  542. err = "未找到请求的事务!";
  543. return lines;
  544. }
  545. if (conn == null)
  546. {
  547. err = "获取数据库连接失败!";
  548. return lines;
  549. }
  550. try
  551. {
  552. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  553. IDbCommand command = conn.CreateCommand();
  554. command.CommandText = Sql;
  555. command.CommandType = cmdType;
  556. command.Transaction = trans;
  557. //给所有的参数赋值
  558. if (Params != null)
  559. {
  560. for (int i = 0; i < Params.Length; i++)
  561. {
  562. command.Parameters.Add(Params[i]);
  563. }
  564. }
  565. lines = command.ExecuteNonQuery();
  566. }
  567. catch (Exception ex)
  568. {
  569. trans.Rollback();
  570. err = ex.Message;
  571. }
  572. return lines;
  573. }
  574. //非事务处理
  575. public int ExecuteNonQuery_NoDBSever(string dbName, string priority, string Sql, CommandType cmdType, IDataParameter[] Params, out string err)
  576. {
  577. err = "";
  578. int lines = -1;//受影响的行数
  579. //获取数据库连接...
  580. IDbConnection conn = null;
  581. //获取数据库连接
  582. conn = GetDbConnection(dbName, priority, Sql);
  583. if (conn == null)
  584. {
  585. err = "获取数据库连接失败!";
  586. return lines;
  587. }
  588. try
  589. {
  590. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  591. IDbCommand command = conn.CreateCommand();
  592. command.CommandText = Sql;
  593. command.CommandType = cmdType;
  594. //给所有的参数赋值
  595. if (Params != null)
  596. {
  597. for (int i = 0; i < Params.Length; i++)
  598. {
  599. command.Parameters.Add(Params[i]);
  600. }
  601. }
  602. lines = command.ExecuteNonQuery();
  603. }
  604. catch (Exception ex)
  605. {
  606. err = ex.Message;
  607. }
  608. finally
  609. {
  610. ReleaseDbConnection(dbName, priority, conn, Sql);
  611. }
  612. return lines;
  613. }
  614. //执行带参数的SQL语句,返回受影响的行数
  615. //事务处理
  616. public int ExecuteNonQuery(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err)
  617. {
  618. err = "";
  619. int lines = -1;//受影响的行数
  620. //获取数据库连接...
  621. IDbConnection conn = null;
  622. IDbTransaction trans = null;
  623. //如果是事务
  624. if (_transList.Contains(guid))
  625. {
  626. //Console.WriteLine("事务:执行数据库操作{0}...\n", Sql);
  627. TransInfo info = (TransInfo)_transList[guid];
  628. conn = info.dbConnection;
  629. trans = info.dbTransaction;
  630. //如果事务已经失效,则返回
  631. if (trans.Connection == null)
  632. {
  633. err = "事务已经失效!";
  634. return lines;
  635. }
  636. }
  637. else
  638. {
  639. err = "未找到请求的事务!";
  640. return lines;
  641. }
  642. if (conn == null)
  643. {
  644. err = "获取数据库连接失败!";
  645. return lines;
  646. }
  647. try
  648. {
  649. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  650. IDbCommand command = conn.CreateCommand();
  651. command.CommandText = Sql;
  652. command.CommandType = cmdType;
  653. command.Transaction = trans;
  654. //给所有的参数赋值
  655. if (Params != null && Values != null)
  656. {
  657. for (int i = 0; i < Params.Length; i++)
  658. {
  659. command.Parameters.Add(Params[i]);
  660. ((IDbDataParameter)command.Parameters[i]).Value = Values[i];
  661. }
  662. }
  663. lines = command.ExecuteNonQuery();
  664. }
  665. catch (Exception ex)
  666. {
  667. err = ex.Message;
  668. trans.Rollback();
  669. }
  670. return lines;
  671. }
  672. //非事务处理
  673. public int ExecuteNonQuery(string dbName, string priority, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, out string err)
  674. {
  675. err = "";
  676. int lines = -1;//受影响的行数
  677. //获取数据库连接...
  678. IDbConnection conn = null;
  679. //获取数据库连接
  680. conn = GetDbConnection(dbName, priority, Sql);
  681. if (conn == null)
  682. {
  683. err = "获取数据库连接失败!";
  684. return lines;
  685. }
  686. try
  687. {
  688. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  689. IDbCommand command = conn.CreateCommand();
  690. command.CommandText = Sql;
  691. command.CommandType = cmdType;
  692. //给所有的参数赋值
  693. if (Params != null && Values != null)
  694. {
  695. for (int i = 0; i < Params.Length; i++)
  696. {
  697. command.Parameters.Add(Params[i]);
  698. ((IDbDataParameter)command.Parameters[i]).Value = Values[i];
  699. }
  700. }
  701. lines = command.ExecuteNonQuery();
  702. }
  703. catch (Exception ex)
  704. {
  705. err = ex.Message;
  706. }
  707. finally
  708. {
  709. //释放连接
  710. ReleaseDbConnection(dbName, priority, conn, Sql);
  711. }
  712. return lines;
  713. }
  714. //执行带参数的SQL语句,返回受影响的行数
  715. // 事务操作
  716. public int ExecuteNonQuery(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, object[] Values, ref ArrayList OutputValues, out string err)
  717. {
  718. err = "";
  719. int lines = -1;//受影响的行数
  720. //获取数据库连接...
  721. IDbConnection conn = null;
  722. IDbTransaction trans = null;
  723. //如果是事务
  724. if (_transList.Contains(guid))
  725. {
  726. TransInfo info = (TransInfo)_transList[guid];
  727. conn = info.dbConnection;
  728. trans = info.dbTransaction;
  729. //如果事务已经失效,则返回
  730. if (trans.Connection == null)
  731. {
  732. err = "事务已经失效!";
  733. return lines;
  734. }
  735. }
  736. else
  737. {
  738. err = "未找到请求的事务!";
  739. return lines;
  740. }
  741. if (conn == null)
  742. {
  743. err = "获取数据库连接失败!";
  744. return lines;
  745. }
  746. showsql(Sql);
  747. try
  748. {
  749. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  750. IDbCommand command = conn.CreateCommand();
  751. command.CommandText = Sql;
  752. command.CommandType = cmdType;
  753. //设置事务
  754. command.Transaction = trans;
  755. if (ParameterNames != null)
  756. {
  757. for (int i = 0; i < ParameterNames.Length; i++)
  758. {
  759. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  760. parameter.ParameterName = ParameterNames[i];
  761. parameter.Value = Values[i];
  762. parameter.Direction = Directions[i];
  763. if (conn is System.Data.OracleClient.OracleConnection)
  764. {
  765. try
  766. {
  767. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  768. }
  769. catch
  770. {
  771. parameter.DbType = DbTypes[i];
  772. }
  773. }
  774. else
  775. {
  776. parameter.DbType = DbTypes[i];
  777. }
  778. if (parameter.Direction != System.Data.ParameterDirection.Input)
  779. parameter.Size = 200;
  780. parameter.Direction = Directions[i];
  781. command.Parameters.Add(parameter);
  782. }
  783. }
  784. lines = command.ExecuteNonQuery();
  785. //返回输出参数值
  786. if (OutputValues != null)
  787. {
  788. for (int k = 0; k < command.Parameters.Count; k++)
  789. {
  790. if (((IDbDataParameter)command.Parameters[k]).Direction != ParameterDirection.Input)
  791. {
  792. OutputValues.Add(((IDbDataParameter)command.Parameters[k]).Value);
  793. }
  794. }
  795. }
  796. }
  797. catch (System.Exception ex)
  798. {
  799. err = ex.Message;
  800. trans.Rollback();
  801. }
  802. return lines;
  803. }
  804. //非事务处理
  805. public int ExecuteNonQuery(string dbName, string priority, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, object[] Values, ref ArrayList OutputValues, out string err)
  806. {
  807. err = "";
  808. int lines = -1;//受影响的行数
  809. //获取数据库连接...
  810. IDbConnection conn = null;
  811. //获取数据库连接
  812. conn = GetDbConnection(dbName, priority, Sql);
  813. if (conn == null)
  814. {
  815. err = "获取数据库连接失败!";
  816. return lines;
  817. }
  818. try
  819. {
  820. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  821. IDbCommand command = conn.CreateCommand();
  822. command.CommandText = Sql;
  823. command.CommandType = cmdType;
  824. //给所有的参数赋值
  825. if (ParameterNames != null)
  826. {
  827. for (int i = 0; i < ParameterNames.Length; i++)
  828. {
  829. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  830. parameter.ParameterName = ParameterNames[i];
  831. parameter.Value = Values[i];
  832. parameter.Direction = Directions[i];
  833. if (conn is System.Data.OracleClient.OracleConnection)
  834. {
  835. try
  836. {
  837. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  838. }
  839. catch
  840. {
  841. parameter.DbType = DbTypes[i];
  842. }
  843. }
  844. else
  845. {
  846. parameter.DbType = DbTypes[i];
  847. }
  848. if (parameter.Direction != System.Data.ParameterDirection.Input)
  849. parameter.Size = 200;
  850. parameter.Direction = Directions[i];
  851. command.Parameters.Add(parameter);
  852. }
  853. }
  854. lines = command.ExecuteNonQuery();
  855. //返回输出参数值
  856. if (OutputValues != null)
  857. {
  858. for (int k = 0; k < command.Parameters.Count; k++)
  859. {
  860. if (((IDbDataParameter)command.Parameters[k]).Direction != ParameterDirection.Input)
  861. {
  862. OutputValues.Add(((IDbDataParameter)command.Parameters[k]).Value);
  863. }
  864. }
  865. }
  866. }
  867. catch (Exception ex)
  868. {
  869. err = ex.Message;
  870. }
  871. finally
  872. {
  873. //释放连接
  874. ReleaseDbConnection(dbName, priority, conn, Sql);
  875. }
  876. return lines;
  877. }
  878. //事务处理
  879. public int ExecuteNonQuery(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, int[] Sizes, object[] Values, ref ArrayList OutputValues, out string err)
  880. {
  881. err = "";
  882. int lines = -1;//受影响的行数
  883. //获取数据库连接...
  884. IDbConnection conn = null;
  885. IDbTransaction trans = null;
  886. //如果是事务
  887. if (_transList.Contains(guid))
  888. {
  889. //Console.WriteLine("事务:执行数据库操作{0}...\n", Sql);
  890. TransInfo info = (TransInfo)_transList[guid];
  891. conn = info.dbConnection;
  892. trans = info.dbTransaction;
  893. //如果事务已经失效,则返回
  894. if (trans.Connection == null)
  895. {
  896. err = "事务已经失效!";
  897. return lines;
  898. }
  899. }
  900. else
  901. {
  902. err = "未找到请求的事务!";
  903. return lines;
  904. }
  905. if (conn == null)
  906. {
  907. err = "获取数据库连接失败!";
  908. return lines;
  909. }
  910. try
  911. {
  912. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  913. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  914. IDbCommand command = conn.CreateCommand();
  915. command.CommandText = Sql;
  916. command.CommandType = cmdType;
  917. command.Transaction = trans;
  918. //给所有的参数赋值
  919. if (ParameterNames != null && DbTypes != null && Values != null)
  920. {
  921. for (int i = 0; i < ParameterNames.Length; i++)
  922. {
  923. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  924. parameter.ParameterName = ParameterNames[i];
  925. if (conn is System.Data.OracleClient.OracleConnection)
  926. {
  927. try
  928. {
  929. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  930. }
  931. catch
  932. {
  933. parameter.DbType = DbTypes[i];
  934. }
  935. }
  936. else
  937. {
  938. parameter.DbType = DbTypes[i];
  939. }
  940. parameter.Direction = Directions[i];
  941. if (parameter.Direction != System.Data.ParameterDirection.Input)
  942. parameter.Size = 200;
  943. parameter.Direction = Directions[i];
  944. parameter.Value = Values[i];
  945. command.Parameters.Add(parameter);
  946. }
  947. }
  948. lines = command.ExecuteNonQuery();
  949. //返回输出参数值
  950. if (OutputValues != null)
  951. {
  952. for (int k = 0; k < command.Parameters.Count; k++)
  953. {
  954. if (((IDbDataParameter)command.Parameters[k]).Direction != ParameterDirection.Input)
  955. {
  956. OutputValues.Add(((IDbDataParameter)command.Parameters[k]).Value);
  957. }
  958. }
  959. }
  960. }
  961. catch (Exception ex)
  962. {
  963. err = ex.Message;
  964. trans.Rollback();
  965. }
  966. return lines;
  967. }
  968. //非事务处理
  969. public int ExecuteNonQuery(string dbName, string priority, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, ParameterDirection[] Directions, bool[] IsNullables, string[] SourceColumns, DataRowVersion[] SourceVersions, int[] Sizes, object[] Values, ref ArrayList OutputValues, out string err)
  970. {
  971. err = "";
  972. int lines = -1;//受影响的行数
  973. //获取数据库连接...
  974. IDbConnection conn = null;
  975. //获取数据库连接
  976. conn = GetDbConnection(dbName, priority, Sql);
  977. if (conn == null)
  978. {
  979. err = "获取数据库连接失败!";
  980. return lines;
  981. }
  982. showsql(Sql);
  983. try
  984. {
  985. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  986. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  987. IDbCommand command = conn.CreateCommand();
  988. command.CommandText = Sql;
  989. command.CommandType = cmdType;
  990. //给所有的参数赋值
  991. if (ParameterNames != null && DbTypes != null && Values != null)
  992. {
  993. for (int i = 0; i < ParameterNames.Length; i++)
  994. {
  995. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  996. parameter.ParameterName = ParameterNames[i];
  997. if (conn is System.Data.OracleClient.OracleConnection)
  998. {
  999. try
  1000. {
  1001. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  1002. }
  1003. catch
  1004. {
  1005. parameter.DbType = DbTypes[i];
  1006. }
  1007. }
  1008. else
  1009. {
  1010. parameter.DbType = DbTypes[i];
  1011. }
  1012. parameter.Direction = Directions[i];
  1013. parameter.Size = Sizes[i];
  1014. parameter.Value = Values[i];
  1015. command.Parameters.Add(parameter);
  1016. }
  1017. }
  1018. lines = command.ExecuteNonQuery();
  1019. //返回输出参数值
  1020. if (OutputValues != null)
  1021. {
  1022. for (int k = 0; k < command.Parameters.Count; k++)
  1023. {
  1024. if (((IDbDataParameter)command.Parameters[k]).Direction != ParameterDirection.Input)
  1025. {
  1026. OutputValues.Add(((IDbDataParameter)command.Parameters[k]).Value);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. catch (Exception ex)
  1032. {
  1033. err = ex.Message;
  1034. }
  1035. finally
  1036. {
  1037. ReleaseDbConnection(dbName, priority, conn, Sql);
  1038. }
  1039. return lines;
  1040. }
  1041. public bool UpdateTable(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, DataTable dt, out string err)
  1042. {
  1043. err = "";
  1044. bool result = true;
  1045. //获取数据库连接...
  1046. IDbConnection conn = null;
  1047. IDbTransaction trans = null;
  1048. //如果是事务
  1049. if (_transList.Contains(guid))
  1050. {
  1051. //Console.WriteLine("事务:更新DataSet<{0}>...\n", Sql);
  1052. TransInfo info = (TransInfo)_transList[guid];
  1053. conn = info.dbConnection;
  1054. trans = info.dbTransaction;
  1055. //如果事务已经失效,则返回
  1056. if (trans.Connection == null)
  1057. {
  1058. err = "事务已经失效!";
  1059. return false;
  1060. }
  1061. }
  1062. else
  1063. {
  1064. err = "未找到请求的事务!";
  1065. return false;
  1066. }
  1067. if (conn == null)
  1068. {
  1069. err = "获取数据库连接失败!";
  1070. return false;
  1071. }
  1072. try
  1073. {
  1074. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  1075. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  1076. IDbCommand command = conn.CreateCommand();
  1077. command.CommandText = Sql;
  1078. command.CommandType = cmdType;
  1079. command.Transaction = trans;
  1080. ((IDbDataAdapter)adapter).SelectCommand = command;
  1081. //给所有的参数赋值
  1082. if (Params != null && Values != null)
  1083. {
  1084. for (int i = 0; i < Params.Length; i++)
  1085. {
  1086. command.Parameters.Add(Params[i]);
  1087. }
  1088. }
  1089. //自动生成SqlCommand命令
  1090. object CB = dataProvider.CreateCommandBuilder(adapter);
  1091. //提交数据更改
  1092. adapter.Update(dt);
  1093. }
  1094. catch (Exception ex)
  1095. {
  1096. err = ex.Message;
  1097. trans.Rollback();
  1098. result = false;
  1099. }
  1100. return result;
  1101. }
  1102. //非事务处理
  1103. public bool UpdateTable(string dbName, string priority, string Sql, CommandType cmdType, IDataParameter[] Params, object[] Values, DataTable dt, out string err)
  1104. {
  1105. err = "";
  1106. bool result = false;
  1107. //获取数据库连接...
  1108. IDbConnection conn = null;
  1109. //获取数据库连接
  1110. conn = GetDbConnection(dbName, priority, Sql);
  1111. if (conn == null)
  1112. {
  1113. err = "获取数据库连接失败!";
  1114. return false;
  1115. }
  1116. try
  1117. {
  1118. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  1119. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  1120. IDbCommand command = conn.CreateCommand();
  1121. command.CommandText = Sql;
  1122. command.CommandType = cmdType;
  1123. ((IDbDataAdapter)adapter).SelectCommand = command;
  1124. //给所有的参数赋值
  1125. if (Params != null && Values != null)
  1126. {
  1127. for (int i = 0; i < Params.Length; i++)
  1128. {
  1129. command.Parameters.Add(Params[i]);
  1130. ((IDbDataParameter)command.Parameters[i]).Value = Values[i];
  1131. }
  1132. }
  1133. //自动生成SqlCommand命令
  1134. object CB = dataProvider.CreateCommandBuilder(adapter);
  1135. //提交数据更改
  1136. adapter.Update(dt);
  1137. }
  1138. catch (Exception ex)
  1139. {
  1140. err = ex.Message;
  1141. result = false;
  1142. }
  1143. finally
  1144. {
  1145. ReleaseDbConnection(dbName, priority, conn, Sql);
  1146. }
  1147. return result;
  1148. }
  1149. //事务处理
  1150. public bool UpdateTable(string dbName, string priority, Guid guid, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, object[] Values, DataTable dt, out string err)
  1151. {
  1152. err = "";
  1153. bool result = true;
  1154. //获取数据库连接...
  1155. IDbConnection conn = null;
  1156. IDbTransaction trans = null;
  1157. //如果是事务
  1158. if (_transList.Contains(guid))
  1159. {
  1160. //Console.WriteLine("事务:更新DataSet<{0}>...\n", Sql);
  1161. TransInfo info = (TransInfo)_transList[guid];
  1162. conn = info.dbConnection;
  1163. trans = info.dbTransaction;
  1164. //如果事务已经失效,则返回
  1165. if (trans.Connection == null)
  1166. {
  1167. err = "事务已经失效!";
  1168. cwl.WriteLog(err, LogInfoLevel.Error, "UpdateTable", "事务1");
  1169. return false;
  1170. }
  1171. }
  1172. else
  1173. {
  1174. err = "未找到请求的事务!";
  1175. return false;
  1176. }
  1177. if (conn == null)
  1178. {
  1179. err = "获取数据库连接失败!";
  1180. return false;
  1181. }
  1182. try
  1183. {
  1184. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  1185. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  1186. IDbCommand command = conn.CreateCommand();
  1187. command.CommandText = Sql;
  1188. command.CommandType = cmdType;
  1189. command.Transaction = trans;
  1190. //给所有的参数赋值
  1191. if (ParameterNames != null && DbTypes != null && Values != null)
  1192. {
  1193. for (int i = 0; i < ParameterNames.Length; i++)
  1194. {
  1195. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  1196. parameter.ParameterName = ParameterNames[i];
  1197. if (conn is System.Data.OracleClient.OracleConnection)
  1198. {
  1199. try
  1200. {
  1201. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  1202. }
  1203. catch
  1204. {
  1205. parameter.DbType = DbTypes[i];
  1206. }
  1207. }
  1208. else
  1209. {
  1210. parameter.DbType = DbTypes[i];
  1211. }
  1212. parameter.Value = Values[i];
  1213. command.Parameters.Add(parameter);
  1214. }
  1215. }
  1216. ((IDbDataAdapter)adapter).SelectCommand = command;
  1217. //自动生成SqlCommand命令
  1218. object CB = dataProvider.CreateCommandBuilder(adapter);
  1219. //提交数据更改
  1220. adapter.Update(dt);
  1221. }
  1222. catch (Exception ex)
  1223. {
  1224. err = ex.Message;
  1225. trans.Rollback();
  1226. result = false;
  1227. }
  1228. return result;
  1229. }
  1230. //非事务处理
  1231. public bool UpdateTable(string dbName, string priority, string Sql, CommandType cmdType, string[] ParameterNames, DbType[] DbTypes, object[] Values, DataTable dt, out string err)
  1232. {
  1233. err = "";
  1234. bool result = true;
  1235. //获取数据库连接...
  1236. IDbConnection conn = null;
  1237. //获取数据库连接
  1238. conn = GetDbConnection(dbName, priority, Sql);
  1239. if (conn == null)
  1240. {
  1241. err = "获取数据库连接失败!";
  1242. return false;
  1243. }
  1244. try
  1245. {
  1246. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  1247. DbDataAdapter adapter = dataProvider.CreateDataAdapterObject();
  1248. IDbCommand command = conn.CreateCommand();
  1249. command.CommandText = Sql;
  1250. command.CommandType = cmdType;
  1251. //给所有的参数赋值
  1252. if (ParameterNames != null && DbTypes != null && Values != null)
  1253. {
  1254. for (int i = 0; i < ParameterNames.Length; i++)
  1255. {
  1256. IDbDataParameter parameter = dataProvider.CreateDataParameter();
  1257. parameter.ParameterName = ParameterNames[i];
  1258. if (conn is System.Data.OracleClient.OracleConnection)
  1259. {
  1260. try
  1261. {
  1262. ((System.Data.OracleClient.OracleParameter)parameter).OracleType = (System.Data.OracleClient.OracleType)DbTypes[i];
  1263. }
  1264. catch
  1265. {
  1266. parameter.DbType = DbTypes[i];
  1267. }
  1268. }
  1269. else
  1270. {
  1271. parameter.DbType = DbTypes[i];
  1272. }
  1273. parameter.Value = Values[i];
  1274. command.Parameters.Add(parameter);
  1275. }
  1276. }
  1277. ((IDbDataAdapter)adapter).SelectCommand = command;
  1278. //自动生成SqlCommand命令
  1279. object CB = dataProvider.CreateCommandBuilder(adapter);
  1280. //提交数据更改
  1281. adapter.Update(dt);
  1282. }
  1283. catch (Exception ex)
  1284. {
  1285. err = ex.Message;
  1286. result = false;
  1287. }
  1288. finally
  1289. {
  1290. //释放连接
  1291. ReleaseDbConnection(dbName, priority, conn, Sql);
  1292. }
  1293. return result;
  1294. }
  1295. #endregion
  1296. #region"基础操作"
  1297. public IDbCommand GetCommand(string dbName, string priority, Guid guid, out string err)
  1298. {
  1299. err = "";
  1300. //获取数据库连接...
  1301. IDbConnection conn = null;
  1302. IDbTransaction trans = null;
  1303. //如果是事务
  1304. if (_transList.Contains(guid))
  1305. {
  1306. //Console.WriteLine("事务:更新DataSet<{0}>...\n", Sql);
  1307. TransInfo info = (TransInfo)_transList[guid];
  1308. conn = info.dbConnection;
  1309. trans = info.dbTransaction;
  1310. //如果事务已经失效,则返回
  1311. if (trans.Connection == null)
  1312. {
  1313. err = "事务已经失效";
  1314. return null;
  1315. }
  1316. }
  1317. else
  1318. {
  1319. //Console.WriteLine("更新DataSet<{0}>...\n", Sql);
  1320. //获取数据库连接
  1321. conn = GetDbConnection(dbName, priority, "GetCommand");
  1322. }
  1323. if (conn == null)
  1324. {
  1325. err = "获取数据库连接失败!";
  1326. return null;
  1327. }
  1328. IDbCommand command = conn.CreateCommand();
  1329. return command;
  1330. }
  1331. public IDbTransaction GetTransaction(string dbName, string priority, Guid guid, out string err)
  1332. {
  1333. err = "";
  1334. IDbConnection conn = null;
  1335. IDbTransaction trans = null;
  1336. //如果是事务
  1337. if (_transList.Contains(guid))
  1338. {
  1339. //Console.WriteLine("事务:更新DataSet<{0}>...\n", Sql);
  1340. TransInfo info = (TransInfo)_transList[guid];
  1341. trans = info.dbTransaction;
  1342. conn = info.dbConnection;
  1343. //如果事务已经失效,则返回
  1344. if (trans.Connection == null)
  1345. {
  1346. err = "事务已经失效";
  1347. return null;
  1348. }
  1349. }
  1350. else
  1351. {
  1352. err = "没有事务!";
  1353. return null;
  1354. }
  1355. if (conn == null)
  1356. {
  1357. err = "获取数据库连接失败!";
  1358. return null;
  1359. }
  1360. return trans;
  1361. }
  1362. /// <summary>
  1363. /// 获取连接提供者
  1364. /// </summary>
  1365. /// <param name="configurationString"></param>
  1366. /// <returns></returns>
  1367. private static DataProvider.IDataProvider GetDataProvider(string dbType)
  1368. {
  1369. DataProvider.IDataProvider dataProvider =
  1370. (DataProvider.IDataProvider)_dataProviderNameList[dbType.ToUpper()];
  1371. return dataProvider;
  1372. }
  1373. public DataProvider.IDataProvider GetDataProvider(string dbName, out string err)
  1374. {
  1375. err = "";
  1376. return GetDataProvider(this.GetDbType(dbName, out err));
  1377. }
  1378. public DataProvider.IDataProvider GetDataProvider(Guid guid, out string err)
  1379. {
  1380. err = "";
  1381. IDbConnection conn = null;
  1382. //如果是事务
  1383. if (_transList.Contains(guid))
  1384. {
  1385. //Console.WriteLine("事务:更新DataSet<{0}>...\n", Sql);
  1386. TransInfo info = (TransInfo)_transList[guid];
  1387. conn = info.dbConnection;
  1388. }
  1389. if (conn == null)
  1390. {
  1391. err = "获取数据库连接失败!";
  1392. return null;
  1393. }
  1394. return GetDataProvider(conn.GetType());
  1395. }
  1396. private static DataProvider.IDataProvider GetDataProvider(System.Type dbType)
  1397. {
  1398. DataProvider.IDataProvider dataProvider =
  1399. (DataProvider.IDataProvider)_dataProviderTypeList[dbType];
  1400. return dataProvider;
  1401. }
  1402. public System.Guid BeginTransaction(string dbName, string priority, long dbManagerID, double minutes, out string err)
  1403. {
  1404. return BeginTransaction("", dbName, priority, dbManagerID, minutes, out err);
  1405. }
  1406. /// <summary>
  1407. /// 开始处理事务,同时指定该事务最长处理时间,单位为分钟,返回结果表示是否成功
  1408. /// 不支持嵌套事务
  1409. /// </summary>
  1410. /// <param name="dbName"></param>
  1411. /// <param name="priority"></param>
  1412. /// <param name="dbManagerID"></param>
  1413. /// <param name="minutes"></param>
  1414. /// <param name="err"></param>
  1415. /// <returns></returns>
  1416. public System.Guid BeginTransaction(string methodName, string dbName, string priority, long dbManagerID, double minutes, out string err)
  1417. {
  1418. lock (_transList)
  1419. {
  1420. err = "";
  1421. Guid guid = Guid.NewGuid();
  1422. //获取数据库连接
  1423. IDbConnection conn = GetDbConnection(dbName, priority, methodName);
  1424. if (conn == null)
  1425. {
  1426. err = "获取数据库连接失败!";
  1427. return guid;
  1428. }
  1429. //设置事务信息,包括数据库连接、事务、事务开始时间等
  1430. TransInfo info = new TransInfo();
  1431. info.dbConnection = conn;
  1432. info.beginTime = DateTime.Now;
  1433. info.dbManagerID = dbManagerID;
  1434. try
  1435. {
  1436. info.dbTransaction = conn.BeginTransaction(); ;
  1437. _transList.Add(guid, info);
  1438. }
  1439. catch (Exception ex)
  1440. {
  1441. err = ex.Message;
  1442. ReleaseDbConnection(dbName, priority, conn, err);
  1443. }
  1444. return guid;
  1445. }
  1446. }
  1447. //提交当前事务,返回结果表示是否成功
  1448. public bool Commit(string dbName, string priority, Guid guid, out string err)
  1449. {
  1450. lock (_transList)
  1451. {
  1452. bool result = true;
  1453. err = "";
  1454. if (!_transList.Contains(guid))
  1455. {
  1456. err = "不存在要提交的事务";
  1457. return false;
  1458. }
  1459. TransInfo info = (TransInfo)_transList[guid];
  1460. IDbConnection conn = info.dbConnection;
  1461. IDbTransaction trans = info.dbTransaction;
  1462. try
  1463. {
  1464. trans.Commit();
  1465. }
  1466. catch (Exception ex)
  1467. {
  1468. err = ex.Message;
  1469. result = false;
  1470. cwl.WriteLog(err, LogInfoLevel.Error, "Rollback", "");
  1471. }
  1472. finally
  1473. {
  1474. //删除事务
  1475. ReleaseDbConnection(dbName, priority, conn, "Commit:" + guid.ToString());
  1476. _transList.Remove(guid);
  1477. }
  1478. return result;
  1479. }
  1480. }
  1481. //回滚当前事务,返回结果表示是否成功
  1482. public bool Rollback(string dbName, string priority, Guid guid, out string err)
  1483. {
  1484. lock (_transList)
  1485. {
  1486. bool result = true;
  1487. err = "";
  1488. if (!_transList.Contains(guid))
  1489. {
  1490. err = "不存在要回滚的事务";
  1491. return false;
  1492. }
  1493. TransInfo info = (TransInfo)_transList[guid];
  1494. IDbConnection conn = info.dbConnection;
  1495. IDbTransaction trans = info.dbTransaction;
  1496. try
  1497. {
  1498. trans.Rollback();
  1499. }
  1500. catch (Exception ex)
  1501. {
  1502. err = ex.Message;
  1503. result = false;
  1504. cwl.WriteLog(err, LogInfoLevel.Error, "Rollback", "");
  1505. }
  1506. finally
  1507. {
  1508. //删除事务
  1509. ReleaseDbConnection(dbName, priority, conn, "Rollback:" + guid.ToString());
  1510. _transList.Remove(guid);
  1511. }
  1512. return result;
  1513. }
  1514. }
  1515. /// <summary>
  1516. /// 从数据库配置文件中读取数据库连接信息
  1517. /// </summary>
  1518. /// <param name="fileName">文件名称</param>
  1519. /// <returns>是否成功</returns>
  1520. private bool ReadDbConnectionInfo(string fileName)
  1521. {
  1522. _dbConfigInfo = new Hashtable();
  1523. //如果文件存在则读取配置文件
  1524. if (File.Exists(fileName) == true)
  1525. {
  1526. try
  1527. {
  1528. XmlTextReader reader = null;
  1529. try
  1530. {
  1531. //打开数据库配置文件,并忽略所有的空格
  1532. reader = new XmlTextReader(fileName);
  1533. reader.WhitespaceHandling = WhitespaceHandling.None;
  1534. string dbName = "";
  1535. DBConnectionInfo dbConnectionInfo = new DBConnectionInfo();
  1536. dbConnectionInfo.connectionPool = new Hashtable();
  1537. string type = "";
  1538. string priority = "";
  1539. string maxNum = "";
  1540. //解释XML文件
  1541. while (reader.Read())
  1542. {
  1543. //bool bFlag = false;
  1544. switch (reader.NodeType)
  1545. {
  1546. case XmlNodeType.Element:
  1547. type = reader.Name;
  1548. break;
  1549. case XmlNodeType.Text:
  1550. if (type.Equals("name"))
  1551. {
  1552. dbName = reader.Value;
  1553. dbConnectionInfo.dbName = dbName;
  1554. }
  1555. else
  1556. {
  1557. if (type.Equals("type"))
  1558. {
  1559. dbConnectionInfo.dbType = reader.Value;
  1560. }
  1561. else
  1562. {
  1563. if (type.Equals("connectionString"))
  1564. {
  1565. dbConnectionInfo.connectionString = reader.Value;
  1566. }
  1567. else
  1568. {
  1569. if (type.Equals("connectionPool"))
  1570. {
  1571. //dbConnectionInfo.connectionPool = new Hashtable();
  1572. }
  1573. else
  1574. {
  1575. if (type.Equals("priority"))
  1576. {
  1577. priority = reader.Value;
  1578. }
  1579. else
  1580. {
  1581. if (type.Equals("maxConnectionNum"))
  1582. {
  1583. maxNum = reader.Value;
  1584. }
  1585. }
  1586. }
  1587. }
  1588. }
  1589. }
  1590. type = "";
  1591. break;
  1592. case XmlNodeType.EndElement:
  1593. if (reader.Name.Equals("connectionPool"))
  1594. {
  1595. dbConnectionInfo.connectionPool.Add(priority, maxNum);
  1596. priority = "";
  1597. maxNum = "";
  1598. }
  1599. else
  1600. {
  1601. if (reader.Name.Equals("database"))
  1602. {
  1603. _dbConfigInfo.Add(dbName, dbConnectionInfo);
  1604. dbName = "";
  1605. dbConnectionInfo = new DBConnectionInfo();
  1606. dbConnectionInfo.connectionPool = new Hashtable();
  1607. }
  1608. }
  1609. break;
  1610. }
  1611. }
  1612. }
  1613. finally
  1614. {
  1615. if (reader != null)
  1616. reader.Close();
  1617. }
  1618. }
  1619. catch (Exception ex)
  1620. {
  1621. return false;
  1622. }
  1623. }
  1624. else//读取默认设置
  1625. {
  1626. return false;
  1627. }
  1628. return true;
  1629. }
  1630. // 获取数据库连接
  1631. private IDbConnection GetDbConnection(string dbName, string priority, string sqlstr)
  1632. {
  1633. IDbConnection conn = null;
  1634. //直接从数据库连接池中取连接,无连接则直接返回
  1635. Hashtable ht1 = (Hashtable)_connList[dbName];
  1636. if (ht1 == null)
  1637. {
  1638. cwl.WriteLog("获取数据库连接失败!", LogInfoLevel.Message, "ht1:" + dbName, "GetDbConnection");
  1639. return conn;
  1640. }
  1641. Hashtable ht2 = (Hashtable)ht1[priority];
  1642. if (ht2 == null)
  1643. {
  1644. cwl.WriteLog("获取数据库连接失败!", LogInfoLevel.Message, "ht2:" + dbName + "/" + priority, "GetDbConnection");
  1645. return conn;
  1646. }
  1647. lock (ht2)
  1648. {
  1649. ArrayList freeList = ht2[0] as ArrayList;
  1650. //排队,最长时间1秒
  1651. //{
  1652. // for (int i = 0; i < 10; i++)
  1653. // {
  1654. // Thread.Sleep(100);
  1655. // if (freeList.Count > 0)
  1656. // {
  1657. // i = 10;
  1658. // }
  1659. // }
  1660. //}
  1661. if (freeList.Count <= 0)
  1662. {
  1663. ArrayList al = ht2[1] as ArrayList;
  1664. if (al == null) al = new ArrayList();
  1665. cwl.WriteLog("数据库忙,请稍后再试!", LogInfoLevel.Message, "<<<>>> " + dbName + " <<<>>> freeList||" + al.Count.ToString(), "GetDbConnection");
  1666. return null;
  1667. }
  1668. conn = (IDbConnection)freeList[0];
  1669. freeList.Remove(conn);
  1670. Hashtable busyList = (Hashtable)ht2[1];
  1671. if (busyList == null)
  1672. {
  1673. busyList = new Hashtable();
  1674. ht2.Add(1, busyList);
  1675. }
  1676. DataProvider.IDataProvider dataProvider = GetDataProvider(conn.GetType());
  1677. try
  1678. {
  1679. if (conn.State != ConnectionState.Open)
  1680. {
  1681. string strConnState = conn.State.ToString();
  1682. conn.Close();
  1683. conn.Dispose();
  1684. conn = null;
  1685. GC.Collect();
  1686. foreach (DictionaryEntry it in _dbConfigInfo)
  1687. {
  1688. DBConnectionInfo info = (DBConnectionInfo)it.Value;
  1689. if (info.dbName == dbName)
  1690. {
  1691. conn = dataProvider.CreateConnectionObject(info.connectionString);
  1692. conn.Open();
  1693. cwl.WriteLog("重连成功!||连接状态:" + strConnState, LogInfoLevel.Message, info.dbName, "GetDbConnection");
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. }
  1699. catch (Exception ex)
  1700. {
  1701. cwl.WriteLog(ex.Message, LogInfoLevel.Error, "", "GetDbConnection");
  1702. return null;
  1703. }
  1704. CurrConnOperateInfo cco = new CurrConnOperateInfo();
  1705. cco.dt = DateTime.Now;
  1706. cco.sqlstr = sqlstr;
  1707. cco.SID = dataProvider.GetSID(ref conn);
  1708. busyList.Add(conn, cco);
  1709. return conn;
  1710. }
  1711. }
  1712. private void ReleaseDbConnection(string dbName, string priority, IDbConnection conn, string SqlText)
  1713. {
  1714. if (conn == null) { return; }
  1715. Hashtable ht1 = (Hashtable)_connList[dbName];
  1716. if (ht1 == null) { return; }
  1717. Hashtable ht2 = (Hashtable)ht1[priority];
  1718. if (ht2 == null) { return; }
  1719. lock (ht2)
  1720. {
  1721. Hashtable busyList = (Hashtable)ht2[1];
  1722. if (busyList != null)
  1723. {
  1724. if (!busyList.Contains(conn))
  1725. {
  1726. // 连接已被检验线程处理,此处不在处理
  1727. //cwl.WriteLog(SqlText, LogInfoLevel.Message, "", "ReleaseDbConnection");
  1728. return;
  1729. }
  1730. busyList.Remove(conn);
  1731. }
  1732. ArrayList freeList = ht2[0] as ArrayList;
  1733. if (freeList == null)
  1734. {
  1735. freeList = new ArrayList();
  1736. ht2.Add(0, freeList);
  1737. }
  1738. freeList.Add(conn);
  1739. }
  1740. }
  1741. /// <summary>
  1742. /// 初始化数据提供接口
  1743. /// </summary>
  1744. /// <param name="dataProvider"></param>
  1745. public static void AddDataProvider(DataProvider.IDataProvider dataProvider)
  1746. {
  1747. _dataProviderNameList[dataProvider.Name.ToUpper()] = dataProvider;
  1748. _dataProviderTypeList[dataProvider.ConnectionType] = dataProvider;
  1749. }
  1750. private void CreateAllConnections()
  1751. {
  1752. //如果连接已经创建,则返回
  1753. if (_connList.Count > 0)
  1754. {
  1755. return;
  1756. }
  1757. foreach (DictionaryEntry it in _dbConfigInfo)
  1758. {
  1759. DBConnectionInfo info = (DBConnectionInfo)it.Value;
  1760. DataProvider.IDataProvider dataProvider = GetDataProvider(info.dbType);
  1761. Hashtable ht1 = new Hashtable();
  1762. foreach (DictionaryEntry pool in info.connectionPool)
  1763. {
  1764. Hashtable ht2 = new Hashtable();
  1765. ArrayList connList = new ArrayList();
  1766. for (int i = 0; i < Convert.ToInt32(pool.Value); i++)
  1767. {
  1768. try
  1769. {
  1770. IDbConnection conn = dataProvider.CreateConnectionObject(info.connectionString);
  1771. conn.Open();
  1772. connList.Add(conn);
  1773. }
  1774. catch (Exception ex)
  1775. {
  1776. cwl.WriteLog("创建数据库连接" + info.dbName + " " + pool.Key + " " + i.ToString() + "失败!", LogInfoLevel.Error, info.dbName, "CreateAllConnections");
  1777. break;
  1778. }
  1779. }
  1780. ht2.Add(0, connList);
  1781. ht1.Add(pool.Key, ht2);
  1782. }
  1783. _connList.Add(info.dbName, ht1);
  1784. }
  1785. cwl.WriteLog("启动成功!数据库连接池创建成功!");
  1786. }
  1787. #region 原来代码 maliang 2011-10-19
  1788. /*
  1789. private void FixConnections()
  1790. {
  1791. while (true)
  1792. {
  1793. try
  1794. {
  1795. foreach (DictionaryEntry it in _dbConfigInfo)
  1796. {
  1797. DBConnectionInfo info = (DBConnectionInfo)it.Value;
  1798. DataProvider.IDataProvider dataProvider = GetDataProvider(info.dbType);
  1799. Hashtable ht1 = (Hashtable)_connList[info.dbName];
  1800. foreach (DictionaryEntry pool in info.connectionPool)
  1801. {
  1802. Hashtable ht2 = (Hashtable)ht1[pool.Key];
  1803. lock (ht2)
  1804. {
  1805. ArrayList connList = ht2[0] as ArrayList;
  1806. Hashtable busyConnList = (Hashtable)ht2[1];
  1807. int connNumber = 0;
  1808. if (connList != null)
  1809. {
  1810. connNumber = connList.Count;
  1811. }
  1812. else
  1813. {
  1814. connList = new ArrayList();
  1815. ht2.Remove(0);
  1816. ht2.Add(0, connList);
  1817. }
  1818. if (busyConnList != null)
  1819. {
  1820. connNumber += busyConnList.Count;
  1821. }
  1822. if (connNumber < Convert.ToInt32(pool.Value))
  1823. {
  1824. cwl.WriteLog("有数据库连接意外销毁,增加数据库连接!", LogInfoLevel.Message, info.dbName, "FixConnections");
  1825. for (int i = connNumber; i < Convert.ToInt32(pool.Value); i++)
  1826. {
  1827. try
  1828. {
  1829. IDbConnection conn = dataProvider.CreateConnectionObject(info.connectionString);
  1830. conn.Open();
  1831. connList.Add(conn);
  1832. cwl.WriteLog("增加数据库连接" + info.dbName + " " + pool.Key + " " + i.ToString() + "成功!", LogInfoLevel.Message, info.dbName, "FixConnections");
  1833. }
  1834. catch (Exception ex)
  1835. {
  1836. cwl.WriteLog("增加数据库连接" + info.dbName + " " + pool.Key + " " + i.ToString() + "失败!", LogInfoLevel.Error, info.dbName, "FixConnections");
  1837. Console.WriteLine("增加数据库连接" + info.dbName + " " + pool.Key + " " + "失败!" + ex.Message + "\n");
  1838. break;
  1839. }
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. }
  1846. catch (Exception ex)
  1847. {
  1848. cwl.WriteLog(ex.Message, LogInfoLevel.Error, "", "FixConnections");
  1849. }
  1850. Thread.Sleep(3 * 60 * 1000);
  1851. }
  1852. }
  1853. private void CheckConnActive()
  1854. {
  1855. while (true)
  1856. {
  1857. try
  1858. {
  1859. if (_init)
  1860. {
  1861. foreach (DictionaryEntry it in _connList)
  1862. {
  1863. Hashtable ht1 = (Hashtable)it.Value;
  1864. string dbName = it.Key as string;
  1865. DBConnectionInfo info = (DBConnectionInfo)_dbConfigInfo[dbName];
  1866. DataProvider.IDataProvider dataProvider = GetDataProvider(info.dbType);
  1867. foreach (DictionaryEntry it2 in ht1)
  1868. {
  1869. ArrayList connList = ((Hashtable)it2.Value)[0] as ArrayList;
  1870. #region // 检查闲列表
  1871. //for ( int l = 0 ; l< connList.Count ;l++)
  1872. //{
  1873. // IDbConnection conn = connList[l] as IDbConnection ;
  1874. // if(conn!=null)
  1875. // {
  1876. // if(!dataProvider.Ping(ref conn) )
  1877. // {
  1878. // //Thread.Sleep( 1000);
  1879. // //if(!dataProvider.Ping(ref conn) )
  1880. // //{
  1881. // cwl.WriteLog("连接失效,重新连接中......", LogInfoLevel.Message, info.dbName, "CheckConnActive");
  1882. // Console.WriteLine(info.dbName + "连接失效,重新连接中......\n");
  1883. // //connList.Remove(conn);
  1884. // if (conn != null )
  1885. // {
  1886. // conn.Close();
  1887. // conn.Dispose();
  1888. // conn = null;
  1889. // GC.Collect();
  1890. // }
  1891. // try
  1892. // {
  1893. // conn = dataProvider.CreateConnectionObject(info.connectionString);
  1894. // conn.Open();
  1895. // connList[l] = conn;
  1896. // cwl.WriteLog("重连成功!", LogInfoLevel.Message, info.dbName, "CheckConnActive");
  1897. // Console.WriteLine(info.dbName + "重连成功!\n");
  1898. // }
  1899. // catch(Exception ex)
  1900. // {
  1901. // cwl.WriteLog("重连失败!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  1902. // Console.WriteLine(info.dbName + "重连失败!" +ex.Message + "\n");
  1903. // }
  1904. // //}
  1905. // }
  1906. // }
  1907. // else
  1908. // {
  1909. // try
  1910. // {
  1911. // conn = dataProvider.CreateConnectionObject(info.connectionString);
  1912. // conn.Open();
  1913. // connList[l]= conn;
  1914. // Console.WriteLine(info.dbName + "重连成功!\n");
  1915. // cwl.WriteLog("重连成功!", LogInfoLevel.Message, info.dbName, "CheckConnActive");
  1916. // }
  1917. // catch(Exception ex)
  1918. // {
  1919. // Console.WriteLine("重连失败!" + ex.Message + "\n");
  1920. // cwl.WriteLog("重连失败!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  1921. // }
  1922. // }
  1923. //}
  1924. #endregion
  1925. #region // 检查忙列表
  1926. if (((Hashtable)it2.Value).Count < 2) continue;
  1927. Hashtable busyConnList = ((Hashtable)it2.Value)[1] as Hashtable;
  1928. if (busyConnList != null)
  1929. {
  1930. Hashtable m_busyConnList = busyConnList.Clone() as Hashtable;
  1931. foreach (DictionaryEntry busyConn in m_busyConnList)
  1932. {
  1933. DateTime dt = ((CurrConnOperateInfo)busyConn.Value).dt;
  1934. DateTime t = DateTime.Now;
  1935. if ((string)it2.Key == "A")
  1936. {
  1937. t = dt.AddSeconds(_connLifeTimeA);
  1938. }
  1939. else if ((string)it2.Key == "B")
  1940. {
  1941. t = dt.AddSeconds(_connLifeTimeB);
  1942. }
  1943. else if ((string)it2.Key == "C")
  1944. {
  1945. t = dt.AddSeconds(_connLifeTimeC);
  1946. }
  1947. #region //判断是否已经超时
  1948. if (t.CompareTo(DateTime.Now) < 0)
  1949. {
  1950. IDbConnection conn = busyConn.Key as IDbConnection;
  1951. if (conn != null)
  1952. {
  1953. try
  1954. {
  1955. cwl.WriteLog("有连接超时,将关闭重连!||忙连接数:" + busyConnList.Count.ToString() + "||闲连接数:" + connList.Count.ToString()
  1956. + " SID:" + ((CurrConnOperateInfo)busyConn.Value).SID.ToString()
  1957. + " SQL:" + ((CurrConnOperateInfo)busyConn.Value).sqlstr
  1958. + " 执行开始时间:" + ((CurrConnOperateInfo)busyConn.Value).dt.ToLongTimeString()
  1959. + " 当前时间:" + DateTime.Now.ToLongTimeString(),
  1960. LogInfoLevel.Error, info.dbName, "CheckConnActive");
  1961. busyConnList.Remove(conn);
  1962. conn.Close();
  1963. conn.Dispose();
  1964. conn = null;
  1965. GC.Collect();
  1966. conn = dataProvider.CreateConnectionObject(info.connectionString);
  1967. conn.Open();
  1968. if (connList == null)
  1969. {
  1970. connList = new ArrayList();
  1971. ht1.Remove(0);
  1972. ht1.Add(0, connList);
  1973. }
  1974. connList.Add(conn);
  1975. cwl.WriteLog("超时连接重连成功!||忙连接数:" + busyConnList.Count.ToString() + "||闲连接数:" + connList.Count.ToString(),
  1976. LogInfoLevel.Message, info.dbName, "CheckConnActive");
  1977. }
  1978. catch (Exception ex)
  1979. {
  1980. cwl.WriteLog("重连失败,丢失一个连接!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  1981. Console.WriteLine("重连失败,丢失一个连接!" + ex.Message + "\n");
  1982. }
  1983. }
  1984. else
  1985. {
  1986. try
  1987. {
  1988. cwl.WriteLog("有连接失效,将关闭重连!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  1989. Console.WriteLine(info.dbName + "有连接失效,将关闭重连!\n");
  1990. busyConnList.Remove(conn);
  1991. conn = dataProvider.CreateConnectionObject(info.connectionString);
  1992. conn.Open();
  1993. if (connList == null)
  1994. {
  1995. connList = new ArrayList();
  1996. ht1.Remove(0);
  1997. ht1.Add(0, connList);
  1998. }
  1999. connList.Add(conn);
  2000. }
  2001. catch (Exception ex)
  2002. {
  2003. cwl.WriteLog("重连失败,丢失一个连接!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  2004. Console.WriteLine("重连失败,丢失一个连接!" + ex.Message + "\n");
  2005. }
  2006. }
  2007. }
  2008. #endregion
  2009. }
  2010. }
  2011. #endregion
  2012. }
  2013. }
  2014. }
  2015. }
  2016. catch (Exception ex)
  2017. {
  2018. cwl.WriteLog(ex.Message, LogInfoLevel.Error, "", "CheckConnActive");
  2019. // Console.WriteLine(ex.Message);
  2020. }
  2021. Thread.Sleep(1 * 60 * 1000);
  2022. }
  2023. }
  2024. */
  2025. #endregion
  2026. #region 变更代码 maliang 2011-10-19
  2027. private static object lockObj = new object();
  2028. private void ConnectionsListener()
  2029. {
  2030. while (true)
  2031. {
  2032. lock (lockObj)
  2033. {
  2034. CheckConnActive();
  2035. FixConnections();
  2036. Thread.Sleep(1 * 60 * 1000);
  2037. }
  2038. }
  2039. }
  2040. private void FixConnections()
  2041. {
  2042. try
  2043. {
  2044. foreach (DictionaryEntry it in _dbConfigInfo)
  2045. {
  2046. DBConnectionInfo info = (DBConnectionInfo)it.Value;
  2047. DataProvider.IDataProvider dataProvider = GetDataProvider(info.dbType);
  2048. Hashtable ht1 = (Hashtable)_connList[info.dbName];
  2049. foreach (DictionaryEntry pool in info.connectionPool)
  2050. {
  2051. Hashtable ht2 = (Hashtable)ht1[pool.Key];
  2052. lock (ht2)
  2053. {
  2054. ArrayList connList = ht2[0] as ArrayList;
  2055. Hashtable busyConnList = (Hashtable)ht2[1];
  2056. int connNumber = 0;
  2057. if (connList != null)
  2058. {
  2059. connNumber = connList.Count;
  2060. }
  2061. else
  2062. {
  2063. connList = new ArrayList();
  2064. ht2.Remove(0);
  2065. ht2.Add(0, connList);
  2066. }
  2067. if (busyConnList != null)
  2068. {
  2069. connNumber += busyConnList.Count;
  2070. }
  2071. if (connNumber < Convert.ToInt32(pool.Value))
  2072. {
  2073. cwl.WriteLog("有数据库连接意外销毁,增加数据库连接!", LogInfoLevel.Message, info.dbName, "FixConnections");
  2074. for (int i = connNumber; i < Convert.ToInt32(pool.Value); i++)
  2075. {
  2076. try
  2077. {
  2078. IDbConnection conn = dataProvider.CreateConnectionObject(info.connectionString);
  2079. conn.Open();
  2080. connList.Add(conn);
  2081. cwl.WriteLog("增加数据库连接" + info.dbName + " " + pool.Key + " " + i.ToString() + "成功!", LogInfoLevel.Message, info.dbName, "FixConnections");
  2082. }
  2083. catch (Exception ex)
  2084. {
  2085. cwl.WriteLog("增加数据库连接" + info.dbName + " " + pool.Key + " " + i.ToString() + "失败!", LogInfoLevel.Error, info.dbName, "FixConnections");
  2086. Console.WriteLine("增加数据库连接" + info.dbName + " " + pool.Key + " " + "失败!" + ex.Message + "\n");
  2087. break;
  2088. }
  2089. }
  2090. }
  2091. }
  2092. }
  2093. }
  2094. }
  2095. catch (Exception ex)
  2096. {
  2097. cwl.WriteLog(ex.Message, LogInfoLevel.Error, "", "FixConnections");
  2098. }
  2099. }
  2100. private void CheckConnActive()
  2101. {
  2102. try
  2103. {
  2104. if (_init)
  2105. {
  2106. foreach (DictionaryEntry it in _connList)
  2107. {
  2108. Hashtable ht1 = (Hashtable)it.Value;
  2109. string dbName = it.Key as string;
  2110. DBConnectionInfo info = (DBConnectionInfo)_dbConfigInfo[dbName];
  2111. DataProvider.IDataProvider dataProvider = GetDataProvider(info.dbType);
  2112. foreach (DictionaryEntry it2 in ht1)
  2113. {
  2114. ArrayList connList = ((Hashtable)it2.Value)[0] as ArrayList;
  2115. #region // 检查忙列表
  2116. if (((Hashtable)it2.Value).Count < 2) continue;
  2117. Hashtable busyConnList = ((Hashtable)it2.Value)[1] as Hashtable;
  2118. if (busyConnList != null)
  2119. {
  2120. Hashtable m_busyConnList = busyConnList.Clone() as Hashtable;
  2121. foreach (DictionaryEntry busyConn in m_busyConnList)
  2122. {
  2123. DateTime dt = ((CurrConnOperateInfo)busyConn.Value).dt;
  2124. DateTime t = DateTime.Now;
  2125. if ((string)it2.Key == "A")
  2126. {
  2127. t = dt.AddSeconds(_connLifeTimeA);
  2128. }
  2129. else if ((string)it2.Key == "B")
  2130. {
  2131. t = dt.AddSeconds(_connLifeTimeB);
  2132. }
  2133. else if ((string)it2.Key == "C")
  2134. {
  2135. t = dt.AddSeconds(_connLifeTimeC);
  2136. }
  2137. #region //判断是否已经超时
  2138. if (t.CompareTo(DateTime.Now) < 0)
  2139. {
  2140. IDbConnection conn = busyConn.Key as IDbConnection;
  2141. if (conn != null)
  2142. {
  2143. try
  2144. {
  2145. cwl.WriteLog("有连接超时,将关闭重连!||忙连接数:" + busyConnList.Count.ToString() + "||闲连接数:" + connList.Count.ToString()
  2146. + " SID:" + ((CurrConnOperateInfo)busyConn.Value).SID.ToString()
  2147. + " SQL:" + ((CurrConnOperateInfo)busyConn.Value).sqlstr
  2148. + " 执行开始时间:" + ((CurrConnOperateInfo)busyConn.Value).dt.ToLongTimeString()
  2149. + " 当前时间:" + DateTime.Now.ToLongTimeString(),
  2150. LogInfoLevel.Error, info.dbName, "CheckConnActive");
  2151. busyConnList.Remove(conn);
  2152. conn.Close();
  2153. conn.Dispose();
  2154. conn = null;
  2155. GC.Collect();
  2156. conn = dataProvider.CreateConnectionObject(info.connectionString);
  2157. conn.Open();
  2158. if (connList == null)
  2159. {
  2160. connList = new ArrayList();
  2161. ht1.Remove(0);
  2162. ht1.Add(0, connList);
  2163. }
  2164. connList.Add(conn);
  2165. cwl.WriteLog("超时连接重连成功!||忙连接数:" + busyConnList.Count.ToString() + "||闲连接数:" + connList.Count.ToString(),
  2166. LogInfoLevel.Message, info.dbName, "CheckConnActive");
  2167. }
  2168. catch (Exception ex)
  2169. {
  2170. cwl.WriteLog("重连失败,丢失一个连接!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  2171. Console.WriteLine("重连失败,丢失一个连接!" + ex.Message + "\n");
  2172. }
  2173. }
  2174. else
  2175. {
  2176. try
  2177. {
  2178. cwl.WriteLog("有连接失效,将关闭重连!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  2179. Console.WriteLine(info.dbName + "有连接失效,将关闭重连!\n");
  2180. busyConnList.Remove(conn);
  2181. conn = dataProvider.CreateConnectionObject(info.connectionString);
  2182. conn.Open();
  2183. if (connList == null)
  2184. {
  2185. connList = new ArrayList();
  2186. ht1.Remove(0);
  2187. ht1.Add(0, connList);
  2188. }
  2189. connList.Add(conn);
  2190. }
  2191. catch (Exception ex)
  2192. {
  2193. cwl.WriteLog("重连失败,丢失一个连接!", LogInfoLevel.Error, info.dbName, "CheckConnActive");
  2194. Console.WriteLine("重连失败,丢失一个连接!" + ex.Message + "\n");
  2195. }
  2196. }
  2197. }
  2198. #endregion
  2199. }
  2200. }
  2201. #endregion
  2202. }
  2203. }
  2204. }
  2205. }
  2206. catch (Exception ex)
  2207. {
  2208. cwl.WriteLog(ex.Message, LogInfoLevel.Error, "", "CheckConnActive");
  2209. // Console.WriteLine(ex.Message);
  2210. }
  2211. }
  2212. #endregion
  2213. ///////////////////////////////////////////////////////////////////////////////
  2214. ///私有数据成员
  2215. //////////////////////////////////////////////////////////////////////////////
  2216. //定义数据库连接信息
  2217. private struct DBConnectionInfo
  2218. {
  2219. public string dbName;
  2220. public string dbType;
  2221. public string connectionString;
  2222. // <Key = priority Value = maxConnectionNum/>
  2223. public Hashtable connectionPool;
  2224. };
  2225. // 存储当前连接的操作信息
  2226. private struct CurrConnOperateInfo
  2227. {
  2228. public DateTime dt;
  2229. public string sqlstr;
  2230. public int SID;
  2231. }
  2232. /// <summary>
  2233. /// 事务信息
  2234. /// </summary>
  2235. private struct TransInfo
  2236. {
  2237. public long dbManagerID;
  2238. public IDbConnection dbConnection;
  2239. public IDbTransaction dbTransaction;
  2240. public DateTime beginTime;
  2241. };
  2242. private void showsql(string sql)
  2243. {
  2244. //Console.WriteLine(sql+"\n");
  2245. }
  2246. #endregion
  2247. private static long[] _dbManagerID = new long[1] { 0 };//由DBProxy为每个DBManager对象分配的ID
  2248. //<Key = name Value DBConnectionInfo>
  2249. private Hashtable _dbConfigInfo;//数据库配置信息
  2250. private static Hashtable _connList = new Hashtable();
  2251. private Hashtable _transList = new Hashtable();
  2252. private Hashtable _dbList = new Hashtable();//每个DBManager对应的数据库信息
  2253. private double _connLifeTimeA;//数据库连接允许的最长无反应时间A级
  2254. private double _connLifeTimeB;//数据库连接允许的最长无反应时间B级
  2255. private double _connLifeTimeC;//数据库连接允许的最长无反应时间C级
  2256. private Thread _checkConnThread;//检查Conn是否有效
  2257. private Thread _fixConnThread;//检查Conn是否有足够数量
  2258. private Thread _ConnectionsListenerThread;//检查Conn是否有效和是否有足够数量
  2259. private long _reference = 0;//DBProxy对象实例数
  2260. private bool _init = false;
  2261. }
  2262. }