AccessDBHelper.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Forms;
  9. namespace UAS_MES_NEW.DataOperate
  10. {
  11. class AccessDBHelper
  12. {
  13. private string _fileName;
  14. private string _connectionString;
  15. private OleDbConnection _odcConnection = null;
  16. private OleDbCommand command = null;
  17. public AccessDBHelper(string fileName)
  18. {
  19. this._fileName = fileName;
  20. //this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
  21. this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";
  22. }
  23. public AccessDBHelper(string fileName,string password)
  24. {
  25. this._fileName = fileName;
  26. //this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database Password="+password+";";
  27. this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Jet OLEDB:Database Password=" + password + ";";
  28. }
  29. public void Open()
  30. {
  31. try
  32. {
  33. // 建立连接
  34. this._odcConnection = new OleDbConnection(this._connectionString);
  35. // 打开连接
  36. this._odcConnection.Open();
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageBox.Show(ex.Message);
  41. }
  42. }
  43. public void Close()
  44. {
  45. this._odcConnection.Close();
  46. }
  47. public DataTable GetDataTable(string sql)
  48. {
  49. DataTable ds = new DataTable();
  50. command = new OleDbCommand(sql, this._odcConnection);
  51. Reconnect(command);
  52. OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  53. try
  54. {
  55. adapter.Fill(ds);
  56. }
  57. catch (Exception ex)
  58. {
  59. throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
  60. }
  61. adapter.Dispose();
  62. return ds;
  63. }
  64. public DataSet GetDataSet(string sql)
  65. {
  66. DataSet ds = new DataSet();
  67. command = new OleDbCommand(sql, this._odcConnection);
  68. Reconnect(command);
  69. OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  70. try
  71. {
  72. adapter.Fill(ds);
  73. }
  74. catch (Exception ex)
  75. {
  76. throw new Exception("sql語句: " + sql + " 執行失敗!"+ex.Message);
  77. }
  78. adapter.Dispose();
  79. return ds;
  80. }
  81. public void Reconnect(OleDbCommand cmd)
  82. {
  83. if (cmd.Connection.State == ConnectionState.Closed)
  84. {
  85. cmd.Connection.Open();
  86. //超时重连
  87. }
  88. }
  89. /// <summary>
  90. /// 根据表名获取该表字段数据类型
  91. /// </summary>
  92. public DataTable GetColumnDataType(string TableName)
  93. {
  94. DataTable dt = new DataTable();
  95. command = new OleDbCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", this._odcConnection);
  96. Reconnect(command);
  97. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  98. ad.Fill(dt);
  99. ad.Dispose();
  100. command.Dispose();
  101. return dt;
  102. }
  103. /// <summary>
  104. /// 获取第一行第一列的信息
  105. /// </summary>
  106. public object getFieldDataByCondition(string TableName, string Field, string Condition)
  107. {
  108. DataTable dt = new DataTable();
  109. string sql = "select " + Field + " from " + TableName + " where " + Condition;
  110. command = new OleDbCommand(sql, this._odcConnection);
  111. Reconnect(command);
  112. OleDbDataAdapter ad = new OleDbDataAdapter();
  113. ad.SelectCommand = command;
  114. try
  115. {
  116. ad.Fill(dt);
  117. }
  118. catch (Exception)
  119. {
  120. this._odcConnection = new OleDbConnection(this._connectionString);
  121. this._odcConnection.Open();
  122. command = new OleDbCommand(sql, this._odcConnection);
  123. ad = new OleDbDataAdapter();
  124. ad.SelectCommand = command;
  125. ad.Fill(dt);
  126. }
  127. ad.Dispose();
  128. command.Dispose();
  129. if (dt.Rows.Count > 0)
  130. {
  131. return dt.Rows[0][0];
  132. }
  133. else
  134. {
  135. return "";
  136. }
  137. }
  138. /// <summary>
  139. /// 获取指定表的记录的条数 ,带条件
  140. /// </summary>
  141. /// <returns></returns>
  142. public int getRowCount(string TableName, string Condition)
  143. {
  144. DataTable dt = new DataTable();
  145. string sql = "select count(1) from " + TableName + " where " + Condition;
  146. command = new OleDbCommand(sql, this._odcConnection);
  147. Reconnect(command);
  148. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  149. ad.Fill(dt);
  150. ad.Dispose();
  151. command.Dispose();
  152. return int.Parse(dt.Rows[0][0].ToString());
  153. }
  154. /// <summary>
  155. /// 获取指定表的记录的条数 ,不带条件
  156. /// </summary>
  157. /// <param name="TableName"></param>
  158. /// <returns></returns>
  159. public int getRowCount(string TableName)
  160. {
  161. DataTable dt = new DataTable();
  162. string sql = "select count(1) from " + TableName;
  163. command = new OleDbCommand(sql, this._odcConnection);
  164. Reconnect(command);
  165. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  166. ad.Fill(dt);
  167. ad.Dispose();
  168. command.Dispose();
  169. return int.Parse(dt.Rows[0][0].ToString());
  170. }
  171. /// <summary>
  172. /// 通过表名和获取单行的记录
  173. /// </summary>
  174. public DataTable getFieldsDataByCondition(string TableName, string[] Fields, string Condition)
  175. {
  176. DataTable dt = new DataTable();
  177. string sql = "select ";
  178. sql += AddField(Fields);
  179. sql += " from " + TableName + " where " + Condition + " and rownum=1";
  180. command = new OleDbCommand(sql, this._odcConnection);
  181. Reconnect(command);
  182. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  183. try
  184. {
  185. ad.Fill(dt);
  186. }
  187. catch (Exception)
  188. {
  189. this._odcConnection = new OleDbConnection(this._connectionString);
  190. this._odcConnection.Open();
  191. command = new OleDbCommand(sql, this._odcConnection);
  192. ad = new OleDbDataAdapter();
  193. ad.SelectCommand = command;
  194. ad.Fill(dt);
  195. }
  196. ad.Dispose();
  197. command.Dispose();
  198. return dt;
  199. }
  200. /// <summary>
  201. /// 将DataTable导入到指定的表中
  202. /// </summary>
  203. /// <param name="DataTable"></param>
  204. /// <param name="TableName"></param>
  205. public void InsertDataTable(DataTable DataTable, string TableName)
  206. {
  207. for (int i = 0; i < DataTable.Rows.Count; i++)
  208. {
  209. for (int j = 0; j < DataTable.Columns.Count; j++)
  210. {
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 按分页获取数据
  216. /// </summary>
  217. /// <param name="TableName">表名</param>
  218. /// <param name="Fields">查询字段</param>
  219. /// <param name="CurrentPage">当前页面</param>
  220. /// <param name="PageSize">页面展示条数</param>
  221. /// <param name="Caller"></param>
  222. /// <returns></returns>
  223. // SELECT * FROM (SELECT A.* FROM (SELECT* FROM datalist) A WHERE ROWNUM <= 50) WHERE ROWNUM >= 21
  224. public DataTable getFieldsDatasByPageing(string TableName, string Fields, int CurrentPage, int PageSize, string Caller, params string[] condition)
  225. {
  226. DataTable dt = new DataTable();
  227. StringBuilder sql = new StringBuilder();
  228. //先查询出配置出去的列
  229. //获取查询的列
  230. string[] caption = GetCaptionFromField(Fields);
  231. //获取对应列的描述
  232. string[] field = GetField(Fields);
  233. sql.Append(" select * from (select RowNum RN, A.* from (select ");
  234. sql.Append(AddField(caption));
  235. if (condition.Length > 0)
  236. {
  237. if (condition[0] != null && condition[0].Trim() != "")
  238. sql.Append(" from " + TableName + " where " + condition[0] + " ) A where ROWNUM <=" + CurrentPage * PageSize + ") where RN>" + (CurrentPage - 1) * PageSize);
  239. else
  240. sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
  241. }
  242. command = new OleDbCommand(sql.ToString(), this._odcConnection);
  243. Reconnect(command);
  244. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  245. ad.Fill(dt);
  246. ad.Dispose();
  247. command.Dispose();
  248. dt.Columns.RemoveAt(0);
  249. foreach (DataColumn dc in dt.Columns)
  250. {
  251. dc.ColumnName = field[dt.Columns.IndexOf(dc)];
  252. dc.Caption = caption[dt.Columns.IndexOf(dc)];
  253. }
  254. return dt;
  255. }
  256. /// <summary>
  257. /// 通过表名,字段和条件获取DataTable类型的数据
  258. /// </summary>
  259. public DataTable getFieldsDatasByCondition(string TableName, string[] Fields, string Condition)
  260. {
  261. DataTable dt = new DataTable();
  262. string sql = "select ";
  263. sql += AddField(Fields);
  264. sql += " from " + TableName + " where " + Condition;
  265. command = new OleDbCommand(sql, this._odcConnection);
  266. Reconnect(command);
  267. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  268. try
  269. {
  270. ad.Fill(dt);
  271. }
  272. catch (Exception)
  273. {
  274. this._odcConnection = new OleDbConnection(this._connectionString);
  275. this._odcConnection.Open();
  276. command = new OleDbCommand(sql, this._odcConnection);
  277. ad = new OleDbDataAdapter();
  278. ad.SelectCommand = command;
  279. ad.Fill(dt);
  280. }
  281. ad.Dispose();
  282. command.Dispose();
  283. return dt;
  284. }
  285. /// <summary>
  286. /// 通过表名,字段获取DataTable类型的数据
  287. /// </summary>
  288. public DataTable getFieldsDatas(string TableName, string Fields)
  289. {
  290. DataTable dt = new DataTable();
  291. string sql = "select ";
  292. sql += Fields;
  293. sql += " from " + TableName;
  294. command = new OleDbCommand(sql, this._odcConnection);
  295. Reconnect(command);
  296. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  297. ad.SelectCommand = command;
  298. try
  299. {
  300. ad.Fill(dt);
  301. }
  302. catch (Exception)
  303. {
  304. this._odcConnection = new OleDbConnection(this._connectionString);
  305. this._odcConnection.Open();
  306. command = new OleDbCommand(sql, this._odcConnection);
  307. ad = new OleDbDataAdapter();
  308. ad.SelectCommand = command;
  309. ad.Fill(dt);
  310. }
  311. ad.Dispose();
  312. command.Dispose();
  313. return dt;
  314. }
  315. /// <summary>
  316. /// 获取DbFind的数据的DataTable的结构
  317. /// </summary>
  318. /// <param name="field"></param>
  319. /// <param name="caller"></param>
  320. /// <returns></returns>
  321. public DataTable GetDbFindDataTable(string field, string caller)
  322. {
  323. string sql = "select * from dbfindsetui where ds_caller='" + caller + "' and ds_whichui='" + field + "'";
  324. DataTable dt = (DataTable)ExecuteSql(sql, "select");
  325. if (dt.Rows.Count != 0)
  326. {
  327. //通过#号分割字段
  328. string[] dbfield = dt.Rows[0]["ds_findtoui"].ToString().Split('#');
  329. string[] cnfield = dt.Rows[0]["ds_dbcaption"].ToString().Split('#');
  330. //获取查询要查询的Table
  331. string dbtable = dt.Rows[0]["ds_tables"].ToString();
  332. //拼接查询的字段
  333. for (int i = 0; i < dbfield.Length; i++)
  334. {
  335. dbfield[i] = dbfield[i].Split(',')[0];
  336. }
  337. //新建一个空的DataTable
  338. DataTable dt1 = new DataTable();
  339. //往空的DataTable添加结构,ColumnName是中文,Caption是实际的字段名称
  340. for (int i = 0; i < cnfield.Length; i++)
  341. {
  342. dt1.Columns.Add(cnfield[i]);
  343. dt1.Columns[i].Caption = dbfield[i];
  344. }
  345. //返回一个带有结构的空的DataTable
  346. //DbFind.BindTable1 = dbtable;
  347. return dt1;
  348. }
  349. else
  350. {
  351. return null;
  352. }
  353. }
  354. /// <summary>
  355. /// 检测内容是否存在
  356. /// </summary>
  357. /// <param name="TableName"></param>
  358. /// <param name="Condition"></param>
  359. /// <returns></returns>
  360. public bool CheckExist(string TableName, string Condition)
  361. {
  362. string sql = "select count(1) from " + TableName + " where " + Condition;
  363. command = new OleDbCommand(sql, this._odcConnection);
  364. Reconnect(command);
  365. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  366. DataTable dt = new DataTable();
  367. try
  368. {
  369. ad.Fill(dt);
  370. }
  371. catch (Exception)
  372. {
  373. this._odcConnection = new OleDbConnection(this._connectionString);
  374. this._odcConnection.Open();
  375. command = new OleDbCommand(sql, this._odcConnection);
  376. ad = new OleDbDataAdapter();
  377. ad.SelectCommand = command;
  378. ad.Fill(dt);
  379. }
  380. ad.Dispose();
  381. command.Dispose();
  382. return int.Parse(dt.Rows[0][0].ToString()) > 0;
  383. }
  384. /// <summary>
  385. /// 直接执行SQL,同时传入SQL的类型
  386. /// </summary>
  387. /// <param name="SQL"></param>
  388. /// <param name="Type"></param>
  389. /// <returns></returns>
  390. public object ExecuteSql(string SQL, string Type, params object[] names)
  391. {
  392. object result = null;
  393. command = new OleDbCommand(SQL, this._odcConnection);
  394. Reconnect(command);
  395. //用来拼接参数的
  396. if (names.Length > 0)
  397. {
  398. string[] par = SQL.Split(':');
  399. //用来存参数的数组
  400. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  401. for (int i = 0; i < par.Length - 1; i++)
  402. {
  403. //新建一个char类型的数组用来存储每个字节的变量
  404. char[] c = par[i + 1].ToCharArray();
  405. addpar[i] = new StringBuilder();
  406. for (int j = 0; j < c.Length; j++)
  407. {
  408. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  409. {
  410. addpar[i].Append(c[j]);
  411. }
  412. else
  413. {
  414. break;
  415. }
  416. }
  417. }
  418. for (int i = 0; i < addpar.Length; i++)
  419. command.Parameters.Add(new OleDbParameter(addpar[i].ToString(),names[i]));
  420. }
  421. switch (Type.ToUpper())
  422. {
  423. case "SELECT":
  424. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  425. result = new DataTable();
  426. try
  427. {
  428. ad.Fill((DataTable)result);
  429. }
  430. catch (Exception)
  431. {
  432. this._odcConnection = new OleDbConnection(this._connectionString);
  433. this._odcConnection.Open();
  434. command = new OleDbCommand(SQL, this._odcConnection);
  435. ad = new OleDbDataAdapter();
  436. ad.SelectCommand = command;
  437. ad.Fill((DataTable)result);
  438. }
  439. break;
  440. case "DELETE":
  441. try
  442. {
  443. result = command.ExecuteNonQuery();
  444. }
  445. catch (Exception)
  446. {
  447. command.Connection = new OleDbConnection(this._connectionString);
  448. command.Connection.Open();
  449. result = command.ExecuteNonQuery();
  450. }
  451. break;
  452. case "UPDATE":
  453. try
  454. {
  455. result = command.ExecuteNonQuery();
  456. }
  457. catch (Exception)
  458. {
  459. command.Connection = new OleDbConnection(this._connectionString);
  460. command.Connection.Open();
  461. result = command.ExecuteNonQuery();
  462. }
  463. break;
  464. case "INSERT":
  465. try
  466. {
  467. result = command.ExecuteNonQuery();
  468. }
  469. catch (Exception)
  470. {
  471. command.Connection = new OleDbConnection(this._connectionString);
  472. command.Connection.Open();
  473. result = command.ExecuteNonQuery();
  474. }
  475. break;
  476. }
  477. command.Dispose();
  478. return result;
  479. }
  480. /// <summary>
  481. /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
  482. /// </summary>
  483. /// <param name="SQL"></param>
  484. public void CheckSQL(string SQL)
  485. {
  486. SQL = SQL.Replace("?", ":Param");
  487. command = new OleDbCommand(SQL, this._odcConnection);
  488. command.ExecuteNonQuery();
  489. command.Dispose();
  490. }
  491. public int GetDistinctRowCount(string TableName, string Field)
  492. {
  493. DataTable dt = new DataTable();
  494. string sql = "select distinct count('" + Field + "') from " + TableName;
  495. command = new OleDbCommand(sql, this._odcConnection);
  496. Reconnect(command);
  497. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  498. try
  499. {
  500. ad.Fill(dt);
  501. }
  502. catch (Exception)
  503. {
  504. this._odcConnection = new OleDbConnection(this._connectionString);
  505. this._odcConnection.Open();
  506. command = new OleDbCommand(sql, this._odcConnection);
  507. ad = new OleDbDataAdapter();
  508. ad.SelectCommand = command;
  509. ad.Fill(dt);
  510. }
  511. ad.Dispose();
  512. command.Dispose();
  513. return int.Parse(dt.Rows[0][0].ToString());
  514. }
  515. /// <summary>
  516. /// 根据Caller获取流水号
  517. /// </summary>
  518. /// <param name="Caller"></param>
  519. /// <returns></returns>
  520. public string GetSerialNumberByCaller(string Caller)
  521. {
  522. string SerialNumber = getFieldDataByCondition("MaxNumbers", "mn_number", "mn_tablename='" + Caller + "'").ToString();
  523. UpdateByCondition("MaxNumbers", "mn_number=mn_number+1", "mn_tablename='" + Caller + "'");
  524. return SerialNumber;
  525. }
  526. /// <summary>
  527. /// 通过序列的名称获取序列
  528. /// </summary>
  529. /// <param name="SeqName"></param>
  530. /// <returns></returns>
  531. public string GetSEQ(string SeqName)
  532. {
  533. DataTable dt = new DataTable();
  534. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
  535. return dt.Rows[0][0].ToString();
  536. }
  537. /// <summary>
  538. /// 通过序列的名称获取序列
  539. /// </summary>
  540. /// <param name="SeqName"></param>
  541. /// <returns></returns>
  542. public string[] GetSEQ(string SeqName, int Num)
  543. {
  544. DataTable dt = new DataTable();
  545. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".nextval FROM DUAL CONNECT BY LEVEL<=" + Num, "select");
  546. string[] SerialNum = new string[dt.Rows.Count];
  547. for (int i = 0; i < dt.Rows.Count; i++)
  548. {
  549. SerialNum[i] = dt.Rows[i][0].ToString();
  550. }
  551. return SerialNum;
  552. }
  553. public void SaveDataTable(DataTable dt, string TableName, string ID, params string[] sql)
  554. {
  555. if (dt == null)
  556. {
  557. return;
  558. }
  559. StringBuilder sb = new StringBuilder();
  560. //预防插入的DataTable中存在不属于该表的列,在进行下一步操作之前全部剔除
  561. DataTable data = (DataTable)ExecuteSql("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", "select");
  562. //将所有的字段拼接起来
  563. for (int i = 0; i < data.Rows.Count; i++)
  564. {
  565. sb.Append("#" + data.Rows[i]["Column_Name"].ToString());
  566. }
  567. //移除掉所有不属于该表的列
  568. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  569. {
  570. if (!sb.ToString().Contains(dt.Columns[i].ColumnName.ToUpper()))
  571. {
  572. dt.Columns.RemoveAt(i);
  573. }
  574. }
  575. sb.Clear();
  576. //计算有多少个是新加的行,根据主键为空来进行判断
  577. int NewRowCount = 0;
  578. for (int i = 0; i < dt.Rows.Count; i++)
  579. {
  580. if (dt.Rows[i][ID] == null || dt.Rows[i][ID].ToString() == "")
  581. {
  582. NewRowCount = NewRowCount + 1;
  583. }
  584. }
  585. if (sql.Length > 0)
  586. {
  587. if (NewRowCount > 0)
  588. {
  589. //获取参数的个数
  590. int paramsNum = sql[0].Split(':').Length - 1;
  591. //解析参数的数据
  592. string[] param = GetParamFromSQL(sql[0]);
  593. //新建一个二维数组去
  594. string[][] param_array = new string[paramsNum][];
  595. //实例化每个一维数组
  596. for (int i = 0; i < paramsNum; i++)
  597. {
  598. param_array[i] = new string[NewRowCount];
  599. }
  600. //设置每列参数的索引
  601. int num = 0;
  602. //变量所有的行,如果有主键为空的则移除,不为空的进行参数的拼接
  603. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  604. {
  605. if (dt.Rows[i][ID] == null || dt.Rows[i][ID].ToString() == "")
  606. {
  607. //当为新添加行的时候才去设置参数,设置过后索引+1
  608. for (int j = 0; j < paramsNum; j++)
  609. {
  610. param_array[j][num] = dt.Rows[i][param[j]].ToString();
  611. }
  612. dt.Rows.RemoveAt(i);
  613. num++;
  614. }
  615. }
  616. BatchInsertDataTable(sql[0], param, param_array);
  617. }
  618. }
  619. sb.Clear();
  620. sb.Append("update " + TableName + " set ");
  621. int ColumnCount = dt.Columns.Count;
  622. int RowCount = dt.Rows.Count;
  623. //存数据的参数
  624. List<string[]> Parameter = new List<string[]>();
  625. //存参数名的参数
  626. string[] ParName = new string[ColumnCount];
  627. for (int i = 0; i < ColumnCount; i++)
  628. {
  629. ParName[i] = dt.Columns[i].ColumnName;
  630. if (i == dt.Columns.Count - 1)
  631. sb.Append(dt.Columns[i].ColumnName + "=:" + dt.Columns[i].ColumnName);
  632. else
  633. sb.Append(dt.Columns[i].ColumnName + "=:" + dt.Columns[i].ColumnName + ",");
  634. }
  635. sb.Append(" where " + ID + " =:" + ID);
  636. //先添加参数
  637. Parameter.Add(ParName);
  638. //添加参数的具体内容
  639. for (int i = 0; i < ColumnCount; i++)
  640. {
  641. string[] par = new string[RowCount];
  642. for (int j = 0; j < RowCount; j++)
  643. {
  644. par[j] = dt.Rows[j][i].ToString();
  645. }
  646. Parameter.Add(par);
  647. }
  648. BatchInsert(sb.ToString(), Parameter.ToArray());
  649. }
  650. /// <summary>
  651. /// 批量通过SQL来执行插入操作 ,参数的第一个数一个string[]数组,用来传递需要添加的参数的名称
  652. /// 之后的是名称参数数组对应的 ,所有的插入参数数据长度必须是一致的
  653. /// </summary>
  654. /// <param name="sql"></param>
  655. /// <param name="names"></param>
  656. public void BatchInsert(string sql, params object[][] names)
  657. {
  658. command = new OleDbCommand(sql, this._odcConnection);
  659. Reconnect(command);
  660. //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
  661. //将第一个数组的下标固定为0作为循环添加的参数的名称
  662. for (int i = 1; i <= names[0].Length; i++)
  663. {
  664. command.Parameters.Add(new OleDbParameter(names[0][i - 1].ToString(),names[i]));
  665. }
  666. try
  667. {
  668. command.ExecuteNonQuery();
  669. }
  670. catch (Exception)
  671. {
  672. command.Connection = new OleDbConnection(this._connectionString);
  673. command.Connection.Open();
  674. command.ExecuteNonQuery();
  675. }
  676. command.Dispose();
  677. }
  678. public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
  679. {
  680. command = new OleDbCommand(sql, this._odcConnection);
  681. Reconnect(command);
  682. //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
  683. //将第一个数组的下标固定为0作为循环添加的参数的名称
  684. for (int i = 0; i < param.Length; i++)
  685. {
  686. command.Parameters.Add(new OleDbParameter(param[i].ToString(),param1[i]));
  687. }
  688. try
  689. {
  690. command.ExecuteNonQuery();
  691. }
  692. catch (Exception)
  693. {
  694. command.Connection = new OleDbConnection(this._connectionString);
  695. command.Connection.Open();
  696. command.ExecuteNonQuery();
  697. }
  698. command.Dispose();
  699. }
  700. /// <summary>
  701. /// 取Configs表中的配置,进行该客户是否执行某个操作
  702. /// </summary>
  703. /// <param name="Code"></param>
  704. /// <param name="Caller"></param>
  705. /// <returns></returns>
  706. public object GetConfig(string Code, string Caller)
  707. {
  708. DataTable dt = new DataTable();
  709. string sql = "select Data from configs where code='" + Code + "' and caller='" + Caller + "'";
  710. dt = (DataTable)ExecuteSql(sql, "select");
  711. if (dt.Rows.Count == 0)
  712. {
  713. return "";
  714. }
  715. else
  716. {
  717. return dt.Rows[0]["Data"];
  718. }
  719. }
  720. //将数据类型的列类型转换为DataTable
  721. public DataTable DataTypeColumnToDataTable(DataTable dt)
  722. {
  723. DataTable dt1 = new DataTable();
  724. dt1.Rows.Add();
  725. foreach (DataRow dr in dt.Rows)
  726. {
  727. dt1.Columns.Add(dr[0].ToString());
  728. int index = dt.Rows.IndexOf(dr);
  729. if (dr[1].ToString() == "NUMBER")
  730. {
  731. dt1.Rows[0][index] = 0;
  732. }
  733. if (dr[1].ToString() == "VARCHAR2")
  734. {
  735. dt1.Rows[0][index] = "这是一段文字";
  736. }
  737. if (dr[1].ToString() == "DATE")
  738. {
  739. dt1.Rows[0][index] = DateTime.Now.ToString("yyyy-MM-dd");
  740. }
  741. if (dr[1].ToString() == "FLOAT")
  742. {
  743. dt1.Rows[0][index] = 1.0;
  744. }
  745. if (dr[1].ToString() == "CLOB")
  746. {
  747. dt1.Rows[0][index] = "一段长文字";
  748. }
  749. }
  750. return dt1;
  751. }
  752. /// <summary>
  753. /// 通过条件更新
  754. /// </summary>
  755. /// <param name="TableName"></param>
  756. /// <param name="update"></param>
  757. /// <param name="condition"></param>
  758. public string UpdateByCondition(string TableName, string update, string condition)
  759. {
  760. string sql = "update " + TableName + " set " + update + " where " + condition;
  761. command = new OleDbCommand(sql, this._odcConnection);
  762. Reconnect(command);
  763. try
  764. {
  765. command.ExecuteNonQuery();
  766. }
  767. catch (Exception)
  768. {
  769. command.Connection = new OleDbConnection(this._connectionString);
  770. command.Connection.Open();
  771. command.ExecuteNonQuery();
  772. }
  773. command.Dispose();
  774. return sql;
  775. }
  776. /// <summary>
  777. /// 调用存储过程
  778. /// </summary>
  779. /// <param name="ProcedureName"></param>
  780. /// <param name="param"></param>
  781. public void CallProcedure(string ProcedureName, ref string[] param)
  782. {
  783. command = new OleDbCommand(ProcedureName);
  784. command.Connection = this._odcConnection;
  785. Reconnect(command);
  786. command.CommandText = ProcedureName;
  787. command.CommandType = CommandType.StoredProcedure;
  788. for (int i = 0; i < param.Length; i++)
  789. command.Parameters.Add(new OleDbParameter(param[i].ToString(), OleDbType.VarChar, 200, param[i]));
  790. try
  791. {
  792. command.ExecuteNonQuery();
  793. }
  794. catch (Exception)
  795. {
  796. command.Connection = new OleDbConnection(this._connectionString);
  797. command.Connection.Open();
  798. command.ExecuteNonQuery();
  799. }
  800. for (int i = 0; i < command.Parameters.Count; i++)
  801. param[i] = command.Parameters[i].Value.ToString();
  802. command.Dispose();
  803. }
  804. /// <summary>
  805. /// 出现异常进行回滚的执行方法
  806. /// </summary>
  807. /// <param name="SQL"></param>
  808. public void ExecuteSQLTran(params string[] SQL)
  809. {
  810. OleDbTransaction tx = this._odcConnection.BeginTransaction();
  811. command = new OleDbCommand();
  812. command.Connection = this._odcConnection;
  813. command.Transaction = tx;
  814. try
  815. {
  816. foreach (string sql in SQL)
  817. {
  818. if (!String.IsNullOrEmpty(sql))
  819. {
  820. command.CommandText = sql;
  821. try
  822. {
  823. command.ExecuteNonQuery();
  824. }
  825. catch (Exception)
  826. {
  827. command.Connection = new OleDbConnection(this._connectionString);
  828. command.Connection.Open();
  829. command.ExecuteNonQuery();
  830. }
  831. }
  832. }
  833. tx.Commit();
  834. }
  835. catch (Exception E)
  836. {
  837. tx.Rollback();
  838. throw new Exception(E.Message);
  839. }
  840. command.Dispose();
  841. }
  842. /// <summary>
  843. /// 用于将string 的数组转换成SQL的查询内容
  844. /// </summary>
  845. /// <param name="Fields"></param>
  846. /// <returns></returns>
  847. private string AddField(string[] Fields)
  848. {
  849. string sql = " ";
  850. foreach (string field in Fields)
  851. {
  852. sql += field + ",";
  853. }
  854. return sql.Substring(0, sql.Length - 1);
  855. }
  856. /// <summary>
  857. /// 通过查询的内容获取到字段的描述
  858. /// </summary>
  859. /// <param name="field"></param>
  860. /// <returns></returns>
  861. private static string[] GetCaptionFromField(string field)
  862. {
  863. string[] caption = field.Split(',');
  864. for (int i = 0; i < caption.Length; i++)
  865. {
  866. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  867. }
  868. return caption;
  869. }
  870. /// <summary>
  871. /// 通过查询的语句获取查询的字段
  872. /// </summary>
  873. /// <param name="field"></param>
  874. /// <returns></returns>
  875. private static string[] GetField(string field)
  876. {
  877. string[] fields = field.Split(',');
  878. for (int i = 0; i < fields.Length; i++)
  879. {
  880. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  881. }
  882. return fields;
  883. }
  884. public object GetLabelParam(string sql)
  885. {
  886. DataTable dt = new DataTable();
  887. command = new OleDbCommand(sql, this._odcConnection);
  888. Reconnect(command);
  889. OleDbDataAdapter ad = new OleDbDataAdapter();
  890. ad.SelectCommand = command;
  891. try
  892. {
  893. ad.Fill(dt);
  894. }
  895. catch (Exception)
  896. {
  897. this._odcConnection = new OleDbConnection(this._connectionString);
  898. this._odcConnection.Open();
  899. command = new OleDbCommand(sql, this._odcConnection);
  900. ad = new OleDbDataAdapter();
  901. ad.SelectCommand = command;
  902. ad.Fill(dt);
  903. }
  904. if (dt.Rows.Count > 0)
  905. {
  906. ad.Dispose();
  907. command.Dispose();
  908. return dt.Rows[0][0];
  909. }
  910. else
  911. {
  912. command.Dispose();
  913. return "";
  914. }
  915. }
  916. public static string[] GetParamFromSQL(string SQL)
  917. {
  918. string[] par = SQL.Split(':');
  919. //用来存参数的数组
  920. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  921. string[] param = new string[par.Length - 1];
  922. for (int i = 0; i < par.Length - 1; i++)
  923. {
  924. //新建一个char类型的数组用来存储每个字节的变量
  925. char[] c = par[i + 1].ToCharArray();
  926. addpar[i] = new StringBuilder();
  927. for (int j = 0; j < c.Length; j++)
  928. {
  929. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  930. {
  931. addpar[i].Append(c[j]);
  932. }
  933. else
  934. {
  935. break;
  936. }
  937. }
  938. }
  939. for (int i = 0; i < par.Length - 1; i++)
  940. {
  941. param[i] = addpar[i].ToString();
  942. }
  943. return param;
  944. }
  945. }
  946. }