DataHelper.cs 43 KB

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