DataHelper.cs 42 KB

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