DataHelper.cs 42 KB

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