8a86d6627789377ffa3850b13175370a6791fe87.svn-base 117 KB

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