DataHelper.cs 41 KB

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