DataHelper.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. using NPOI.SS.Formula.Functions;
  2. using Oracle.ManagedDataAccess.Client;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Text;
  7. using UAS_MES_NEW.PublicMethod;
  8. namespace UAS_MES_NEW.DataOperate
  9. {
  10. class DataHelper
  11. {
  12. //系统默认的的连接字符串
  13. private string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=81.71.42.91)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  14. //用户选择的数据库的连接字符串
  15. public static string DBConnectionString;
  16. public static OracleConnection connection = null;
  17. OracleCommand command = null;
  18. int ReconnectTime = 0;
  19. /// <summary>
  20. /// 执行构造函数的时候打开数据库的链接
  21. /// </summary>
  22. public DataHelper()
  23. {
  24. try
  25. {
  26. //如果选择的是默认数据则直接用配置文件的信息连接,否则选择数据库的账套信息
  27. if (DBConnectionString == null || DBConnectionString == ConnectionStrings)
  28. connection = new OracleConnection(ConnectionStrings);
  29. else
  30. connection = new OracleConnection(DBConnectionString);
  31. connection.Open();
  32. }
  33. catch (Exception e) { LogManager.DoLog(e.Message); }
  34. }
  35. /// <summary>
  36. /// 根据表名获取该表字段数据类型
  37. /// </summary>
  38. public DataTable GetColumnDataType(string TableName)
  39. {
  40. DataTable dt = new DataTable();
  41. command = new OracleCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", connection);
  42. Reconnect(command);
  43. OracleDataAdapter ad = new OracleDataAdapter(command);
  44. ad.Fill(dt);
  45. ad.Dispose();
  46. return dt;
  47. }
  48. /// <summary>
  49. /// 获取第一行第一列的信息
  50. /// </summary>
  51. public object getFieldDataByCondition(string TableName, string Field, string Condition)
  52. {
  53. DataTable dt = new DataTable();
  54. string sql = "select " + Field + " from " + TableName + " where " + Condition;
  55. command = new OracleCommand(sql, connection);
  56. Console.WriteLine(sql);
  57. Reconnect(command);
  58. OracleDataAdapter ad = new OracleDataAdapter();
  59. ad.SelectCommand = command;
  60. try
  61. {
  62. ad.Fill(dt);
  63. }
  64. catch (Exception)
  65. {
  66. connection = new OracleConnection(DBConnectionString);
  67. connection.Open();
  68. command = new OracleCommand(sql, connection);
  69. ad = new OracleDataAdapter();
  70. ad.SelectCommand = command;
  71. ad.Fill(dt);
  72. }
  73. ad.Dispose();
  74. if (dt.Rows.Count > 0)
  75. {
  76. return dt.Rows[0][0];
  77. }
  78. else
  79. {
  80. return "";
  81. }
  82. }
  83. /// <summary>
  84. /// 执行打印的SQL
  85. /// </summary>
  86. /// <param name="SQL">SQL语句</param>
  87. /// <param name="Parameters">动态添加的参数,主要根据条码枪扫描获取</param>
  88. /// <returns></returns>
  89. public object ExecutePrintSQL(string SQL, params string[] Parameters)
  90. {
  91. //按照?拆分数据,然后以:Param替换问号,同时添加参数
  92. string[] Param = SQL.Split('?');
  93. int ParamNum = Param.Length - 1;
  94. //条码打印必然存在需要维护的参数
  95. if (ParamNum > 0)
  96. {
  97. StringBuilder sb = new StringBuilder();
  98. for (int i = 0; i < ParamNum; i++)
  99. {
  100. sb.Append(Param[i] + ":Param" + i);
  101. }
  102. command = new OracleCommand(sb.ToString(), connection);
  103. for (int i = 0; i < ParamNum; i++)
  104. {
  105. command.Parameters.Add("Param" + i, OracleDbType.Varchar2, Parameters[i], ParameterDirection.Input);
  106. }
  107. OracleDataAdapter ad = new OracleDataAdapter(command);
  108. DataTable dt = new DataTable();
  109. ad.Fill(dt);
  110. ad.Dispose();
  111. return dt;
  112. }
  113. return "参数错误,请检查SQL语句";
  114. }
  115. /// <summary>
  116. /// 获取指定表的记录的条数 ,带条件
  117. /// </summary>
  118. /// <returns></returns>
  119. public int getRowCount(string TableName, string Condition)
  120. {
  121. DataTable dt = new DataTable();
  122. string sql = "select count(1) from " + TableName + " where " + Condition;
  123. command = new OracleCommand(sql, connection);
  124. Reconnect(command);
  125. OracleDataAdapter ad = new OracleDataAdapter(command);
  126. ad.Fill(dt);
  127. ad.Dispose();
  128. return int.Parse(dt.Rows[0][0].ToString());
  129. }
  130. /// <summary>
  131. /// 获取指定表的记录的条数 ,不带条件
  132. /// </summary>
  133. /// <param name="TableName"></param>
  134. /// <returns></returns>
  135. public int getRowCount(string TableName)
  136. {
  137. DataTable dt = new DataTable();
  138. string sql = "select count(1) from " + TableName;
  139. command = new OracleCommand(sql, connection);
  140. Reconnect(command);
  141. OracleDataAdapter ad = new OracleDataAdapter(command);
  142. ad.Fill(dt);
  143. ad.Dispose();
  144. return int.Parse(dt.Rows[0][0].ToString());
  145. }
  146. /// <summary>
  147. /// 通过表名和获取单行的记录
  148. /// </summary>
  149. public DataTable getFieldsDataByCondition(string TableName, string[] Fields, string Condition)
  150. {
  151. DataTable dt = new DataTable();
  152. string sql = "select ";
  153. sql += AddField(Fields);
  154. sql += " from " + TableName + " where " + Condition + " and rownum=1";
  155. command = new OracleCommand(sql, connection);
  156. Reconnect(command);
  157. OracleDataAdapter ad = new OracleDataAdapter(command);
  158. try
  159. {
  160. ad.Fill(dt);
  161. }
  162. catch (Exception)
  163. {
  164. connection = new OracleConnection(DBConnectionString);
  165. connection.Open();
  166. command = new OracleCommand(sql, connection);
  167. ad = new OracleDataAdapter();
  168. ad.SelectCommand = command;
  169. ad.Fill(dt);
  170. }
  171. ad.Dispose();
  172. return dt;
  173. }
  174. /// <summary>
  175. /// 将DataTable导入到指定的表中
  176. /// </summary>
  177. /// <param name="DataTable"></param>
  178. /// <param name="TableName"></param>
  179. public void InsertDataTable(DataTable DataTable, string TableName)
  180. {
  181. for (int i = 0; i < DataTable.Rows.Count; i++)
  182. {
  183. for (int j = 0; j < DataTable.Columns.Count; j++)
  184. {
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// 按分页获取数据
  190. /// </summary>
  191. /// <param name="TableName">表名</param>
  192. /// <param name="Fields">查询字段</param>
  193. /// <param name="CurrentPage">当前页面</param>
  194. /// <param name="PageSize">页面展示条数</param>
  195. /// <param name="Caller"></param>
  196. /// <returns></returns>
  197. // SELECT * FROM (SELECT A.* FROM (SELECT* FROM datalist) A WHERE ROWNUM <= 50) WHERE ROWNUM >= 21
  198. public DataTable getFieldsDatasByPageing(string TableName, string Fields, int CurrentPage, int PageSize, string Caller, params string[] condition)
  199. {
  200. DataTable dt = new DataTable();
  201. StringBuilder sql = new StringBuilder();
  202. //先查询出配置出去的列
  203. //获取查询的列
  204. string[] caption = GetCaptionFromField(Fields);
  205. //获取对应列的描述
  206. string[] field = GetField(Fields);
  207. sql.Append(" select * from (select RowNum RN, A.* from (select ");
  208. sql.Append(AddField(caption));
  209. if (condition.Length > 0)
  210. {
  211. if (condition[0] != null && condition[0].Trim() != "")
  212. sql.Append(" from " + TableName + " where " + condition[0] + " ) A where ROWNUM <=" + CurrentPage * PageSize + ") where RN>" + (CurrentPage - 1) * PageSize);
  213. else
  214. sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
  215. }
  216. command = new OracleCommand(sql.ToString(), connection);
  217. Reconnect(command);
  218. OracleDataAdapter ad = new OracleDataAdapter(command);
  219. ad.Fill(dt);
  220. ad.Dispose();
  221. dt.Columns.RemoveAt(0);
  222. foreach (DataColumn dc in dt.Columns)
  223. {
  224. dc.ColumnName = field[dt.Columns.IndexOf(dc)];
  225. dc.Caption = caption[dt.Columns.IndexOf(dc)];
  226. }
  227. return dt;
  228. }
  229. /// <summary>
  230. /// 通过表名,字段和条件获取DataTable类型的数据
  231. /// </summary>
  232. public DataTable getFieldsDatasByCondition(string TableName, string[] Fields, string Condition)
  233. {
  234. DataTable dt = new DataTable();
  235. string sql = "select ";
  236. sql += AddField(Fields);
  237. sql += " from " + TableName + " where " + Condition;
  238. command = new OracleCommand(sql, connection);
  239. Reconnect(command);
  240. OracleDataAdapter ad = new OracleDataAdapter(command);
  241. try
  242. {
  243. ad.Fill(dt);
  244. }
  245. catch (Exception)
  246. {
  247. connection = new OracleConnection(DBConnectionString);
  248. connection.Open();
  249. command = new OracleCommand(sql, connection);
  250. ad = new OracleDataAdapter();
  251. ad.SelectCommand = command;
  252. ad.Fill(dt);
  253. }
  254. ad.Dispose();
  255. return dt;
  256. }
  257. /// <summary>
  258. /// 通过表名,字段获取DataTable类型的数据
  259. /// </summary>
  260. public DataTable getFieldsDatas(string TableName, string Fields)
  261. {
  262. DataTable dt = new DataTable();
  263. string sql = "select ";
  264. sql += Fields;
  265. sql += " from " + TableName;
  266. command = new OracleCommand(sql, connection);
  267. Reconnect(command);
  268. OracleDataAdapter ad = new OracleDataAdapter(command);
  269. ad.SelectCommand = command;
  270. try
  271. {
  272. ad.Fill(dt);
  273. }
  274. catch (Exception)
  275. {
  276. connection = new OracleConnection(DBConnectionString);
  277. connection.Open();
  278. command = new OracleCommand(sql, connection);
  279. ad = new OracleDataAdapter();
  280. ad.SelectCommand = command;
  281. ad.Fill(dt);
  282. }
  283. ad.Dispose();
  284. return dt;
  285. }
  286. /// <summary>
  287. /// 根据DataTable和指定的表名更新数据,如果需要保存新增的数据则需要传递一条Insert的SQL
  288. /// </summary>
  289. /// <param name="DataTable"></param>
  290. /// <param name="TableName"></param>
  291. /// <param name="Condition"></param>
  292. public void UpDateTableByCondition(DataTable DataTable, string TableName, string PrimaryKey, params string[] sql)
  293. {
  294. if (DataTable == null)
  295. {
  296. return;
  297. }
  298. StringBuilder sb = new StringBuilder();
  299. //预防插入的DataTable中存在不属于该表的列,在进行下一步操作之前全部剔除
  300. DataTable data = (DataTable)ExecuteSql("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", "select");
  301. //将所有的字段拼接起来
  302. for (int i = 0; i < data.Rows.Count; i++)
  303. {
  304. sb.Append("#" + data.Rows[i]["Column_Name"].ToString());
  305. }
  306. //移除掉所有不属于该表的列
  307. for (int i = DataTable.Columns.Count - 1; i >= 0; i--)
  308. {
  309. if (!sb.ToString().Contains(DataTable.Columns[i].ColumnName.ToUpper()))
  310. {
  311. DataTable.Columns.RemoveAt(i);
  312. }
  313. }
  314. sb.Clear();
  315. //计算有多少个是新加的行,根据主键为空来进行判断
  316. int NewRowCount = 0;
  317. for (int i = 0; i < DataTable.Rows.Count; i++)
  318. {
  319. if (DataTable.Rows[i][PrimaryKey] == null || DataTable.Rows[i][PrimaryKey].ToString() == "")
  320. {
  321. NewRowCount = NewRowCount + 1;
  322. }
  323. }
  324. if (sql.Length > 0)
  325. {
  326. if (NewRowCount > 0)
  327. {
  328. //获取参数的个数
  329. int paramsNum = sql[0].Split(':').Length - 1;
  330. //解析参数的数据
  331. string[] param = GetParamFromSQL(sql[0]);
  332. //新建一个二维数组去
  333. string[][] param_array = new string[paramsNum][];
  334. //实例化每个一维数组
  335. for (int i = 0; i < paramsNum; i++)
  336. {
  337. param_array[i] = new string[NewRowCount];
  338. }
  339. //设置每列参数的索引
  340. int num = 0;
  341. //变量所有的行,如果有主键为空的则移除,不为空的进行参数的拼接
  342. for (int i = DataTable.Rows.Count - 1; i >= 0; i--)
  343. {
  344. if (DataTable.Rows[i][PrimaryKey] == null || DataTable.Rows[i][PrimaryKey].ToString() == "")
  345. {
  346. //当为新添加行的时候才去设置参数,设置过后索引+1
  347. for (int j = 0; j < paramsNum; j++)
  348. {
  349. param_array[j][num] = DataTable.Rows[i][param[j]].ToString();
  350. }
  351. DataTable.Rows.RemoveAt(i);
  352. num++;
  353. }
  354. }
  355. BatchInsertDataTable(sql[0], param, param_array);
  356. }
  357. }
  358. //不是新增行的启用更新的方法
  359. sb.Append("update " + TableName + " set ");
  360. //拼接语句,特殊处理日期
  361. foreach (DataColumn dc in DataTable.Columns)
  362. {
  363. if (!dc.DataType.ToString().Equals("System.DateTime"))
  364. {
  365. sb.Append(dc.Caption + "=:" + dc.Caption + ",");
  366. }
  367. else
  368. {
  369. sb.Append(dc.Caption + "=:" + dc.Caption + ",");
  370. }
  371. }
  372. sb.Remove(sb.Length - 1, 1);
  373. sb.Append(" where " + PrimaryKey + "=:" + PrimaryKey);
  374. command = new OracleCommand(sb.ToString(), connection);
  375. OracleDataAdapter ad = new OracleDataAdapter(command);
  376. // 参数的长度是DataTable的行数决定的
  377. command.ArrayBindCount = DataTable.Rows.Count;
  378. //默认全部是Varchar2类型的
  379. OracleDbType ob = OracleDbType.Varchar2;
  380. for (int i = 0; i < DataTable.Columns.Count; i++)
  381. {
  382. object[] param = new object[DataTable.Rows.Count];
  383. for (int j = 0; j < DataTable.Rows.Count; j++)
  384. {
  385. DateTime dt = DateTime.Now;
  386. if (DateTime.TryParse(DataTable.Rows[j][i].ToString(), out dt))
  387. {
  388. param[j] = dt;
  389. ob = OracleDbType.Date;
  390. }
  391. else
  392. {
  393. ob = OracleDbType.Varchar2;
  394. param[j] = DataTable.Rows[j][i];
  395. }
  396. }
  397. //添加批量更新的参数
  398. command.Parameters.Add(new OracleParameter(DataTable.Columns[i].Caption, ob, param, ParameterDirection.Input));
  399. }
  400. ad.UpdateCommand = command;
  401. ad.Update(DataTable);
  402. ad.Dispose();
  403. }
  404. /// <summary>
  405. /// 获取DbFind的数据的DataTable的结构
  406. /// </summary>
  407. /// <param name="field"></param>
  408. /// <param name="caller"></param>
  409. /// <returns></returns>
  410. public DataTable GetDbFindDataTable(string field, string caller)
  411. {
  412. string sql = "select * from dbfindsetui where ds_caller='" + caller + "' and ds_whichui='" + field + "'";
  413. DataTable dt = (DataTable)ExecuteSql(sql, "select");
  414. if (dt.Rows.Count != 0)
  415. {
  416. //通过#号分割字段
  417. string[] dbfield = dt.Rows[0]["ds_findtoui"].ToString().Split('#');
  418. string[] cnfield = dt.Rows[0]["ds_dbcaption"].ToString().Split('#');
  419. //获取查询要查询的Table
  420. string dbtable = dt.Rows[0]["ds_tables"].ToString();
  421. //拼接查询的字段
  422. for (int i = 0; i < dbfield.Length; i++)
  423. {
  424. dbfield[i] = dbfield[i].Split(',')[0];
  425. }
  426. //新建一个空的DataTable
  427. DataTable dt1 = new DataTable();
  428. //往空的DataTable添加结构,ColumnName是中文,Caption是实际的字段名称
  429. for (int i = 0; i < cnfield.Length; i++)
  430. {
  431. dt1.Columns.Add(cnfield[i]);
  432. dt1.Columns[i].Caption = dbfield[i];
  433. }
  434. //返回一个带有结构的空的DataTable
  435. //DbFind.BindTable1 = dbtable;
  436. return dt1;
  437. }
  438. else
  439. {
  440. return null;
  441. }
  442. }
  443. /// <summary>
  444. /// 检测内容是否存在
  445. /// </summary>
  446. /// <param name="TableName"></param>
  447. /// <param name="Condition"></param>
  448. /// <returns></returns>
  449. public bool CheckExist(string TableName, string Condition)
  450. {
  451. string sql = "select count(1) from " + TableName + " where " + Condition;
  452. command = new OracleCommand(sql, connection);
  453. Reconnect(command);
  454. OracleDataAdapter ad = new OracleDataAdapter(command);
  455. DataTable dt = new DataTable();
  456. try
  457. {
  458. ad.Fill(dt);
  459. }
  460. catch (Exception)
  461. {
  462. connection = new OracleConnection(DBConnectionString);
  463. connection.Open();
  464. command = new OracleCommand(sql, connection);
  465. ad = new OracleDataAdapter();
  466. ad.SelectCommand = command;
  467. ad.Fill(dt);
  468. }
  469. ad.Dispose();
  470. Console.WriteLine(dt.Rows[0][0].ToString());
  471. Console.WriteLine(int.Parse(dt.Rows[0][0].ToString()) > 0);
  472. return int.Parse(dt.Rows[0][0].ToString()) > 0;
  473. }
  474. /// <summary>
  475. /// 直接执行SQL,同时传入SQL的类型
  476. /// </summary>
  477. /// <param name="SQL"></param>
  478. /// <param name="Type"></param>
  479. /// <returns></returns>
  480. public object ExecuteSql(string SQL, string Type, params object[] names)
  481. {
  482. object result = null;
  483. command = new OracleCommand(SQL, connection);
  484. Reconnect(command);
  485. //用来拼接参数的
  486. if (names.Length > 0)
  487. {
  488. string[] par = SQL.Split(':');
  489. //用来存参数的数组
  490. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  491. for (int i = 0; i < par.Length - 1; i++)
  492. {
  493. //新建一个char类型的数组用来存储每个字节的变量
  494. char[] c = par[i + 1].ToCharArray();
  495. addpar[i] = new StringBuilder();
  496. for (int j = 0; j < c.Length; j++)
  497. {
  498. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  499. {
  500. addpar[i].Append(c[j]);
  501. }
  502. else
  503. {
  504. break;
  505. }
  506. }
  507. }
  508. for (int i = 0; i < addpar.Length; i++)
  509. command.Parameters.Add(new OracleParameter(addpar[i].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
  510. }
  511. Console.WriteLine(SQL);
  512. switch (Type.ToUpper())
  513. {
  514. case "SELECT":
  515. OracleDataAdapter ad = new OracleDataAdapter(command);
  516. result = new DataTable();
  517. try
  518. {
  519. ad.Fill((DataTable)result);
  520. }
  521. catch (Exception)
  522. {
  523. connection = new OracleConnection(DBConnectionString);
  524. connection.Open();
  525. command = new OracleCommand(SQL, connection);
  526. ad = new OracleDataAdapter();
  527. ad.SelectCommand = command;
  528. ad.Fill((DataTable)result);
  529. }
  530. break;
  531. case "DELETE":
  532. try
  533. {
  534. result = command.ExecuteNonQuery();
  535. }
  536. catch (Exception)
  537. {
  538. command.Connection = new OracleConnection(DBConnectionString);
  539. command.Connection.Open();
  540. result = command.ExecuteNonQuery();
  541. }
  542. break;
  543. case "UPDATE":
  544. try
  545. {
  546. result = command.ExecuteNonQuery();
  547. }
  548. catch (Exception)
  549. {
  550. command.Connection = new OracleConnection(DBConnectionString);
  551. command.Connection.Open();
  552. result = command.ExecuteNonQuery();
  553. }
  554. break;
  555. case "INSERT":
  556. try
  557. {
  558. result = command.ExecuteNonQuery();
  559. }
  560. catch (Exception)
  561. {
  562. command.Connection = new OracleConnection(DBConnectionString);
  563. command.Connection.Open();
  564. result = command.ExecuteNonQuery();
  565. }
  566. break;
  567. }
  568. return result;
  569. }
  570. /// <summary>
  571. /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
  572. /// </summary>
  573. /// <param name="SQL"></param>
  574. public void CheckSQL(string SQL)
  575. {
  576. SQL = SQL.Replace("?", ":Param");
  577. command = new OracleCommand(SQL, connection);
  578. command.ExecuteNonQuery();
  579. }
  580. public int GetDistinctRowCount(string TableName, string Field)
  581. {
  582. DataTable dt = new DataTable();
  583. string sql = "select distinct count('" + Field + "') from " + TableName;
  584. command = new OracleCommand(sql, connection);
  585. Reconnect(command);
  586. OracleDataAdapter ad = new OracleDataAdapter(command);
  587. try
  588. {
  589. ad.Fill(dt);
  590. }
  591. catch (Exception)
  592. {
  593. connection = new OracleConnection(DBConnectionString);
  594. connection.Open();
  595. command = new OracleCommand(sql, connection);
  596. ad = new OracleDataAdapter();
  597. ad.SelectCommand = command;
  598. ad.Fill(dt);
  599. }
  600. ad.Dispose();
  601. return int.Parse(dt.Rows[0][0].ToString());
  602. }
  603. /// <summary>
  604. /// 根据Caller获取流水号
  605. /// </summary>
  606. /// <param name="Caller"></param>
  607. /// <returns></returns>
  608. public string GetSerialNumberByCaller(string Caller)
  609. {
  610. string SerialNumber = getFieldDataByCondition("MaxNumbers", "mn_number", "mn_tablename='" + Caller + "'").ToString();
  611. UpdateByCondition("MaxNumbers", "mn_number=mn_number+1", "mn_tablename='" + Caller + "'");
  612. return SerialNumber;
  613. }
  614. /// <summary>
  615. /// 根据主键ID删除表的数据
  616. /// </summary>
  617. /// <param name="TableName">表名</param>
  618. /// <param name="ID">主键</param>
  619. /// <param name="DeleteID">需要删除主键ID的数组</param>
  620. public void DeleteDataByID(string TableName, string ID, string[] DeleteID)
  621. {
  622. string sql = "delete from " + TableName + " where " + ID + " =:DeleteID";
  623. command = new OracleCommand(sql, connection);
  624. Reconnect(command);
  625. command.ArrayBindCount = DeleteID.Length;
  626. command.Parameters.Add(new OracleParameter("DeleteID", OracleDbType.Long, DeleteID, ParameterDirection.Input));
  627. try
  628. {
  629. command.ExecuteNonQuery();
  630. }
  631. catch (Exception)
  632. {
  633. command.Connection = new OracleConnection(DBConnectionString);
  634. command.Connection.Open();
  635. command.ExecuteNonQuery();
  636. }
  637. }
  638. /// <summary>
  639. /// 通过序列的名称获取序列
  640. /// </summary>
  641. /// <param name="SeqName"></param>
  642. /// <returns></returns>
  643. public string GetSEQ(string SeqName)
  644. {
  645. DataTable dt = new DataTable();
  646. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
  647. return dt.Rows[0][0].ToString();
  648. }
  649. /// <summary>
  650. /// 通过序列的名称获取序列
  651. /// </summary>
  652. /// <param name="SeqName"></param>
  653. /// <returns></returns>
  654. public string[] GetSEQ(string SeqName, int Num)
  655. {
  656. DataTable dt = new DataTable();
  657. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".nextval FROM DUAL CONNECT BY LEVEL<=" + Num, "select");
  658. string[] SerialNum = new string[dt.Rows.Count];
  659. for (int i = 0; i < dt.Rows.Count; i++)
  660. {
  661. SerialNum[i] = dt.Rows[i][0].ToString();
  662. }
  663. return SerialNum;
  664. }
  665. public void SaveDataTable(DataTable dt, string TableName, string ID, params string[] sql)
  666. {
  667. if (dt == null)
  668. {
  669. return;
  670. }
  671. StringBuilder sb = new StringBuilder();
  672. //预防插入的DataTable中存在不属于该表的列,在进行下一步操作之前全部剔除
  673. DataTable data = (DataTable)ExecuteSql("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", "select");
  674. //将所有的字段拼接起来
  675. for (int i = 0; i < data.Rows.Count; i++)
  676. {
  677. sb.Append("#" + data.Rows[i]["Column_Name"].ToString());
  678. }
  679. //移除掉所有不属于该表的列
  680. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  681. {
  682. if (!sb.ToString().Contains(dt.Columns[i].ColumnName.ToUpper()))
  683. {
  684. dt.Columns.RemoveAt(i);
  685. }
  686. }
  687. sb.Clear();
  688. //计算有多少个是新加的行,根据主键为空来进行判断
  689. int NewRowCount = 0;
  690. for (int i = 0; i < dt.Rows.Count; i++)
  691. {
  692. if (dt.Rows[i][ID] == null || dt.Rows[i][ID].ToString() == "")
  693. {
  694. NewRowCount = NewRowCount + 1;
  695. }
  696. }
  697. if (sql.Length > 0)
  698. {
  699. if (NewRowCount > 0)
  700. {
  701. //获取参数的个数
  702. int paramsNum = sql[0].Split(':').Length - 1;
  703. //解析参数的数据
  704. string[] param = GetParamFromSQL(sql[0]);
  705. //新建一个二维数组去
  706. string[][] param_array = new string[paramsNum][];
  707. //实例化每个一维数组
  708. for (int i = 0; i < paramsNum; i++)
  709. {
  710. param_array[i] = new string[NewRowCount];
  711. }
  712. //设置每列参数的索引
  713. int num = 0;
  714. //变量所有的行,如果有主键为空的则移除,不为空的进行参数的拼接
  715. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  716. {
  717. if (dt.Rows[i][ID] == null || dt.Rows[i][ID].ToString() == "")
  718. {
  719. //当为新添加行的时候才去设置参数,设置过后索引+1
  720. for (int j = 0; j < paramsNum; j++)
  721. {
  722. param_array[j][num] = dt.Rows[i][param[j]].ToString();
  723. }
  724. dt.Rows.RemoveAt(i);
  725. num++;
  726. }
  727. }
  728. BatchInsertDataTable(sql[0], param, param_array);
  729. }
  730. }
  731. sb.Clear();
  732. sb.Append("update " + TableName + " set ");
  733. int ColumnCount = dt.Columns.Count;
  734. int RowCount = dt.Rows.Count;
  735. //存数据的参数
  736. List<string[]> Parameter = new List<string[]>();
  737. //存参数名的参数
  738. string[] ParName = new string[ColumnCount];
  739. for (int i = 0; i < ColumnCount; i++)
  740. {
  741. ParName[i] = dt.Columns[i].ColumnName;
  742. if (i == dt.Columns.Count - 1)
  743. sb.Append(dt.Columns[i].ColumnName + "=:" + dt.Columns[i].ColumnName);
  744. else
  745. sb.Append(dt.Columns[i].ColumnName + "=:" + dt.Columns[i].ColumnName + ",");
  746. }
  747. sb.Append(" where " + ID + " =:" + ID);
  748. //先添加参数
  749. Parameter.Add(ParName);
  750. //添加参数的具体内容
  751. for (int i = 0; i < ColumnCount; i++)
  752. {
  753. string[] par = new string[RowCount];
  754. for (int j = 0; j < RowCount; j++)
  755. {
  756. par[j] = dt.Rows[j][i].ToString();
  757. }
  758. Parameter.Add(par);
  759. }
  760. BatchInsert(sb.ToString(), Parameter.ToArray());
  761. }
  762. /// <summary>
  763. /// 批量通过SQL来执行插入操作 ,参数的第一个数一个string[]数组,用来传递需要添加的参数的名称
  764. /// 之后的是名称参数数组对应的 ,所有的插入参数数据长度必须是一致的
  765. /// </summary>
  766. /// <param name="sql"></param>
  767. /// <param name="names"></param>
  768. public void BatchInsert(string sql, params object[][] names)
  769. {
  770. command = new OracleCommand(sql, connection);
  771. Reconnect(command);
  772. command.ArrayBindCount = names[1].Length;
  773. //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
  774. //将第一个数组的下标固定为0作为循环添加的参数的名称
  775. for (int i = 1; i <= names[0].Length; i++)
  776. {
  777. command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
  778. }
  779. try
  780. {
  781. command.ExecuteNonQuery();
  782. }
  783. catch (Exception)
  784. {
  785. command.Connection = new OracleConnection(DBConnectionString);
  786. command.Connection.Open();
  787. command.ExecuteNonQuery();
  788. }
  789. }
  790. public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
  791. {
  792. command = new OracleCommand(sql, connection);
  793. Reconnect(command);
  794. command.ArrayBindCount = param1[0].Length;
  795. //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
  796. //将第一个数组的下标固定为0作为循环添加的参数的名称
  797. for (int i = 0; i < param.Length; i++)
  798. {
  799. command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, param1[i], ParameterDirection.Input));
  800. }
  801. try
  802. {
  803. command.ExecuteNonQuery();
  804. }
  805. catch (Exception)
  806. {
  807. command.Connection = new OracleConnection(DBConnectionString);
  808. command.Connection.Open();
  809. command.ExecuteNonQuery();
  810. }
  811. }
  812. /// <summary>
  813. /// 取Configs表中的配置,进行该客户是否执行某个操作
  814. /// </summary>
  815. /// <param name="Code"></param>
  816. /// <param name="Caller"></param>
  817. /// <returns></returns>
  818. public object GetConfig(string Code, string Caller)
  819. {
  820. DataTable dt = new DataTable();
  821. string sql = "select Data from configs where code='" + Code + "' and caller='" + Caller + "'";
  822. dt = (DataTable)ExecuteSql(sql, "select");
  823. if (dt.Rows.Count == 0)
  824. {
  825. return "";
  826. }
  827. else
  828. {
  829. return dt.Rows[0]["Data"];
  830. }
  831. }
  832. //将数据类型的列类型转换为DataTable
  833. public DataTable DataTypeColumnToDataTable(DataTable dt)
  834. {
  835. DataTable dt1 = new DataTable();
  836. dt1.Rows.Add();
  837. foreach (DataRow dr in dt.Rows)
  838. {
  839. dt1.Columns.Add(dr[0].ToString());
  840. int index = dt.Rows.IndexOf(dr);
  841. if (dr[1].ToString() == "NUMBER")
  842. {
  843. dt1.Rows[0][index] = 0;
  844. }
  845. if (dr[1].ToString() == "VARCHAR2")
  846. {
  847. dt1.Rows[0][index] = "这是一段文字";
  848. }
  849. if (dr[1].ToString() == "DATE")
  850. {
  851. dt1.Rows[0][index] = DateTime.Now.ToString("yyyy-MM-dd");
  852. }
  853. if (dr[1].ToString() == "FLOAT")
  854. {
  855. dt1.Rows[0][index] = 1.0;
  856. }
  857. if (dr[1].ToString() == "CLOB")
  858. {
  859. dt1.Rows[0][index] = "一段长文字";
  860. }
  861. }
  862. return dt1;
  863. }
  864. /// <summary>
  865. /// 通过条件更新
  866. /// </summary>
  867. /// <param name="TableName"></param>
  868. /// <param name="update"></param>
  869. /// <param name="condition"></param>
  870. public string UpdateByCondition(string TableName, string update, string condition)
  871. {
  872. string sql = "update " + TableName + " set " + update + " where " + condition;
  873. command = new OracleCommand(sql, connection);
  874. Console.WriteLine(sql);
  875. Reconnect(command);
  876. try
  877. {
  878. command.ExecuteNonQuery();
  879. }
  880. catch (Exception)
  881. {
  882. command.Connection = new OracleConnection(DBConnectionString);
  883. command.Connection.Open();
  884. command.ExecuteNonQuery();
  885. }
  886. return sql;
  887. }
  888. /// <summary>
  889. /// 调用存储过程
  890. /// </summary>
  891. /// <param name="ProcedureName"></param>
  892. /// <param name="param"></param>
  893. public void CallProcedure(string ProcedureName, ref string[] param)
  894. {
  895. command = new OracleCommand(ProcedureName);
  896. command.Connection = connection;
  897. Reconnect(command);
  898. command.CommandText = ProcedureName;
  899. command.CommandType = CommandType.StoredProcedure;
  900. for (int i = 0; i < param.Length; i++)
  901. command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, 4000, param[i], ParameterDirection.InputOutput));
  902. try
  903. {
  904. command.ExecuteNonQuery();
  905. }
  906. catch (Exception)
  907. {
  908. command.Connection = new OracleConnection(DBConnectionString);
  909. command.Connection.Open();
  910. command.ExecuteNonQuery();
  911. }
  912. for (int i = 0; i < command.Parameters.Count; i++)
  913. param[i] = command.Parameters[i].Value.ToString();
  914. }
  915. public void CallProcedure(string ProcedureName, string[] ParamName, ref string[] param)
  916. {
  917. command = new OracleCommand(ProcedureName);
  918. command.Connection = connection;
  919. Reconnect(command);
  920. command.CommandText = ProcedureName;
  921. command.CommandType = CommandType.StoredProcedure;
  922. for (int i = 0; i < param.Length; i++)
  923. {
  924. command.Parameters.Add(new OracleParameter(ParamName[i], OracleDbType.Varchar2, 200, param[i], ParameterDirection.InputOutput));
  925. //command.Parameters.Add(new OracleParameter(ParamName[i], OracleType.VarChar, 200, ParameterDirection.InputOutput, "", DataRowVersion.Default, true, param[i]));
  926. }
  927. try
  928. {
  929. command.ExecuteNonQuery();
  930. }
  931. catch (Exception)
  932. {
  933. command.Connection = new OracleConnection(ConnectionStrings);
  934. command.Connection.Open();
  935. command.ExecuteNonQuery();
  936. }
  937. for (int i = 0; i < command.Parameters.Count; i++)
  938. param[i] = command.Parameters[i].Value.ToString();
  939. }
  940. /// <summary>
  941. /// 出现异常进行回滚的执行方法
  942. /// </summary>
  943. /// <param name="SQL"></param>
  944. public void ExecuteSQLTran(params string[] SQL)
  945. {
  946. OracleTransaction tx = connection.BeginTransaction();
  947. command = new OracleCommand();
  948. command.Connection = connection;
  949. command.Transaction = tx;
  950. try
  951. {
  952. foreach (string sql in SQL)
  953. {
  954. if (!String.IsNullOrEmpty(sql))
  955. {
  956. Console.WriteLine(sql);
  957. command.CommandText = sql;
  958. try
  959. {
  960. command.ExecuteNonQuery();
  961. }
  962. catch (Exception)
  963. {
  964. command.Connection = new OracleConnection(DBConnectionString);
  965. command.Connection.Open();
  966. command.ExecuteNonQuery();
  967. }
  968. }
  969. }
  970. tx.Commit();
  971. }
  972. catch (Exception E)
  973. {
  974. tx.Rollback();
  975. throw new Exception(E.Message);
  976. }
  977. }
  978. /// <summary>
  979. /// 用于将string 的数组转换成SQL的查询内容
  980. /// </summary>
  981. /// <param name="Fields"></param>
  982. /// <returns></returns>
  983. private string AddField(string[] Fields)
  984. {
  985. string sql = " ";
  986. foreach (string field in Fields)
  987. {
  988. sql += field + ",";
  989. }
  990. return sql.Substring(0, sql.Length - 1);
  991. }
  992. /// <summary>
  993. /// 通过查询的内容获取到字段的描述
  994. /// </summary>
  995. /// <param name="field"></param>
  996. /// <returns></returns>
  997. private static string[] GetCaptionFromField(string field)
  998. {
  999. string[] caption = field.Split(',');
  1000. for (int i = 0; i < caption.Length; i++)
  1001. {
  1002. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  1003. }
  1004. return caption;
  1005. }
  1006. /// <summary>
  1007. /// 通过查询的语句获取查询的字段
  1008. /// </summary>
  1009. /// <param name="field"></param>
  1010. /// <returns></returns>
  1011. private static string[] GetField(string field)
  1012. {
  1013. string[] fields = field.Split(',');
  1014. for (int i = 0; i < fields.Length; i++)
  1015. {
  1016. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  1017. }
  1018. return fields;
  1019. }
  1020. public object GetLabelParam(string sql)
  1021. {
  1022. DataTable dt = new DataTable();
  1023. command = new OracleCommand(sql, connection);
  1024. Reconnect(command);
  1025. OracleDataAdapter ad = new OracleDataAdapter();
  1026. ad.SelectCommand = command;
  1027. try
  1028. {
  1029. ad.Fill(dt);
  1030. }
  1031. catch (Exception)
  1032. {
  1033. connection = new OracleConnection(DBConnectionString);
  1034. connection.Open();
  1035. command = new OracleCommand(sql, connection);
  1036. ad = new OracleDataAdapter();
  1037. ad.SelectCommand = command;
  1038. ad.Fill(dt);
  1039. }
  1040. if (dt.Rows.Count > 0)
  1041. {
  1042. ad.Dispose();
  1043. return dt.Rows[0][0];
  1044. }
  1045. else
  1046. {
  1047. return "";
  1048. }
  1049. }
  1050. public static string[] GetParamFromSQL(string SQL)
  1051. {
  1052. string[] par = SQL.Split(':');
  1053. //用来存参数的数组
  1054. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  1055. string[] param = new string[par.Length - 1];
  1056. for (int i = 0; i < par.Length - 1; i++)
  1057. {
  1058. //新建一个char类型的数组用来存储每个字节的变量
  1059. char[] c = par[i + 1].ToCharArray();
  1060. addpar[i] = new StringBuilder();
  1061. for (int j = 0; j < c.Length; j++)
  1062. {
  1063. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  1064. {
  1065. addpar[i].Append(c[j]);
  1066. }
  1067. else
  1068. {
  1069. break;
  1070. }
  1071. }
  1072. }
  1073. for (int i = 0; i < par.Length - 1; i++)
  1074. {
  1075. param[i] = addpar[i].ToString();
  1076. }
  1077. return param;
  1078. }
  1079. public void Dispose()
  1080. {
  1081. }
  1082. private void Reconnect(OracleCommand cmd)
  1083. {
  1084. if (cmd.Connection.State == ConnectionState.Closed)
  1085. {
  1086. cmd.Connection.Open();
  1087. LogManager.DoLog("超时重连");
  1088. }
  1089. }
  1090. }
  1091. }