DataHelper.cs 41 KB

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