DataHelper.cs 47 KB

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