5e6e4db425949263a70032c666508b766307d250.svn-base 112 KB

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