DBProxy.cs 108 KB

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