38b2caf5685d21b1d911a1d2c4b4d34fa94fd71b.svn-base 98 KB

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