DBHelper.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using Microsoft.Win32;
  9. //Including multiple providers...
  10. using System.Data.OleDb;
  11. using System.Xml;
  12. using System.IO;
  13. //using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
  14. namespace com.cloudsoft.utils
  15. {
  16. /// <summary>
  17. /// Class contains methods for performing database queries
  18. /// </summary>
  19. public class DBHelper
  20. {
  21. private Boolean _pbUppercaseNodes = false;
  22. private String _pzDateFormat = "dd-MMM-yyyy HH:mm:ss";
  23. private String pzSystem = "";
  24. private String pzSchema = "";
  25. private XMLHelper _request = null;
  26. private Boolean lbAutoCommit = true;
  27. private OleDbConnection oConnection = null;
  28. private ArrayList _aParser = null;
  29. private DBParams _Command = null;
  30. public DBHelper(XMLHelper oRequest)
  31. {
  32. lbAutoCommit = (oRequest._con == null);
  33. _request = oRequest;
  34. pzSystem = oRequest.getString("system");
  35. oConnection = oRequest._con;
  36. }
  37. /// <summary>
  38. /// Creates, opens, and returns a connection to the database using details specified in the registry
  39. /// with the name stored in the local pzSystem variable.
  40. /// </summary>
  41. /// <param name="bIsRecordSet">If true then PLSQLRSet=1 is added to the connection string.</param>
  42. /// <param name="bOleDbConnection">Whether to create an OleDbConnection or a standard OleDbConnection.</param>
  43. /// <returns>An OleDbConnection.</returns>
  44. private OleDbConnection getConnection()
  45. {
  46. //RegistryKey kLM = Registry.LocalMachine;
  47. //String zUser;
  48. //String zPassword;
  49. //String zDBSource;
  50. ////Lock the registry while accessing connection values...
  51. //lock (kLM)
  52. //{
  53. // //Open the key for your connection string...
  54. // RegistryKey kSystem = kLM.OpenSubKey("Software\\Cloudsoft\\" + pzSystem);
  55. // //zPassword = (String)kSystem.GetValue("DBPassword", "notfound");
  56. // //zUser = (String)kSystem.GetValue("DBUser", "notfound");
  57. // zDBSource = (String)kSystem.GetValue("DBURL", "notfound");
  58. // //pzSchema = (String)kSystem.GetValue("DBSchema", "notfound");
  59. //}
  60. OleDbConnection cn;
  61. //String zConnString = zDBSource;
  62. String zConnString = ConfigurationManager.ConnectionStrings[pzSystem].ConnectionString;
  63. cn = new OleDbConnection(zConnString);
  64. try
  65. {
  66. cn.Open();
  67. }
  68. catch (Exception ex)
  69. {
  70. throw ex;
  71. }
  72. return cn;
  73. }
  74. //For File Loader ends here
  75. #region Public Methods
  76. /// <summary>Creates a new instance of DBHelper for a given system,
  77. /// defaulting to the default data provider</summary>
  78. /// <param name="zSystem">The system to connect to</param>
  79. /// <remarks>
  80. /// A registry entry should be set up for the given system under Hkey/LocalMachine/Software/GlaxoWellcome/system
  81. /// The system should have a minimum of the following:
  82. /// <list type="bullet">
  83. /// <item><description>DBUser</description></item>
  84. /// <item><description>DBPassword</description></item>
  85. /// <item><description>DBSource</description></item>
  86. /// </list>
  87. ///</remarks>
  88. public DBHelper(String zSystem)
  89. {
  90. pzSystem = zSystem.ToUpper();
  91. }
  92. public DBHelper(String zSystem, Boolean bUppercaseNodes) : this(zSystem)
  93. {
  94. _pbUppercaseNodes = bUppercaseNodes;
  95. }
  96. public DBHelper(String zSystem, Boolean bUppercaseNodes, String zDateFormat) : this(zSystem, bUppercaseNodes)
  97. {
  98. _pzDateFormat = zDateFormat;
  99. }
  100. /// <summary>
  101. /// Creates Parameters to pass to the passed in Command type
  102. /// </summary>
  103. /// <param name="zCommandText">Can be either a SQL Command or a stored procedure depending on the type</param>
  104. /// <param name="tCommandType">The type of Command to create parameters for</param>
  105. /// <returns>The Parameters Object</returns>
  106. public DBParams createSPParams(String zSQL)
  107. {
  108. return createSPParams(zSQL, "");
  109. }
  110. public DBParams createSPParams(String zSQL, XMLHelper oNode)
  111. {
  112. _request = oNode;
  113. zSQL = parseSQL(zSQL);
  114. if (_Command != null && _Command.Command != null)
  115. _Command.Command.Connection.Close();
  116. OleDbCommand oCommand = getConnection().CreateCommand();
  117. oCommand.CommandText = zSQL;
  118. _Command = new DBParams(oCommand);
  119. addParserParams(oNode);
  120. return _Command;
  121. }
  122. public DBParams createSPParams(String zSQL, String zXPath)
  123. {
  124. if (_request != null)
  125. zSQL = parseSQL(zSQL);
  126. if (_Command != null && _Command.Command != null)
  127. _Command.Command.Connection.Close();
  128. OleDbCommand oCommand = getConnection().CreateCommand();
  129. oCommand.CommandText = zSQL;
  130. _Command = new DBParams(oCommand);
  131. if (zXPath.Equals(""))
  132. addParserParams();
  133. else
  134. addParserParams(zXPath);
  135. return _Command;
  136. }
  137. public String parseSQL(String zSQL)
  138. {
  139. _aParser = new ArrayList();
  140. return parseSQL(zSQL, false);
  141. }
  142. public String parseSQL(String zSQL, Boolean bConditional)
  143. {
  144. try
  145. {
  146. //Make sure square brackets are even
  147. int iEven = 0;
  148. for(int i = 0, n = zSQL.Length ; i < n ; i++)
  149. {
  150. char c = zSQL[i];
  151. if (c == '[') iEven++;
  152. if (c == ']') iEven--;
  153. }
  154. if (iEven != 0)
  155. {
  156. throw new Exception("Square brackets must be matched");
  157. }
  158. // Find optional clauses
  159. Regex regex = new Regex("\\[([^\\[\\]]+|\\[([^\\[\\]]+|\\[([^\\[\\]]+)*\\])*\\])*\\]");
  160. Match matcher = regex.Match(zSQL);
  161. int iCurr = 0;
  162. StringBuilder sb = new StringBuilder();
  163. while (matcher.Success)
  164. {
  165. sb.Append(replaceSQLVars(zSQL.Substring(iCurr, matcher.Index - iCurr), bConditional));
  166. //iCurr = matcher.start() + matcher.end();
  167. iCurr = matcher.Index + matcher.Length;
  168. String match = matcher.Groups[0].Value;
  169. match = match.Substring(1, match.Length - 2);
  170. if (matcher.Groups[2].Value != null)
  171. sb.Append(parseSQL(match,true));
  172. else
  173. sb.Append(replaceSQLVars(match, true));
  174. matcher = matcher.NextMatch();
  175. }
  176. sb.Append(replaceSQLVars(zSQL.Substring(iCurr), bConditional));
  177. return sb.ToString();
  178. }
  179. catch (Exception ex)
  180. {
  181. return "";
  182. }
  183. }
  184. private String replaceSQLVars(String zSQL, Boolean bConditional)
  185. {
  186. try
  187. {
  188. // Find variables to replace
  189. Regex regex = new Regex("[vilcbotdf]?:[@a-z_0-9./]*");
  190. Match matcher = regex.Match(zSQL);
  191. StringBuilder sb = new StringBuilder();
  192. int iCurr = 0;
  193. while (matcher.Success)
  194. {
  195. // Get the replace value
  196. String match = matcher.Groups[0].Value;
  197. String zMatch = match.Substring(match.IndexOf(':')+1);
  198. char zMatchType = match[0];
  199. // If this is a conditional replace return nothing as soon as a variable is empty
  200. //if (bConditional && _request.getString(zMatch) == "" && zMatchType != 'o')
  201. if (bConditional && _request.getString(zMatch).Equals(""))
  202. return "";
  203. String zTextBefore = zSQL.Substring(iCurr, matcher.Index - iCurr);
  204. String zReplace = "";
  205. switch (zMatchType)
  206. {
  207. case ':':
  208. case 'o': zReplace = _request.getString(zMatch);
  209. //if (zReplace == "") zReplace = "=";
  210. if (zReplace.Equals("soundex"))
  211. {
  212. String[] arrWords = zTextBefore.Split(' ');
  213. zReplace += "(" + arrWords[arrWords.Length-1] + ") = ";
  214. zTextBefore = "";
  215. for (int i = 0; i < arrWords.Length - 1; i++)
  216. zTextBefore += arrWords[i] + " ";
  217. }
  218. break;
  219. case 'i':
  220. case 'f':
  221. case 't':
  222. case 'd':
  223. case 'b':
  224. zReplace = "?";
  225. _aParser.Add(match);
  226. break;
  227. default: zReplace = "?";
  228. String zOp = _request.getString("op_" + zMatch);
  229. if (zOp.Equals("soundex"))
  230. zReplace = "soundex('" + zReplace + "')";
  231. else if (zOp.Equals("like"))
  232. match = "l:" + zMatch;
  233. _aParser.Add(match);
  234. break;
  235. }
  236. sb.Append(zTextBefore);
  237. iCurr = matcher.Index + matcher.Length;
  238. sb.Append(zReplace);
  239. matcher = matcher.NextMatch();
  240. }
  241. sb.Append(zSQL.Substring(iCurr));
  242. return sb.ToString();
  243. }
  244. catch (Exception ex)
  245. {
  246. return "";
  247. }
  248. }
  249. private void addParserParams()
  250. {
  251. addParserParams(_request);
  252. }
  253. private void addParserParams(String zXPath)
  254. {
  255. foreach (XMLHelper oNode in _request.getNodes(zXPath))
  256. {
  257. addParserParams(oNode);
  258. //_Command.addBatch();
  259. }
  260. }
  261. private void addParserParams(XMLHelper oNode)
  262. {
  263. for (int i=0; i < _aParser.Count; i++)
  264. {
  265. // Get the replace value
  266. String match = _aParser[i] as string;
  267. String zMatch = match.Substring(match.IndexOf(':')+1);
  268. char zMatchType = match[0];
  269. Object oParam = null;
  270. String zValue = (zMatch.Equals("xml_data")) ? oNode.getNode(zMatch).getFirstChild().getXML() : oNode.getString(zMatch);
  271. OleDbType iType = OleDbType.VarChar;
  272. switch (zMatchType)
  273. {
  274. case 'f': oParam = oNode.getDouble(zMatch);
  275. iType = OleDbType.Double;
  276. break;
  277. case 'i': oParam = oNode.getInt(zMatch);
  278. iType = OleDbType.Integer;
  279. break;
  280. case 't': oParam = oNode.getSQLDateTime(zMatch);
  281. iType = OleDbType.DBTimeStamp;
  282. break;
  283. case 'd': oParam = oNode.getSQLDate(zMatch);
  284. iType = OleDbType.DBDate;
  285. break;
  286. case 'b': oParam = (oNode.getBoolean(zMatch)) ? "Y" : "N";
  287. zValue = (String) oParam;
  288. iType = OleDbType.Char;
  289. break;
  290. case 'c': oParam = zValue;
  291. iType = OleDbType.LongVarChar;
  292. break;
  293. case 'l': oParam = "%" + zValue + "%";
  294. zValue = (String) oParam;
  295. break;
  296. default: oParam = zValue;
  297. break;
  298. }
  299. if (zValue.Length == 0)
  300. oParam = null;
  301. _Command.set(i+1, oParam, iType);
  302. }
  303. }
  304. /// <summary>
  305. /// Legacy method - now superseded by createDBSPParams
  306. /// </summary>
  307. public DBParams createSPParams(String zCommandText, CommandType tCommandType)
  308. {
  309. OleDbCommand oCommand = getConnection().CreateCommand();
  310. oCommand.CommandType = tCommandType;
  311. oCommand.CommandText = zCommandText;
  312. return new DBParams(oCommand);
  313. }
  314. /// <summary>
  315. /// Executes a store procedure optionally returning output parameters
  316. /// This is not used for returning recordsets as XML, but for transactional stored procedures
  317. /// </summary>
  318. /// <param name="oParams">DBParams Parameters</param>
  319. /// <returns>An XMLHelper with any output parameters</returns>
  320. public XMLHelper execute(DBParams oParams)
  321. {
  322. //return execute(oParams, false);
  323. return execute(oParams, false);
  324. }
  325. /// <summary>
  326. /// Executes a SQL Query
  327. /// This is not used for returning recordsets as XML, but for transactional stored procedures
  328. /// </summary>
  329. /// <param name="zSQL">DBParams Parameters</param>
  330. /// <returns>An XMLHelper with any output parameters</returns>
  331. public XMLHelper execute(String zSQL)
  332. {
  333. DBParams oParams = createSPParams(zSQL, CommandType.Text);
  334. return execute(oParams, false);
  335. }
  336. /// <summary>
  337. /// Executes a store procedure optionally returning output parameters
  338. /// This is not used for returning recordsets as XML, but for transactional stored procedures
  339. /// </summary>
  340. /// <param name="oParams">DBParams Parameters</param>
  341. /// <returns>An XMLHelper with any output parameters</returns>
  342. public XMLHelper execute(DBParams oParams, Boolean bKeepOpen)
  343. {
  344. return executeODAC(oParams, bKeepOpen);
  345. }
  346. #endregion
  347. #region ODAC Specific Private Methods
  348. /// <summary>
  349. /// Executes a store procedure optionally returning output parameters
  350. /// This is not used for returning recordsets as XML, but for transactional stored procedures
  351. /// </summary>
  352. /// <param name="oParams">DBParams Parameters</param>
  353. /// <param name="bKeepOpen">Flag to determine if the connection is kept open or not</param>
  354. /// <returns>An XMLHelper with any output parameters</returns>
  355. private XMLHelper executeODAC(DBParams oParams, Boolean bKeepOpen)
  356. {
  357. DateTime dStart = DateTime.Now;
  358. XMLHelper oResponse = new XMLHelper("response");
  359. oResponse.appendNode("rows", Convert.ToString(oParams.ExecuteNonQuery()));
  360. foreach (OleDbParameter oParam in oParams.ODACParameters())
  361. {
  362. if (oParam.Direction == System.Data.ParameterDirection.Output)
  363. {
  364. switch (oParam.OleDbType.ToString())
  365. {
  366. case "Clob":
  367. {
  368. //XMLHelper oReturn = new XMLHelper(((Oracle.DataAccess.Types.OracleClob)(oParam.Value)).Value);
  369. //XMLHelper oParamNode = oResponse.appendNode(oParam.ParameterName);
  370. //oParamNode.appendNode(oReturn);
  371. break;
  372. }
  373. default:
  374. {
  375. oResponse.appendNode(oParam.ParameterName.Replace(":", ""), oParam.Value.ToString());
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. TimeSpan tDiff = DateTime.Now.Subtract(dStart);
  382. oResponse.appendAttribute("duration", tDiff.TotalMilliseconds.ToString());
  383. if (!bKeepOpen)
  384. {
  385. oParams.CloseConnection();
  386. oParams.CloseCommand();
  387. }
  388. return oResponse;
  389. }
  390. /// <summary>
  391. /// Creates Parameters to pass to a Stored Procedure. Also calls getConnection to get an
  392. /// OleDbConnection, and appends the required RefCursor.
  393. /// </summary>
  394. /// <param name="zCommandText">The stored procedure to be called</param>
  395. /// <returns>The Parameters Object</returns>
  396. /// <example>
  397. /// DBHelper oDB = new DBHelper("uki");
  398. /// DBParams oCommand = oDB.createOracleSPParams("pkg_orgcusts.RetrieveTargetOrgsByBrand");
  399. /// </example>
  400. private DBParams createODACSPParams(String zCommandText, CommandType tCommandType, Boolean bindByName)
  401. {
  402. OleDbCommand oCommand = getConnection().CreateCommand();
  403. oCommand.CommandType = tCommandType;
  404. switch (tCommandType)
  405. {
  406. case CommandType.Text:
  407. {
  408. oCommand.CommandText = zCommandText;
  409. break;
  410. }
  411. case CommandType.StoredProcedure:
  412. case CommandType.TableDirect:
  413. {
  414. oCommand.CommandText = pzSchema + "." + zCommandText;
  415. break;
  416. }
  417. }
  418. return new DBParams(oCommand);
  419. }
  420. #endregion
  421. public void closeConnection(DBParams oCommand)
  422. {
  423. oCommand.CloseConnection();
  424. }
  425. #region Overrides for Get Dom
  426. public XMLHelper getDOM(IDataReader oRS)
  427. {
  428. return getDOM(oRS, 0, 2000);
  429. }
  430. /// <summary>
  431. /// Gets a DOM of a recordset
  432. /// </summary>
  433. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  434. /// <returns>An XMLHelper representation of the recordset in the format response/rows/row</returns>
  435. public XMLHelper getDOM(IDataReader oRS, int iStartRow, int iMaxRows)
  436. {
  437. return appendToDOM(null, null, null, null, oRS, "rows", "row", iStartRow, iMaxRows, true, RefCurPosition.First, false, true, false);
  438. }
  439. /// <summary>
  440. /// Gets a DOM of a recordset
  441. /// </summary>
  442. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  443. /// <returns>An XMLHelper representation of the recordset in the format response/rows/row</returns>
  444. public XMLHelper getDOM(String zSQL)
  445. {
  446. _Command = createSPParams(zSQL);
  447. return getDOM(_Command, "rows", "row");
  448. }
  449. /// <summary>
  450. /// Gets a DOM of a recordset
  451. /// </summary>
  452. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  453. /// <returns>An XMLHelper representation of the recordset in the format response/rows/row</returns>
  454. public XMLHelper getDOM(DBParams oCommand, String zRowsetName, String zRowName)
  455. {
  456. return getDOM(oCommand, 0, 0, false, RefCurPosition.First, zRowsetName, zRowName, false, true, null, null, null, null, false);
  457. }
  458. /// <summary>
  459. /// Gets a DOM of a recordset
  460. /// </summary>
  461. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  462. /// <returns>An XMLHelper representation of the recordset in the format response/rows/row</returns>
  463. public XMLHelper getDOM(DBParams oCommand, Boolean withRefCur)
  464. {
  465. RefCurPosition refCurPosition = RefCurPosition.None;
  466. if (withRefCur)
  467. refCurPosition = RefCurPosition.First;
  468. return getDOM(oCommand, 0, 0, false, refCurPosition, "rows", "row", false, true, null, null, null, null, false);
  469. }
  470. /// <summary>
  471. /// Gets a XML DOM of a recordset
  472. /// </summary>
  473. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  474. /// <param name="iStartRow">The start row number to retrieve</param>
  475. /// <param name="iMaxRows">The maximum number of rows</param>
  476. /// <param name="bShowTotal">Determines whether the total Rowcount is shown.</param>
  477. /// <param name="zRowsetName">The Rowset node name.</param>
  478. /// <param name="zRowName">The Row node name</param>
  479. /// <param name="bShowMetadata">Determines whether to create meta data info</param>
  480. /// <returns>An XMLHelper representation of the recordset</returns>
  481. public XMLHelper getDOM(DBParams oCommand, int iStartRow, int iMaxRows, Boolean bShowTotal, DBHelper.RefCurPosition refCurPosition,
  482. String zRowsetName, String zRowName, Boolean bShowMetadata, Boolean bCloseConnection,
  483. XMLHelper oInputDOM, String zNode, String zKey, String zGrouping, Boolean CheckRowName)
  484. {
  485. //Time the processing...
  486. DateTime dStart = DateTime.Now;
  487. //Execute the database Command...
  488. XMLHelper oResponse = appendToDOM(oInputDOM, zNode, zKey, zGrouping, oCommand, zRowsetName, zRowName, iStartRow, iMaxRows, bShowTotal, refCurPosition, bShowMetadata, false, CheckRowName);
  489. //Add the execution time to the ouptut...
  490. TimeSpan tDiff = DateTime.Now.Subtract(dStart);
  491. oResponse.appendAttribute("duration", tDiff.TotalMilliseconds.ToString());
  492. //Close the connection if required...
  493. if (bCloseConnection)
  494. {
  495. oCommand.CloseConnection();
  496. }
  497. return oResponse;
  498. }
  499. #endregion
  500. #region Overrides for Append To DOM
  501. /// <summary>
  502. /// Executes a Command and adds the recordset to an existing DOM
  503. /// </summary>
  504. /// <param name="oInputDom">Dom to append to</param>
  505. /// <param name="zNode">Xpath to the node to append information to</param>
  506. /// <param name="zKey">Key to match</param>
  507. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  508. /// <param name="zRowsetName">The Rowset node name</param>
  509. /// <param name="zRowName">The row Node Name</param>
  510. public void appendToDOM(XMLHelper oInputDom, String zNode, String zKey, DBParams oCommand, String zRowsetName, String zRowName)
  511. {
  512. appendToDOM(oInputDom, zNode, zKey, null, oCommand, zRowsetName, zRowName, 0, 9999999,
  513. false, (oCommand.Command.CommandType == CommandType.Text ? RefCurPosition.None : RefCurPosition.First), false, false, false);
  514. //appendToDOM(oInputDom, zNode, zKey, oCommand, zRowsetName, zRowName, 0, 9999999, false, false);
  515. }
  516. /// <summary>
  517. /// Executes a Command and adds the recordset to an existing DOM
  518. /// </summary>
  519. /// <param name="oInputDom">Dom to append to</param>
  520. /// <param name="zNode">Xpath to the node to append information to</param>
  521. /// <param name="zKey">Key to match</param>
  522. /// <param name="oParams">Parameters to pass to the stored procedure</param>
  523. /// <param name="zRowsetName">The Rowset node name</param>
  524. /// <param name="zRowName">The row Node Name</param>
  525. /// <param name="iStartRow">The start row number</param>
  526. /// <param name="iMaxRows">The maximun number of rows to fetch</param>
  527. /// <param name="bShowTotal">Determines whether totals are shown</param>
  528. /// <param name="bShowMetadata">Determines whether metadata is to be shown</param>
  529. ///
  530. public void appendToDOM(XMLHelper oInputDom, String zNode, String zKey, DBParams oParams, String zRowsetName, String zRowName, int iStartRow, int iMaxRows, Boolean bShowTotal, Boolean bShowMetadata)
  531. {
  532. appendToDOM(oInputDom, zNode, zKey, null, oParams, zRowsetName, zRowName, iStartRow, iMaxRows,
  533. bShowTotal, RefCurPosition.None, false, false, false);
  534. }
  535. /// <summary>
  536. /// Executes a Command and adds the recordset to an existing DOM
  537. /// JF 12/2007 - Generalised to take an IDbCommand, allowing it to be used with either an
  538. /// OleDbCommand or OleDbCommand
  539. /// </summary>
  540. /// <param name="oInputDom">Dom to append to</param>
  541. /// <param name="zNode">Xpath to the node to append information to</param>
  542. /// <param name="zKey">Key to match</param>
  543. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  544. /// <param name="zRowsetName">The Rowset node name</param>
  545. /// <param name="zRowName">The row Node Name</param>
  546. /// <param name="iStartRow">The start row number</param>
  547. /// <param name="iMaxRows">The maximun number of rows to fetch</param>
  548. /// <param name="bShowTotal">Determines whether totals are shown</param>
  549. /// <param name="bShowMetadata">Determines whether metadata is to be shown</param>
  550. /// <param name="bCloseConnection">Whether to close the Commands connection when finished exection</param>
  551. /// <param name="CheckRowName">This is an override to force a check of the row name against any xpath which
  552. /// may have been defined in the column name from the procedure/package. It's function is to avoid the creation
  553. /// of a dom structure such as rows/row/row if the xpath in the procedure reads row/@value. If the first component
  554. /// of the xpath in the procedure matches the rowname specified in the call to this method, then it will assume
  555. /// that you don't want to append a new child with the same name. It will still allow the creation of
  556. /// rows/row/childrow if the procedure reads childrow/@value.</param>
  557. public XMLHelper appendToDOM(XMLHelper oInputDom, String zNode, String zKey, String zGrouping, DBParams oParams,
  558. String zRowsetName, String zRowName, int iStartRow, int iMaxRows,
  559. Boolean bShowTotal, DBHelper.RefCurPosition refCurPosition, Boolean bShowMetadata, Boolean bCloseConnection,
  560. Boolean CheckRowName)
  561. {
  562. #region Execute the database Command
  563. //Execute the Command and obtain the output recordset...
  564. OleDbCommand oCommand = oParams.AsOleDbCommand();
  565. //oCommand.Prepare();
  566. CommandBehavior cb = CommandBehavior.Default;
  567. if (bCloseConnection)
  568. {
  569. cb = CommandBehavior.CloseConnection;
  570. }
  571. IDataReader oRS = oCommand.ExecuteReader(cb);
  572. #endregion
  573. //Return the response (the input dom may also have been appended to inline)...
  574. return appendToDOM(oInputDom, zNode, zKey, zGrouping, oRS, zRowsetName, zRowName, iStartRow, iMaxRows, bShowTotal, refCurPosition, bShowMetadata, bCloseConnection, CheckRowName);
  575. }
  576. public XMLHelper appendToDOM(XMLHelper oInputDom, String zNode, String zKey, String zGrouping, IDataReader oRS,
  577. String zRowsetName, String zRowName, int iStartRow, int iMaxRows,
  578. Boolean bShowTotal, DBHelper.RefCurPosition refCurPosition, Boolean bShowMetadata, Boolean bCloseConnection,
  579. Boolean CheckRowName)
  580. {
  581. //Establish output vars...
  582. XMLHelper oResponse = new XMLHelper("response");
  583. //XMLHelper oResponse = (oInputDom == null || oInputDom.getName() == "request") ? new XMLHelper("response") : oInputDom;
  584. XMLHelper oInRowGroup = null;
  585. XMLHelper oOutRowGroup = null;
  586. XMLHelper oRow = null;
  587. String zNodeValue;
  588. Int32 lFields = oRS.FieldCount;
  589. String[] vValue = null;
  590. Int32 lGroupCols = 0;
  591. String zGroupNode = null;
  592. if (zGrouping != "" && zGrouping != null)
  593. {
  594. vValue = zGrouping.Split(":".ToCharArray());
  595. lGroupCols = Convert.ToInt32(vValue[0]);
  596. zGroupNode = vValue[1];
  597. }
  598. //Determine whether to create a grouping node...
  599. if (zRowsetName != "" && zRowsetName != null)
  600. {
  601. //Append a grouping node to the response to hold child nodes...
  602. oOutRowGroup = oResponse.appendNode(zRowsetName);
  603. }
  604. else
  605. {
  606. //Appending child nodes directly to the response root, rather than a grouping node...
  607. oOutRowGroup = oResponse;
  608. }
  609. // Create a collection of keys in the DOM
  610. Dictionary<String, XMLHelper> oColKeys = new Dictionary<String, XMLHelper>();
  611. if (zNode != "" && zNode != null)
  612. {
  613. XMLHelper[] oNodes = oInputDom.getNodes(zNode);
  614. for (int i = 0; i < oNodes.Length; i++)
  615. {
  616. XMLHelper oNode = oNodes[i];
  617. if (zRowsetName != "" && zRowsetName != null)
  618. {
  619. oInRowGroup = oNode.appendNode(zRowsetName);
  620. }
  621. else
  622. {
  623. oInRowGroup = oNode;
  624. }
  625. if (zKey != "" && zKey != null)
  626. oColKeys.Add(oNodes[i].getValue(zKey), oInRowGroup);
  627. }
  628. }
  629. //If you are required to append "Metadata", then
  630. //append column names and types...
  631. if (bShowMetadata)
  632. {
  633. XMLHelper oMeta = oResponse.appendNode("meta_data");
  634. XMLHelper oCols = oMeta.appendNode("columns");
  635. for (int i = 0; i < oRS.FieldCount; i++)
  636. {
  637. XMLHelper oNode = oCols.appendNode("column");
  638. oNode.appendAttribute("name", oRS.GetName(i).ToLower());
  639. oNode.appendAttribute("type", oRS.GetDataTypeName(i));
  640. }
  641. }
  642. #region Deal with paging
  643. // Set the start row and max rows. Unless maxrows has been explicitly
  644. //defined then default to a safe value (2000 in this case)...
  645. int iRow = iStartRow;
  646. if (iMaxRows == 0)
  647. iMaxRows = Int32.MaxValue;
  648. //iMaxRows = 2000;
  649. //If you are "paging", then a startrow will be provided, so
  650. //advance to the startrow of this page...
  651. if (iStartRow > 0)
  652. {
  653. for (int i = 0; i < iStartRow; i++)
  654. oRS.Read();
  655. }
  656. #endregion
  657. String zCurrRowKey = null;
  658. String zPrevRowKey = null;
  659. Boolean bFirst = true;
  660. XMLHelper oTempNode = null;
  661. Int32 lField = 0;
  662. // Loop through the recordset and construct the XML DOM
  663. while (oRS.Read() && iRow < (iMaxRows + iStartRow))
  664. {
  665. if (iRow >= iStartRow)
  666. {
  667. bFirst = true;
  668. // Get the "Key" for this row. Used to determine whether this should be a new grouping.
  669. zCurrRowKey = null;
  670. for (lField = 0; lField <= lGroupCols - 1; lField++)
  671. {
  672. zCurrRowKey += (string)oRS[lField].ToString() + "-";
  673. }
  674. if (!string.IsNullOrEmpty(zKey))
  675. {
  676. oInRowGroup = oColKeys[oRS[0].ToString()];
  677. }
  678. //If you aren't grouping, or if this key is new...
  679. if (lGroupCols == 0 || zCurrRowKey != zPrevRowKey)
  680. {
  681. //Create a new node...
  682. oRow = new XMLHelper(zRowName);
  683. if (oInRowGroup != null)
  684. {
  685. oRow = oInRowGroup.appendNode(oRow);
  686. }
  687. oTempNode = oRow;
  688. }
  689. lField = 0;
  690. for (int i = 0; i < oRS.FieldCount; i++)
  691. {
  692. //DataRow oField = oRS.[i];
  693. if (lField >= lGroupCols || zCurrRowKey != zPrevRowKey)
  694. {
  695. //vValue = oField.Value;
  696. zNodeValue = "";
  697. // Get the data from the field
  698. if (oRS.GetFieldType(i).Name =="DateTime")
  699. {
  700. if (oRS.IsDBNull(i))
  701. zNodeValue = "";
  702. else
  703. zNodeValue = oRS.GetDateTime(i).ToString(_pzDateFormat);
  704. }
  705. else
  706. {
  707. if (oRS.IsDBNull(i))
  708. zNodeValue = "";
  709. else
  710. zNodeValue = oRS[i].ToString();
  711. }
  712. String zNodeName = oRS.GetName(i).ToLower().Replace("\"", "");
  713. if (_pbUppercaseNodes)
  714. zNodeName = zNodeName.ToUpper();
  715. //This is an override to check the row name during an append.
  716. //See procedure definition comment for an explanation...
  717. if (CheckRowName && zNodeName.Split("/".ToCharArray()).Length > 1)
  718. {
  719. if (zRowName == zNodeName.Split("/".ToCharArray())[0])
  720. zNodeName = zNodeName.Replace(zRowName + "/", "");
  721. }
  722. // Add the field to the current row
  723. if (bFirst & !string.IsNullOrEmpty(zKey))
  724. {
  725. //Do Nothing...
  726. }
  727. else if (lGroupCols == lField & lGroupCols > 0)
  728. {
  729. oRow = oRow.appendNode(zGroupNode);
  730. oRow.appendXPathNode(zNodeName, zNodeValue, false);
  731. }
  732. else
  733. {
  734. //if this is a value node rather than an attribute,
  735. //and if it is a match to the node name...
  736. if (CheckRowName &&
  737. (zNodeName == zRowName) &&
  738. !zNodeName.Contains("@")
  739. )
  740. {
  741. //Just set the node value...
  742. oRow.setValue(zNodeValue);
  743. }
  744. else
  745. {
  746. if (zNodeName == "xml-data" || zNodeName.EndsWith("_xml"))
  747. oRow.appendNode(new XMLHelper("<" + zNodeName + ">" + zNodeValue + "</" + zNodeName + ">"));
  748. else
  749. oRow.appendXPathNode(zNodeName, zNodeValue, false);
  750. }
  751. }
  752. }
  753. bFirst = false;
  754. lField = lField + 1;
  755. }
  756. oRow = oTempNode;
  757. //If you are dom merging, append to the inrowgroup...
  758. //if (oInRowGroup != null)
  759. // oInRowGroup.appendNode(oRow);
  760. //Always append to the outrowgroup...
  761. oOutRowGroup.appendNode(oRow);
  762. zPrevRowKey = zCurrRowKey;
  763. }
  764. iRow++;
  765. }
  766. // If total is to be shown move to the end of the recordset
  767. if (bShowTotal)
  768. {
  769. //Need to read the remaining rows in the recordset to count rows...
  770. bool additionalRowsCounted = false;
  771. while (oRS.Read() && iRow < 2000)
  772. {
  773. additionalRowsCounted = true;
  774. iRow++;
  775. }
  776. //if ((iStartRow == 0) && (additionalRowsCounted))
  777. // iRow++;
  778. //if (iStartRow > 0)
  779. // iRow--;
  780. if (additionalRowsCounted)
  781. iRow++;
  782. //Append the rowcount to the returned results...
  783. oResponse.appendAttribute("total_rows", Convert.ToString(iRow));
  784. //oResponse.appendNode("total_rows", Convert.ToString(iRow));
  785. }
  786. //Always close the recordset...
  787. oRS.Close();
  788. //Return the response (the input dom may also have been appended to inline)...
  789. return oResponse;
  790. }
  791. public XMLHelper getTables(String zSchema)
  792. {
  793. XMLHelper oResponse = null;
  794. try
  795. {
  796. DataTable dt = getConnection().GetSchema("Tables", new String[] {zSchema});
  797. oResponse = getDOM(dt);
  798. }
  799. catch (Exception ex)
  800. {
  801. }
  802. return oResponse;
  803. }
  804. public XMLHelper getColumns(String zTable)
  805. {
  806. XMLHelper oResponse = null;
  807. try
  808. {
  809. OleDbCommand cmd = new OleDbCommand("select * from " + zTable, getConnection());
  810. IDataReader rs = cmd.ExecuteReader();
  811. oResponse = getDOM(rs.GetSchemaTable());
  812. }
  813. catch (Exception ex)
  814. {
  815. }
  816. return oResponse;
  817. }
  818. public XMLHelper getDOM(DataTable dt)
  819. {
  820. XMLHelper oResponse = new XMLHelper("response");
  821. XMLHelper oRows = oResponse.appendNode("rows");
  822. foreach (DataRow dr in dt.Rows)
  823. {
  824. XMLHelper oRow = oRows.appendNode("row");
  825. foreach (DataColumn dc in dt.Columns)
  826. {
  827. oRow.appendNode(dc.ColumnName.ToLower(), dr[dc].ToString());
  828. }
  829. }
  830. return oResponse;
  831. }
  832. #endregion
  833. #region Overrides for Get XML
  834. public enum RefCurPosition
  835. {
  836. First,
  837. Last,
  838. None
  839. }
  840. /// <summary>
  841. /// Gets an XML String of a recordset
  842. /// </summary>
  843. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  844. /// <returns>An XML string representation of the recordset in the format response/rows/row</returns>
  845. public String getXML(DBParams oCommand)
  846. {
  847. return getDOM(oCommand, true).getXML();
  848. }
  849. public String getXML(DBParams oCommand, Boolean withRefCur)
  850. {
  851. return getDOM(oCommand, withRefCur).getXML();
  852. }
  853. /// <summary>
  854. /// Gets an XML String of a recordset
  855. /// </summary>
  856. /// <param name="oCommand">Parameters to pass to the stored procedure</param>
  857. /// <param name="iStartRow">The start row number to retrieve</param>
  858. /// <param name="iMaxRows">The maximum number of rows</param>
  859. /// <param name="bShowTotal">Determines whether the total Rowcount is shown.</param>
  860. /// <param name="zRowsetName">The Rowset node name.</param>
  861. /// <param name="zRowName">The Row node name</param>
  862. /// <param name="bShowMetadata">Determines whether to create meta data info</param>
  863. /// <returns>An XML string representation of the recordset</returns>
  864. public String getXML(DBParams oCommand, int iStartRow, int iMaxRows, Boolean bShowTotal, String zRowsetName, String zRowName, Boolean bShowMetadata)
  865. {
  866. return getXML(oCommand, iStartRow, iMaxRows, bShowTotal, zRowsetName, zRowName, bShowMetadata, true);
  867. }
  868. public XMLHelper getXMLHelper(DBParams oCommand, int iStartRow, int iMaxRows, Boolean bShowTotal, String zRowsetName, String zRowName, Boolean bShowMetadata)
  869. {
  870. return getXMLHelper(oCommand, iStartRow, iMaxRows, bShowTotal, zRowsetName, zRowName, bShowMetadata, true);
  871. }
  872. public String getXML(DBParams oCommand, int iStartRow, int iMaxRows, Boolean bShowTotal, String zRowsetName, String zRowName, Boolean bShowMetadata, Boolean bCloseConnection)
  873. {
  874. return getXML(oCommand, iStartRow, iMaxRows, bShowTotal, RefCurPosition.First, zRowsetName, zRowName, bShowMetadata, bCloseConnection, null, null, null, null, false).getXML();
  875. }
  876. public XMLHelper getXMLHelper(DBParams oCommand, int iStartRow, int iMaxRows, Boolean bShowTotal, String zRowsetName, String zRowName, Boolean bShowMetadata, Boolean bCloseConnection)
  877. {
  878. return getXML(oCommand, iStartRow, iMaxRows, bShowTotal, RefCurPosition.First, zRowsetName, zRowName, bShowMetadata, bCloseConnection, null, null, null, null, false);
  879. }
  880. public XMLHelper getXML(DBParams oCommand, int iStartRow, int iMaxRows, Boolean bShowTotal, DBHelper.RefCurPosition refCurPosition, String zRowsetName, String zRowName, Boolean bShowMetadata, Boolean bCloseConnection, XMLHelper oInputDOM, String zNode, String zKey, String zGrouping, Boolean CheckRowName)
  881. {
  882. return getDOM(oCommand, iStartRow, iMaxRows, bShowTotal, refCurPosition, zRowsetName, zRowName, bShowMetadata, bCloseConnection, oInputDOM, zNode, zKey, zGrouping, CheckRowName);
  883. }
  884. /// <summary>
  885. /// High performance version of append to dom.
  886. /// Will always add columns as attributes.
  887. /// </summary>
  888. public String getXML(DBParams oParams, String zRowsetName, String zRowName, Boolean bCloseConnection)
  889. {
  890. return getXML(oParams, zRowsetName, zRowName, bCloseConnection, null, null, null, RefCurPosition.First);
  891. }
  892. public String getXML(DBParams oParams, String zRowsetName, String zRowName, Boolean bCloseConnection, Int32? StartRow, Int32? MaxRows, Boolean? ShowTotal, RefCurPosition refCurPosition)
  893. {
  894. return getXMLStringBuilder(oParams, zRowsetName, zRowName, bCloseConnection, StartRow, MaxRows, ShowTotal, refCurPosition).ToString();
  895. }
  896. public XMLHelper getXMLHelper(DBParams oParams, String zRowsetName, String zRowName, Boolean bCloseConnection, Int32? StartRow, Int32? MaxRows, Boolean? ShowTotal, RefCurPosition refCurPosition)
  897. {
  898. return new XMLHelper(getXMLStringBuilder(oParams, zRowsetName, zRowName, bCloseConnection, StartRow, MaxRows, ShowTotal, refCurPosition));
  899. }
  900. /// <summary>
  901. /// High performance version of append to dom.
  902. /// Will always add columns as attributes.
  903. /// </summary>
  904. private StringBuilder getXMLStringBuilder(DBParams oParams, String zRowsetName, String zRowName, Boolean bCloseConnection, Int32? StartRow, Int32? MaxRows, Boolean? ShowTotal, RefCurPosition refCurPosition)
  905. {
  906. //oParams.Command.BindByName = true;
  907. addRefCur(refCurPosition, oParams);
  908. //Execute the procedure...
  909. //System.Data.IDbCommand oCommand = oParams.AsIDbCommand();
  910. OleDbCommand oCommand = oParams.Command;
  911. IDataReader oRS = null;
  912. if (bCloseConnection)
  913. {
  914. oRS = oCommand.ExecuteReader(CommandBehavior.CloseConnection);
  915. }
  916. else
  917. {
  918. oRS = oCommand.ExecuteReader();
  919. }
  920. //Get the number of columns...
  921. Int32 FieldCount = oRS.FieldCount;
  922. //Get the column names (in advance of the loop, so it doesn't
  923. //need to read the rowset every time through...
  924. Dictionary<Int32, String> oColumnNames = new Dictionary<Int32, String>();
  925. String ColName = null;
  926. for (int i = 0; i < FieldCount; i++)
  927. {
  928. ColName = oRS.GetName(i).ToLower();
  929. if (ColName.StartsWith("@"))
  930. {
  931. ColName = ColName.Substring(1);
  932. }
  933. oColumnNames.Add(i, ColName);
  934. }
  935. StringWriter sw = new StringWriter();
  936. XmlTextWriter xtw = new XmlTextWriter(sw);
  937. //Append a placeholder for the rows...
  938. xtw.WriteStartElement("response");
  939. if (zRowsetName != null)
  940. xtw.WriteStartElement(zRowsetName);
  941. //Paging setup...
  942. if (StartRow == null) StartRow = 0;
  943. if (MaxRows == null) MaxRows = 0;
  944. if (MaxRows == 0) MaxRows = int.MaxValue;
  945. //Loop through every row in the rs, adding rows and cols appropriately...
  946. Int32 rowCount = 0;
  947. Boolean WriteRow = true;
  948. while (oRS.Read())
  949. {
  950. //If you are at the start row...
  951. if (rowCount >= StartRow && WriteRow)
  952. {
  953. //Add a new row...
  954. xtw.WriteStartElement(zRowName);
  955. for (int i = 0; i < FieldCount; i++)
  956. {
  957. // Add the field to the current row
  958. xtw.WriteAttributeString(oColumnNames[i].ToString(), oRS[i].ToString());
  959. }
  960. xtw.WriteEndElement();
  961. //If you have reached the max number of rows you want to paint, then stop painting...
  962. if ((rowCount - StartRow) >= (MaxRows-1))
  963. WriteRow = false;
  964. }
  965. rowCount++;
  966. }
  967. if (zRowsetName != null)
  968. {
  969. xtw.WriteElementString("total_rows", rowCount.ToString());
  970. xtw.WriteEndElement();
  971. }
  972. xtw.WriteElementString("total_rows", rowCount.ToString());
  973. xtw.WriteEndElement();
  974. //Close the rs...
  975. oRS.Close();
  976. //Return the built dom...
  977. return sw.GetStringBuilder();
  978. }
  979. public StringBuilder getCSV(DBParams oParams, String zRowsetName, String zRowName,
  980. Boolean bCloseConnection, Int32? StartRow, Int32? MaxRows, Boolean? ShowTotal, RefCurPosition refCurPosition,
  981. out long rowCount)
  982. {
  983. //oParams.Command.BindByName = true;
  984. addRefCur(refCurPosition, oParams);
  985. //Execute the procedure...
  986. //System.Data.IDbCommand oCommand = oParams.AsIDbCommand();
  987. OleDbCommand oCommand = oParams.Command;
  988. OleDbDataReader oRS = null;
  989. if (bCloseConnection)
  990. {
  991. oRS = oCommand.ExecuteReader(CommandBehavior.CloseConnection);
  992. }
  993. else
  994. {
  995. oRS = oCommand.ExecuteReader();
  996. }
  997. //Get the number of columns...
  998. Int32 FieldCount = oRS.FieldCount;
  999. StringWriter sw = new StringWriter();
  1000. //Get the column names (in advance of the loop, so it doesn't
  1001. //need to read the rowset every time through...
  1002. Dictionary<Int32, String> oColumnNames = new Dictionary<Int32, String>();
  1003. String ColName = null;
  1004. for (int i = 0; i < FieldCount; i++)
  1005. {
  1006. ColName = oRS.GetName(i).ToLower();
  1007. if (ColName.StartsWith("@"))
  1008. {
  1009. ColName = ColName.Substring(1);
  1010. }
  1011. oColumnNames.Add(i, ColName);
  1012. sw.Write(ColName);
  1013. if (i < (FieldCount - 1))
  1014. sw.Write(",");
  1015. }
  1016. sw.Write(Environment.NewLine);
  1017. //Paging setup...
  1018. if (StartRow == null) StartRow = 0;
  1019. if (MaxRows == null) MaxRows = 0;
  1020. if (MaxRows == 0) MaxRows = int.MaxValue;
  1021. Type type;
  1022. //Loop through every row in the rs, adding rows and cols appropriately...
  1023. rowCount = 0;
  1024. Boolean WriteRow = true;
  1025. while (oRS.Read())
  1026. {
  1027. //If you are at the start row...
  1028. if (rowCount >= StartRow && WriteRow)
  1029. {
  1030. for (int i = 0; i < FieldCount; i++)
  1031. {
  1032. type = oRS[i].GetType();
  1033. if ((type == typeof(String)))
  1034. sw.Write("\"");
  1035. else if (oColumnNames[i] == "ean_code") //always output ean codes as text to avoid conversion by excel to exponents
  1036. sw.Write("=\"");
  1037. sw.Write(oRS[i].ToString());
  1038. if (type == typeof(String))
  1039. sw.Write("\"");
  1040. else if (oColumnNames[i] == "ean_code") //always output ean codes as text to avoid conversion by excel to exponents
  1041. sw.Write("\"");
  1042. if (i < (FieldCount-1))
  1043. sw.Write(",");
  1044. }
  1045. sw.Write(Environment.NewLine);
  1046. //If you have reached the max number of rows you want to paint, then stop painting...
  1047. if ((rowCount - StartRow) >= (MaxRows - 1))
  1048. WriteRow = false;
  1049. }
  1050. rowCount++;
  1051. }
  1052. //Close the rs...
  1053. oRS.Close();
  1054. //Return the built csv...
  1055. return sw.GetStringBuilder();
  1056. }
  1057. /// <summary>
  1058. /// Adds a param for a returnable refcursor based on the set refCurPosition
  1059. /// </summary>
  1060. /// <param name="refCurPosition">The position to add the refcur in</param>
  1061. /// <param name="oParams">The object to add the refcur to</param>
  1062. private void addRefCur(RefCurPosition refCurPosition, DBParams oParams)
  1063. {
  1064. //Add a refcursor output param if there isn't already one added...
  1065. if ( (refCurPosition != RefCurPosition.None) && (oParams.Command == null || !oParams.Command.Parameters.Contains("poRefCur")) )
  1066. {
  1067. Int32? refCurIndex = null;
  1068. switch (refCurPosition)
  1069. {
  1070. case RefCurPosition.First:
  1071. {
  1072. refCurIndex = 0;
  1073. break;
  1074. }
  1075. case RefCurPosition.Last:
  1076. {
  1077. refCurIndex = oParams.Command.Parameters.Count;
  1078. break;
  1079. }
  1080. }
  1081. //Append the output refcursor param...
  1082. OleDbParameter oRefCursor = new OleDbParameter("poRefCur", OleDbType.IDispatch);
  1083. if (pzSystem == "SECURITY")
  1084. {
  1085. oRefCursor.Direction = ParameterDirection.ReturnValue;
  1086. }
  1087. else
  1088. {
  1089. oRefCursor.Direction = ParameterDirection.Output;
  1090. }
  1091. oParams.Command.Parameters.Insert(refCurIndex.Value, oRefCursor);
  1092. }
  1093. }
  1094. #endregion
  1095. }
  1096. }