DBConnSql.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. //using System.Timers;
  8. namespace SysBaseLibs
  9. {
  10. public delegate void evDBConnectionError(string pcCommand, string pcMessage, string pcErrorNumbers);
  11. public delegate void evDBConnectionAction(string pcCommand,string pcMessage);
  12. public class DBConnSql : IErrorMsg
  13. {
  14. // Fields
  15. private SqlDataAdapter _Adapter=null;
  16. private SqlConnection _Connection=null;
  17. private string _ConnectionString=null;
  18. protected DbConnStatus _Status=DbConnStatus.Error;
  19. private SqlTransaction _Transaction=null;
  20. // private Timer _timer = null;
  21. // Events
  22. public event evDBConnectionError OnDBConnectionError;
  23. public event evDBConnectionAction OnDBConnectionAction;
  24. // Methods
  25. public DBConnSql()
  26. {
  27. this._Adapter = new SqlDataAdapter();
  28. this._DataSet = null;
  29. this._Adapter.RowUpdated += new SqlRowUpdatedEventHandler(this._Adapter_RowUpdated);
  30. this._DataSet = new DataSet();
  31. this._ConnectionString = DALibs.CenterConnectionStr;
  32. }
  33. private void _Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
  34. {
  35. if (e.Errors != null)
  36. {
  37. this.FireDBConnectionError(" RowUpdated ", e.ToString(), e.Errors.Message);
  38. }
  39. }
  40. /// <summary>
  41. /// 打开数据库连接
  42. /// </summary>
  43. /// <returns></returns>
  44. public bool Open()
  45. {
  46. bool llRetVal = true;
  47. if (_ConnectionString=="")
  48. _ConnectionString = DALibs.CenterConnectionStr;
  49. try
  50. {
  51. _Connection = new SqlConnection(_ConnectionString);
  52. _Connection.Open();
  53. this._Status = DbConnStatus.Open;
  54. }
  55. catch (Exception e)
  56. {
  57. _ErrorMsg = "OpenConnection " + " Establishing Connection " + " \r\n " + e.Message;
  58. this.FireDBConnectionError("OpenConnection", "Establishing Connection", e.Message);
  59. llRetVal = false;
  60. }
  61. return llRetVal;
  62. }
  63. /// <summary>
  64. /// 打开数据库连接
  65. /// </summary>
  66. /// <param name="pcConnString"></param>
  67. /// <returns></returns>
  68. public bool Open(string pcConnString)
  69. {
  70. _ConnectionString = pcConnString;
  71. return Open();
  72. }
  73. /// <summary>
  74. /// 关闭数据库连接
  75. /// </summary>
  76. public void Close()
  77. {
  78. if (this._Connection != null)
  79. {
  80. this._Connection.Close();
  81. this._Connection.Dispose();
  82. this._Connection = null;
  83. this._Status = DbConnStatus.Close;
  84. }
  85. }
  86. /// <summary>
  87. /// 开始事务
  88. /// </summary>
  89. /// <returns></returns>
  90. public bool BeginTrans()
  91. {
  92. bool llRetVal = false;
  93. if (this._Connection != null && this._Transaction == null)
  94. {
  95. try
  96. {
  97. _Transaction = this._Connection.BeginTransaction();
  98. }
  99. catch (SqlException e)
  100. {
  101. _ErrorMsg = "BeginTrans " + " Start transaction: " + " \r\n " + e.Message;
  102. this.FireDBConnectionError("BeginTrans", "Start transaction: ", e.Message);
  103. }
  104. _ErrorMsg = "";
  105. llRetVal = true;
  106. }
  107. return llRetVal;
  108. }
  109. /// <summary>
  110. /// 确认事务
  111. /// </summary>
  112. /// <returns></returns>
  113. public bool CommitTrans()
  114. {
  115. if (_Transaction != null)
  116. {
  117. try
  118. {
  119. _Transaction.Commit();
  120. }
  121. catch (SqlException e)
  122. {
  123. _ErrorMsg = "CommitTrans: " + " Commit transaction: " + " \r\n " + e.Message;
  124. this.FireDBConnectionError("CommitTrans", "Commit transaction: ", e.Message);
  125. }
  126. _ErrorMsg = "";
  127. _Transaction.Dispose();
  128. _Transaction = null;
  129. }
  130. return true;
  131. }
  132. /// <summary>
  133. /// 回滚事务
  134. /// </summary>
  135. /// <returns></returns>
  136. public bool RollbackTrans()
  137. {
  138. if (this._Transaction != null)
  139. {
  140. try
  141. {
  142. _Transaction.Rollback();
  143. }
  144. catch (SqlException e)
  145. {
  146. this.FireDBConnectionError("RollbackTrans", "Rollback transaction: ", e.Message);
  147. }
  148. _Transaction.Dispose();
  149. _Transaction = null;
  150. }
  151. return true;
  152. }
  153. /// <summary>
  154. /// 在事务控制下,执行一个SQL语句
  155. /// </summary>
  156. /// <param name="pcCommand"></param>
  157. /// <returns></returns>
  158. public bool ExcuteSqlTran(string pcCommand)
  159. {
  160. bool lbRetVal = false;
  161. if (this.BeginTrans())
  162. {
  163. if (this.ExecuteSql(pcCommand))
  164. {
  165. lbRetVal = true;
  166. this.CommitTrans();
  167. }
  168. else
  169. this.RollbackTrans();
  170. }
  171. return lbRetVal;
  172. }
  173. /// <summary>
  174. /// 执行SQL语句
  175. /// </summary>
  176. /// <param name="pcCommand"></param>
  177. /// <returns></returns>
  178. public bool ExecuteSql(string pcCommand)
  179. {
  180. FireDBConnectionAction("ExecuteSql", pcCommand + " ");
  181. bool llRetVal = false;
  182. if (this._Connection != null)
  183. {
  184. try
  185. {
  186. SqlCommand loCommand = new SqlCommand(pcCommand, this._Connection,
  187. this._Transaction);
  188. loCommand.ExecuteNonQuery();
  189. llRetVal = true;
  190. }
  191. catch (Exception e)
  192. {
  193. _ErrorMsg ="ExecuteSql: "+ pcCommand+" \r\n "+ e.Message;
  194. this.FireDBConnectionError("ExecuteSql", pcCommand, e.Message);
  195. llRetVal = false;
  196. }
  197. }
  198. return llRetVal;
  199. }
  200. //@TableName varchar(500), --查询的数据表名
  201. //@SelectFieldNames varchar(2000)='*', --查询的字段名称(不能包含"SELECT"关键字;以","隔开)
  202. //@StrWhere nvarchar(2000)=NULL, --查询条件字符串(不能包括"WHERE"关键字)
  203. //@OrderBy varchar(500)=NULL, --排序字段连接字符串(不能包含"ORDER BY"关键字;以","隔开)
  204. //@OrderType bit=0, --排序类型(0:升序;非0:降序)
  205. //@PageIndex int=1, --当前页码索引,默认1
  206. //@PageSize int=10, --每页显示的记录大小,默认10
  207. //@RowCount int=0 output, --总行数(记录数),默认0(输出参数)
  208. //@PageCount int=0 output --总页数;默认0(输出参数)
  209. /// <summary>
  210. ///
  211. /// </summary>
  212. /// <param name="pcTableName">查询的数据表名</param>
  213. /// <param name="pcPrimaryKey">主键</param>
  214. /// <param name="pcSelectFieldNames">查询的字段名称(不能包含"SELECT"关键字;以","隔开)</param>
  215. /// <param name="pcStrWhere">查询条件字符串(不能包括"WHERE"关键字)</param>
  216. /// <param name="pcOrderBy">排序字段连接字符串(不能包含"ORDER BY"关键字;以","隔开)</param>
  217. /// <param name="piOrderType">排序类型(0:升序;非0:降序)</param>
  218. /// <param name="piPageIndex">当前页码索引,默认1</param>
  219. /// <param name="piPageSize">每页显示的记录大小,默认10</param>
  220. /// <param name="piRowCount">总行数(记录数),默认0(输出参数)</param>
  221. /// <param name="piPageCount">总页数;默认0(输出参数)</param>
  222. /// <returns></returns>
  223. public DataSet GetPageRecords(string pcTableName, string pcPrimaryKey, string pcSelectFieldNames, string pcStrWhere, string pcOrderBy,
  224. int piOrderType, int piPageIndex, int piPageSize, out int piRowCount, out int piPageCount)
  225. {
  226. IdCollection loColl = new IdCollection();
  227. SqlSPPar loSPpar = new SqlSPPar("TableName", SqlDbType.VarChar);
  228. loSPpar.ParaLength = 500;
  229. loSPpar.ParameterValue = pcTableName;
  230. loColl.Add(loSPpar);
  231. loSPpar = new SqlSPPar("PrimaryKey", SqlDbType.VarChar);
  232. loSPpar.ParaLength = 100;
  233. loSPpar.ParameterValue = pcPrimaryKey;
  234. loColl.Add(loSPpar);
  235. loSPpar = new SqlSPPar("SelectFieldNames", SqlDbType.VarChar);
  236. loSPpar.ParaLength = 2000;
  237. loSPpar.ParameterValue = pcSelectFieldNames;
  238. loColl.Add(loSPpar);
  239. loSPpar = new SqlSPPar("StrWhere", SqlDbType.NVarChar);
  240. loSPpar.ParaLength = 2000;
  241. loSPpar.ParameterValue = pcStrWhere;
  242. loColl.Add(loSPpar);
  243. loSPpar = new SqlSPPar("OrderBy", SqlDbType.VarChar);
  244. loSPpar.ParaLength = 2000;
  245. loSPpar.ParameterValue = pcOrderBy;
  246. loColl.Add(loSPpar);
  247. loSPpar = new SqlSPPar("OrderType", SqlDbType.Int);
  248. loSPpar.ParameterValue = piOrderType + "";
  249. loColl.Add(loSPpar);
  250. loSPpar = new SqlSPPar("PageIndex", SqlDbType.Int);
  251. loSPpar.ParameterValue = piPageIndex + "";
  252. loColl.Add(loSPpar);
  253. loSPpar = new SqlSPPar("PageSize", SqlDbType.Int);
  254. //loSPpar.ParaLength = 5;
  255. loSPpar.ParameterValue = piPageSize + "";
  256. loColl.Add(loSPpar);
  257. piRowCount = 0;
  258. loSPpar = new SqlSPPar("RowCount", SqlDbType.Int);
  259. loSPpar.ParameterValue = piRowCount + "";
  260. loSPpar.IsOutput = true;
  261. loColl.Add(loSPpar);
  262. piPageCount = 0;
  263. loSPpar = new SqlSPPar("PageCount", SqlDbType.Int);
  264. loSPpar.ParameterValue = piPageCount + "";
  265. loSPpar.IsOutput = true;
  266. loColl.Add(loSPpar);
  267. DataSet loRetVal = new DataSet();
  268. if (!ExecuteStoredProc("p_Pagination", loColl, loRetVal))
  269. {
  270. loRetVal = null;
  271. }
  272. piRowCount = Utils.ValI(((SqlSPPar)loColl[8]).ParameterValue.ToString());
  273. piPageCount = Utils.ValI(((SqlSPPar)loColl[9]).ParameterValue.ToString());
  274. return loRetVal;
  275. }
  276. ///*************************************************************
  277. //Description:常用分页查询存储过程
  278. //**************************************************************/
  279. //CREATE PROC [dbo].[p_Pagination]
  280. //@TableName varchar(500), --查询的数据表名
  281. //@SelectFieldNames varchar(2000)='*', --查询的字段名称(不能包含"SELECT"关键字;以","隔开)
  282. //@StrWhere nvarchar(2000)=NULL, --查询条件字符串(不能包括"WHERE"关键字)
  283. //@OrderBy varchar(500)=NULL, --排序字段连接字符串(不能包含"ORDER BY"关键字;以","隔开)
  284. //@OrderType bit=0, --排序类型(0:升序;非0:降序)
  285. //@PageIndex int=1, --当前页码索引,默认1
  286. //@PageSize int=10, --每页显示的记录大小,默认10
  287. //@RowCount int=0 output, --总行数(记录数),默认0(输出参数)
  288. //@PageCount int=0 output --总页数;默认0(输出参数)
  289. //AS
  290. //BEGIN
  291. // --局部变量声明
  292. // DECLARE @StrSQL Nvarchar(4000), --分页主查询SQL语句变量
  293. // @StrGetCountSQL Nvarchar(4000), --查询总行数SQL语句变量
  294. // @PrimaryKey varchar(100), --主键列名称变量
  295. // @StrOrderType varchar(4) --排序类型字符(0:ASC;1:DESC)
  296. // --查询该表的主键列名称
  297. // SELECT @PrimaryKey=c.name FROM sysindexes AS i
  298. // JOIN sysindexkeys AS k on i.id = k.id AND i.indid = k.indid
  299. // JOIN sysobjects AS o on i.id = o.id
  300. // JOIN syscolumns AS c on i.id=c.id AND k.colid = c.colid
  301. // WHERE o.xtype = 'U' AND
  302. // EXISTS(SELECT 1 FROM sysobjects WHERE xtype = 'PK' and name = i.name)
  303. // AND o.name=@TableName
  304. // --判断页码是否小于0
  305. // IF(@PageIndex <1)SET @PageIndex=1;
  306. // IF(@SelectFieldNames='')SET @SelectFieldNames='*';
  307. //-- SET @StrWhere=LTRIM(RTRIM(@StrWhere));
  308. //-- SET @StrWhere=LTRIM(RTRIM(@StrWhere));
  309. // --判断查询条件是否为NULL或空字符串
  310. // IF ((@StrWhere IS NULL) OR (@StrWhere = '') )
  311. // BEGIN
  312. // SET @StrWhere='';
  313. // END
  314. // ELSE
  315. // BEGIN
  316. // SET @StrWhere=' AND '+@StrWhere;
  317. // END
  318. // --判断排序条件是否为NULL或空字符串
  319. // IF ((@OrderBy IS NULL) OR (@OrderBy = '') )
  320. // BEGIN
  321. // SET @OrderBy='';
  322. // END
  323. // ELSE
  324. // BEGIN
  325. // --判断排序类型
  326. // IF(@OrderType=0)
  327. // SET @StrOrderType='ASC';
  328. // ELSE
  329. // SET @StrOrderType='DESC';
  330. // SET @OrderBy=' Order By '+@OrderBy+' '+@StrOrderType;
  331. // END
  332. // --初始化查询记录总行数SQL语句
  333. // SET @StrGetCountSQL='SELECT @RowCount=COUNT(*) FROM '+@TableName +' WHERE 1=1 '+@StrWhere;
  334. // --初始化分页查询SQL语句
  335. // SET @StrSQL='SELECT TOP '+STR(@PageSize)+' '+@SelectFieldNames+' FROM '+@TableName
  336. // +' WHERE '+@PrimaryKey+' NOT IN ( SELECT TOP '+STR(@PageSize*(@PageIndex-1))+' '
  337. // +@PrimaryKey+' FROM '+@TableName +' WHERE 1=1 '+@StrWhere+@OrderBy+' ) '
  338. // +@StrWhere+@OrderBy;
  339. // --输出查询语句
  340. // print @StrGetCountSQL
  341. // print @StrSQL
  342. // --执行SQL查询语句
  343. // --EXEC(@StrGetCountSQL)--错误的执行带参数的方法
  344. // --正确的方法;声明变量输出
  345. // EXEC SP_EXECUTESQL @StrGetCountSQL,N'@RowCount int OUT',@RowCount out
  346. // --计算总页数
  347. // SET @PageCount=@RowCount/@PageSize
  348. // IF(@RowCount%@PageSize<>0)SET @PageCount=@PageCount+1;
  349. // EXEC(@StrSQL)
  350. //END
  351. //GO
  352. public DataSet GetPageRecords_RowNum(string pcTableName, string pcSelectFieldNames, string pcStrWhere, string pcOrderBy, int piPageIndex, int piPageSize, out int piRowCount, out int piPageCount)
  353. {
  354. IdCollection loColl = new IdCollection();
  355. SqlSPPar loSPpar = new SqlSPPar("TableName", SqlDbType.VarChar)
  356. {
  357. ParaLength = 500,
  358. ParameterValue = pcTableName
  359. };
  360. loColl.Add(loSPpar);
  361. loSPpar = new SqlSPPar("SelectFieldNames", SqlDbType.VarChar)
  362. {
  363. ParaLength = 2000,
  364. ParameterValue = pcSelectFieldNames
  365. };
  366. loColl.Add(loSPpar);
  367. loSPpar = new SqlSPPar("StrWhere", SqlDbType.NVarChar)
  368. {
  369. ParaLength = 4000,
  370. ParameterValue = pcStrWhere
  371. };
  372. loColl.Add(loSPpar);
  373. loSPpar = new SqlSPPar("OrderBy", SqlDbType.VarChar)
  374. {
  375. ParaLength = 2000,
  376. ParameterValue = pcOrderBy
  377. };
  378. loColl.Add(loSPpar);
  379. loSPpar = new SqlSPPar("PageIndex", SqlDbType.Int) {ParameterValue = piPageIndex + ""};
  380. loColl.Add(loSPpar);
  381. loSPpar = new SqlSPPar("PageSize", SqlDbType.Int) {ParameterValue = piPageSize + ""};
  382. //loSPpar.ParaLength = 5;
  383. loColl.Add(loSPpar);
  384. piRowCount = 0;
  385. loSPpar = new SqlSPPar("RowCount", SqlDbType.Int)
  386. {
  387. ParameterValue = piRowCount + "",
  388. IsOutput = true
  389. };
  390. loColl.Add(loSPpar);
  391. piPageCount = 0;
  392. loSPpar = new SqlSPPar("PageCount", SqlDbType.Int)
  393. {
  394. ParameterValue = piPageCount + "",
  395. IsOutput = true
  396. };
  397. loColl.Add(loSPpar);
  398. DataSet loRetVal = new DataSet();
  399. if (!ExecuteStoredProc("Pr_Pagination_RowNum", loColl, loRetVal))
  400. {
  401. loRetVal = null;
  402. }
  403. piRowCount = Utils.ValI(((SqlSPPar)loColl[6]).ParameterValue.ToString());
  404. piPageCount = Utils.ValI(((SqlSPPar)loColl[7]).ParameterValue.ToString ());
  405. return loRetVal;
  406. }
  407. public DataSet GetPageRecords(string pcQueryStr, int piPageSize, int piPageCurrent, string pcFdShow, string pcFdOrder)
  408. {
  409. IdCollection loColl = new IdCollection();
  410. SqlSPPar loSPpar = new SqlSPPar("QueryStr", SqlDbType.NVarChar);
  411. loSPpar.ParaLength = 4000;
  412. loSPpar.ParameterValue = pcQueryStr;
  413. loColl.Add(loSPpar);
  414. loSPpar = new SqlSPPar("PageSize", SqlDbType.Int);
  415. //loSPpar.ParaLength = 2000;
  416. loSPpar.ParameterValue = piPageSize + "";
  417. loColl.Add(loSPpar);
  418. loSPpar = new SqlSPPar("PageCurrent", SqlDbType.Int);
  419. //loSPpar.ParaLength = 5;
  420. loSPpar.ParameterValue = piPageCurrent + "";
  421. loColl.Add(loSPpar);
  422. loSPpar = new SqlSPPar("FdShow", SqlDbType.NVarChar);
  423. loSPpar.ParaLength = 4000;
  424. loSPpar.ParameterValue = pcFdShow;
  425. loColl.Add(loSPpar);
  426. loSPpar = new SqlSPPar("FdOrder", SqlDbType.NVarChar);
  427. loSPpar.ParaLength = 1000;
  428. loSPpar.ParameterValue = pcFdOrder;
  429. loColl.Add(loSPpar);
  430. DataSet loRetVal = new DataSet();
  431. if (!ExecuteStoredProc("p_Pagination", loColl, loRetVal))
  432. {
  433. loRetVal = null;
  434. }
  435. return loRetVal;
  436. }
  437. public bool ExecuteStoredProc(string pcSPName, IdCollection poParams, DataSet poOutDataSet)
  438. {
  439. FireDBConnectionAction("ExecuteStoredProc", pcSPName + " ");
  440. bool llRetVal = false;
  441. if (this._Connection != null)
  442. {
  443. try
  444. {
  445. SqlCommand loCommand = new SqlCommand(pcSPName, this._Connection, this._Transaction);
  446. loCommand.CommandType = CommandType.StoredProcedure;
  447. foreach (SqlSPPar loParmObject in poParams)
  448. {
  449. SqlParameter loSqlParm = new SqlParameter("@" + loParmObject.ParameterName.Trim(), loParmObject.ParameterType);
  450. if (loParmObject.IsOutput)
  451. {
  452. loSqlParm.Direction = ParameterDirection.Output;
  453. if (loParmObject.ParaLength > 0)
  454. loSqlParm.Size = loParmObject.ParaLength;
  455. }
  456. else
  457. {
  458. loSqlParm.Direction = ParameterDirection.Input;
  459. loSqlParm.Value = loParmObject.ParameterValue;
  460. if (loParmObject.ParaLength > 0)
  461. loSqlParm.Size = loParmObject.ParaLength;
  462. }
  463. loCommand.Parameters.Add(loSqlParm);
  464. }
  465. this._Adapter.SelectCommand = loCommand;
  466. this._Adapter.Fill(poOutDataSet);
  467. foreach (SqlParameter loParam in loCommand.Parameters)
  468. {
  469. if (loParam.Direction == ParameterDirection.Output)
  470. {
  471. string lcOutputParm = loParam.ParameterName.TrimStart('@');
  472. if (poParams[lcOutputParm] != null && poParams[lcOutputParm] is SqlSPPar)
  473. {
  474. SqlSPPar loOutputParm = (SqlSPPar)poParams[lcOutputParm];
  475. loOutputParm.ParameterValue = loParam.Value.ToString();
  476. }
  477. }
  478. }
  479. llRetVal = true;
  480. }
  481. catch (Exception e)
  482. {
  483. _ErrorMsg = "ExecuteStoredProc: " + pcSPName + " \r\n " + e.Message;
  484. this.FireDBConnectionError("ExecuteStoredProc", pcSPName, e.Message);
  485. }
  486. }
  487. return llRetVal;
  488. }
  489. /// <summary>
  490. /// 执行一个存储过程
  491. /// </summary>
  492. /// <param name="pcSPName">存储过程名</param>
  493. /// <param name="poParams">存储过程的参数</param>
  494. /// <returns>返回执行是否成功</returns>
  495. public bool ExecuteStoredProc(string pcSPName, IdCollection poParams)
  496. {
  497. FireDBConnectionAction("ExecuteStoredProc", pcSPName+" ");
  498. bool llRetVal = false;
  499. if (this._Connection != null)
  500. {
  501. try
  502. {
  503. SqlCommand loCommand = new SqlCommand(pcSPName, this._Connection,this._Transaction);
  504. loCommand.CommandType = CommandType.StoredProcedure;
  505. foreach (SqlSPPar loParmObject in poParams)
  506. {
  507. SqlParameter loSqlParm = new SqlParameter("@" + loParmObject.ParameterName.Trim(), loParmObject.ParameterType);
  508. if (loParmObject.IsOutput)
  509. {
  510. loSqlParm.Direction = ParameterDirection.Output;
  511. if (loParmObject.ParaLength > 0)
  512. loSqlParm.Size = loParmObject.ParaLength;
  513. }
  514. else
  515. {
  516. loSqlParm.Direction = ParameterDirection.Input;
  517. loSqlParm.Value = loParmObject.ParameterValue;
  518. if (loParmObject.ParaLength > 0)
  519. loSqlParm.Size = loParmObject.ParaLength;
  520. }
  521. loCommand.Parameters.Add(loSqlParm);
  522. }
  523. loCommand.ExecuteNonQuery();
  524. foreach (SqlParameter loParam in loCommand.Parameters)
  525. {
  526. if (loParam.Direction == ParameterDirection.Output)
  527. {
  528. string lcOutputParm = loParam.ParameterName.TrimStart('@');
  529. if (poParams[lcOutputParm] != null && poParams[lcOutputParm] is SqlSPPar)
  530. {
  531. SqlSPPar loOutputParm = (SqlSPPar)poParams[lcOutputParm];
  532. loOutputParm.ParameterValue = loParam.Value.ToString();
  533. }
  534. }
  535. }
  536. llRetVal = true;
  537. }
  538. catch (Exception e)
  539. {
  540. _ErrorMsg = "ExecuteStoredProc: " + pcSPName + " \r\n " + e.Message;
  541. this.FireDBConnectionError("ExecuteStoredProc", pcSPName, e.Message);
  542. }
  543. }
  544. return llRetVal;
  545. }
  546. private void FireDBConnectionAction(string pcCommand, string pcMessage)
  547. {
  548. if (OnDBConnectionAction != null)
  549. {
  550. if (OnlyExec)
  551. {
  552. if (pcCommand == "ExecuteSql")
  553. OnDBConnectionAction(pcCommand, pcMessage);
  554. }
  555. else
  556. OnDBConnectionAction(pcCommand, pcMessage);
  557. }
  558. }
  559. private void FireDBConnectionError(string pcCommand, string pcMessage, string pcErrorMessage)
  560. {
  561. if (OnDBConnectionError != null)
  562. {
  563. OnDBConnectionError(pcCommand, pcMessage, pcErrorMessage);
  564. }
  565. }
  566. /// <summary>
  567. /// 执行SQL语句,返回XML的数据集和数据结构
  568. /// </summary>
  569. /// <param name="pcSqlCommand"></param>
  570. /// <param name="pcXmlData"></param>
  571. /// <param name="pcXmlDataSchema"></param>
  572. /// <returns></returns>
  573. public bool GetData(string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema)
  574. {
  575. return GetData("", pcSqlCommand, ref pcXmlData, ref pcXmlDataSchema);
  576. }
  577. public bool GetData(string pcTableName, string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema)
  578. {
  579. FireDBConnectionAction("GetData", pcSqlCommand);
  580. bool lbRetVal = false;
  581. DataSet loDS = OpenDataSet(pcTableName, pcSqlCommand);
  582. if (loDS != null && loDS.Tables.Count == 1)
  583. {
  584. pcXmlData = loDS.GetXml();
  585. pcXmlDataSchema = loDS.GetXmlSchema();
  586. lbRetVal = true;
  587. }
  588. return lbRetVal;
  589. }
  590. // 扩展
  591. public bool GetData(string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema, ref int piRecCount)
  592. {
  593. return GetData("", pcSqlCommand, ref pcXmlData, ref pcXmlDataSchema, ref piRecCount);
  594. }
  595. public bool GetData(string pcTableName, string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema, ref int piRecCount)
  596. {
  597. FireDBConnectionAction("GetData", pcSqlCommand);
  598. bool lbRetVal = false;
  599. DataSet loDS = OpenDataSet(pcTableName, pcSqlCommand);
  600. if (loDS != null && loDS.Tables.Count == 1)
  601. {
  602. pcXmlData = loDS.GetXml();
  603. pcXmlDataSchema = loDS.GetXmlSchema();
  604. piRecCount = loDS.Tables[0].Rows.Count;
  605. lbRetVal = true;
  606. }
  607. return lbRetVal;
  608. }
  609. /// <summary>
  610. /// 打开一个dataset
  611. /// </summary>
  612. /// <param name="pcSqlCommand"></param>
  613. /// <returns></returns>
  614. public DataSet OpenDataSet(string pcSqlCommand)
  615. {
  616. return OpenDataSet("", pcSqlCommand);
  617. }
  618. public DataSet OpenDataSet(string pcTableName, string pcSqlCommand)
  619. {
  620. FireDBConnectionAction("OpenDataSet", pcSqlCommand);
  621. DataSet dataSet = new DataSet();
  622. if (this._Connection != null)
  623. {
  624. SqlCommand command = new SqlCommand(pcSqlCommand, this._Connection, this._Transaction);
  625. command.CommandTimeout = 60;
  626. this._Adapter.SelectCommand = command;
  627. try
  628. {
  629. if (pcTableName.Trim().Length == 0)
  630. this._Adapter.Fill(dataSet);
  631. else
  632. this._Adapter.Fill(dataSet, pcTableName);
  633. command.Dispose();
  634. command = null;
  635. }
  636. catch (Exception e)
  637. {
  638. _ErrorMsg = pcSqlCommand + " \r\n " + e.ToString();
  639. this.FireDBConnectionError("OpenDataSet", pcSqlCommand, e.Message);
  640. }
  641. }
  642. return dataSet;
  643. }
  644. /// <summary>
  645. /// 打开一个query
  646. /// </summary>
  647. /// <param name="pcSqlCommand"></param>
  648. /// <returns></returns>
  649. public rsQuery OpenQuery(string pcSqlCommand)
  650. {
  651. rsQuery loRetVal = new rsQuery();
  652. DataTable loTable = this.OpenDataTable(pcSqlCommand);
  653. loRetVal.OpenFromDataTable(loTable);
  654. return loRetVal;
  655. }
  656. /// <summary>
  657. /// 打开一个dataTable
  658. /// </summary>
  659. /// <param name="pcSqlCommand"></param>
  660. /// <returns></returns>
  661. public DataTable OpenDataTable(string pcSqlCommand)
  662. {
  663. FireDBConnectionAction("OpenDataTable", pcSqlCommand);
  664. DataTable loRetVal = new DataTable();
  665. if (this._Connection != null)
  666. {
  667. SqlCommand loComm = new SqlCommand(pcSqlCommand, this._Connection, this._Transaction);
  668. loComm.CommandTimeout = 60;
  669. this._Adapter.SelectCommand = loComm;
  670. try
  671. {
  672. this._Adapter.Fill(loRetVal);
  673. loComm.Dispose();
  674. loComm = null;
  675. }
  676. catch (Exception e)
  677. {
  678. _ErrorMsg = "OpenDataTable: " + pcSqlCommand + " \r\n " + e.Message;
  679. this.FireDBConnectionError("OpenDataTable", pcSqlCommand, e.Message);
  680. }
  681. }
  682. return loRetVal;
  683. }
  684. /// <summary>
  685. /// 打开一个dataTable
  686. /// </summary>
  687. /// <param name="pcSqlCommand"></param>
  688. /// <returns></returns>
  689. public DataTable OpenDataTable(DataSet poDataSet, string pcTableName, string pcSqlCommand)
  690. {
  691. FireDBConnectionAction("OpenDataTable", pcSqlCommand);
  692. DataTable table2 = null;
  693. if (this._Connection != null)
  694. {
  695. SqlCommand command = new SqlCommand(pcSqlCommand, this._Connection, this._Transaction);
  696. command.CommandTimeout = 60;
  697. if (poDataSet == null)
  698. {
  699. poDataSet = this._DataSet;
  700. }
  701. if (poDataSet.Tables.Contains(pcTableName))
  702. {
  703. poDataSet.Tables[pcTableName].Columns.Clear();
  704. poDataSet.Tables[pcTableName].Clear();
  705. }
  706. this._Adapter.SelectCommand = command;
  707. try
  708. {
  709. this._Adapter.Fill(poDataSet, pcTableName);
  710. command.Dispose();
  711. command = null;
  712. table2 = poDataSet.Tables[pcTableName];
  713. }
  714. catch (Exception e)
  715. {
  716. _ErrorMsg = "OpenDataTable : 执行的语句 "+pcSqlCommand +" 错误信息: "+e.Message ;
  717. this.FireDBConnectionError("OpenDataTable", pcSqlCommand, e.Message);
  718. }
  719. }
  720. return table2;
  721. }
  722. #region Properties
  723. private DataSet _DataSet = null;
  724. public DataSet DataSetDB
  725. {
  726. get
  727. {
  728. return this._DataSet;
  729. }
  730. }
  731. private string _ErrorMsg = "";
  732. public string ErrorMsg
  733. {
  734. get { return _ErrorMsg; }
  735. set { _ErrorMsg = value; }
  736. }
  737. public bool IsOpened
  738. {
  739. get
  740. {
  741. return (this._Status == DbConnStatus.Open);
  742. }
  743. }
  744. bool _OnlyExec=true;
  745. public bool OnlyExec
  746. {
  747. get { return _OnlyExec; }
  748. set { _OnlyExec = value; }
  749. }
  750. #endregion
  751. }
  752. public enum DbConnStatus
  753. {
  754. Open,
  755. Close,
  756. Error
  757. }
  758. public class SqlSPPar:INpId
  759. {
  760. // Fields
  761. private bool _IsOutput = false;
  762. private string _ParameterName;
  763. private SqlDbType _ParameterType;
  764. private object _ParameterValue = null;
  765. // Methods
  766. public SqlSPPar(string pcParameterName, SqlDbType pcParameterType)
  767. {
  768. this._ParameterName = pcParameterName.Trim();
  769. this._ParameterType = pcParameterType;
  770. }
  771. public string Id
  772. {
  773. get { return _ParameterName; }
  774. set { }
  775. }
  776. // Properties
  777. public bool IsOutput
  778. {
  779. get
  780. {
  781. return this._IsOutput;
  782. }
  783. set
  784. {
  785. this._IsOutput = value;
  786. }
  787. }
  788. public string ParameterName
  789. {
  790. get
  791. {
  792. return this._ParameterName;
  793. }
  794. }
  795. public SqlDbType ParameterType
  796. {
  797. get
  798. {
  799. return this._ParameterType;
  800. }
  801. }
  802. public object ParameterValue
  803. {
  804. get
  805. {
  806. return this._ParameterValue;
  807. }
  808. set
  809. {
  810. this._ParameterValue = value;
  811. }
  812. }
  813. private int _ParaLength = 0;
  814. public int ParaLength
  815. {
  816. get { return _ParaLength; }
  817. set { _ParaLength = value; }
  818. }
  819. }
  820. }