DataHelper.cs 47 KB

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