DataHelper.cs 53 KB

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