DataHelper.cs 40 KB

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