DataHelper.cs 52 KB

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