DataHelper.cs 52 KB

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