AccessDBHelper.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.IO;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace UAS_LabelMachine
  9. {
  10. class AccessDBHelper
  11. {
  12. private string _fileName;
  13. private string _connectionString;
  14. private OleDbConnection _odcConnection = null;
  15. private OleDbCommand command = null;
  16. public AccessDBHelper(string fileName)
  17. {
  18. this._fileName = fileName;
  19. this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";
  20. Open();
  21. }
  22. public AccessDBHelper(string fileName, string password)
  23. {
  24. this._fileName = fileName;
  25. this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Jet OLEDB:Database Password=" + password + ";";
  26. Open();
  27. }
  28. public void Open()
  29. {
  30. try
  31. {
  32. // 建立连接
  33. this._odcConnection = new OleDbConnection(this._connectionString);
  34. // 打开连接
  35. this._odcConnection.Open();
  36. }
  37. catch (Exception ex)
  38. {
  39. MessageBox.Show(ex.Message);
  40. }
  41. }
  42. public void Close()
  43. {
  44. this._odcConnection.Close();
  45. }
  46. public DataTable GetDataTable(string sql)
  47. {
  48. DataTable ds = new DataTable();
  49. command = new OleDbCommand(sql, this._odcConnection);
  50. Reconnect(command);
  51. OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  52. try
  53. {
  54. adapter.Fill(ds);
  55. }
  56. catch (Exception ex)
  57. {
  58. throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
  59. }
  60. adapter.Dispose();
  61. return ds;
  62. }
  63. public DataSet GetDataSet(string sql)
  64. {
  65. DataSet ds = new DataSet();
  66. command = new OleDbCommand(sql, this._odcConnection);
  67. Reconnect(command);
  68. OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  69. try
  70. {
  71. adapter.Fill(ds);
  72. }
  73. catch (Exception ex)
  74. {
  75. throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
  76. }
  77. adapter.Dispose();
  78. return ds;
  79. }
  80. public void Reconnect(OleDbCommand cmd)
  81. {
  82. if (cmd.Connection.State == ConnectionState.Closed)
  83. {
  84. cmd.Connection.Open();
  85. //超时重连
  86. }
  87. }
  88. /// <summary>
  89. /// 根据表名获取该表字段数据类型
  90. /// </summary>
  91. public DataTable GetColumnDataType(string TableName)
  92. {
  93. DataTable dt = new DataTable();
  94. command = new OleDbCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", this._odcConnection);
  95. Reconnect(command);
  96. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  97. ad.Fill(dt);
  98. ad.Dispose();
  99. command.Dispose();
  100. return dt;
  101. }
  102. /// <summary>
  103. /// 获取第一行第一列的信息
  104. /// </summary>
  105. public object getFieldDataByCondition(string TableName, string Field, string Condition)
  106. {
  107. DataTable dt = new DataTable();
  108. string sql = "select " + Field + " from " + TableName + " where " + Condition;
  109. command = new OleDbCommand(sql, this._odcConnection);
  110. Reconnect(command);
  111. OleDbDataAdapter ad = new OleDbDataAdapter();
  112. ad.SelectCommand = command;
  113. try
  114. {
  115. ad.Fill(dt);
  116. }
  117. catch (Exception)
  118. {
  119. this._odcConnection = new OleDbConnection(this._connectionString);
  120. this._odcConnection.Open();
  121. command = new OleDbCommand(sql, this._odcConnection);
  122. ad = new OleDbDataAdapter();
  123. ad.SelectCommand = command;
  124. ad.Fill(dt);
  125. }
  126. ad.Dispose();
  127. command.Dispose();
  128. if (dt.Rows.Count > 0)
  129. {
  130. return dt.Rows[0][0];
  131. }
  132. else
  133. {
  134. return "";
  135. }
  136. }
  137. public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
  138. {
  139. command = new OleDbCommand(sql, _odcConnection);
  140. OleDbDataAdapter adapter = new OleDbDataAdapter();
  141. adapter.UpdateBatchSize = param1[0].Length;
  142. //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
  143. //将第一个数组的下标固定为0作为循环添加的参数的名称
  144. for (int i = 0; i < param.Length; i++)
  145. {
  146. command.Parameters.Add(new OleDbParameter(param[i].ToString(), param1[i]));
  147. }
  148. try
  149. {
  150. command.ExecuteNonQuery();
  151. }
  152. catch (Exception)
  153. {
  154. }
  155. command.Dispose();
  156. }
  157. public void DeleleFile(string accessFile )
  158. {
  159. var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
  160. try
  161. {
  162. string tempFile = Path.Combine(Path.GetDirectoryName(accessFile),Path.GetRandomFileName() + Path.GetExtension(accessFile));
  163. dbe.CompactDatabase(accessFile, tempFile);
  164. FileInfo temp = new FileInfo(tempFile);
  165. temp.CopyTo(accessFile, true);
  166. temp.Delete();
  167. }
  168. catch (Exception e)
  169. {
  170. Console.WriteLine("Error: " + e.Message);
  171. }
  172. }
  173. public void SaveDataTable(DataTable dt, string TableName)
  174. {
  175. StringBuilder sb = new StringBuilder();
  176. //预防插入的DataTable中存在不属于该表的列,在进行下一步操作之前全部剔除
  177. DataTable data = (DataTable)ExecuteSql("select top 1 * from " + TableName, "select");
  178. //将所有的字段拼接起来
  179. for (int i = 0; i < data.Columns.Count; i++)
  180. {
  181. sb.Append("#" + data.Columns[i].ColumnName.ToString().ToUpper());
  182. }
  183. //移除掉所有不属于该表的列
  184. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  185. {
  186. if (!sb.ToString().Contains(dt.Columns[i].ColumnName.ToUpper()))
  187. {
  188. dt.Columns.RemoveAt(i);
  189. }
  190. }
  191. sb.Clear();
  192. sb.Append("insert into prodiobarcode (");
  193. string field = "";
  194. string valuefield = "";
  195. for (int i = 0; i < dt.Columns.Count; i++)
  196. {
  197. if (i != dt.Columns.Count - 1)
  198. {
  199. field += dt.Columns[i].ColumnName + ",";
  200. valuefield += "?,";
  201. }
  202. else
  203. {
  204. field += dt.Columns[i].ColumnName + "";
  205. valuefield += "?";
  206. }
  207. }
  208. sb.Append(field + ") values(" + valuefield + ")");
  209. OleDbTransaction tx = this._odcConnection.BeginTransaction();
  210. command = new OleDbCommand();
  211. command.Connection = this._odcConnection;
  212. command.Transaction = tx;
  213. string sql = sb.ToString();
  214. try
  215. {
  216. for (int i = 0; i < dt.Rows.Count; i++)
  217. {
  218. command.CommandText = sql;
  219. for (int j = 0; j < dt.Columns.Count; j++)
  220. {
  221. command.Parameters.AddWithValue(dt.Columns[j].ColumnName, dt.Rows[i][j]);
  222. }
  223. try
  224. {
  225. command.ExecuteNonQuery();
  226. command.Parameters.Clear();
  227. }
  228. catch (Exception e)
  229. {
  230. MessageBox.Show(e.Message.ToString());
  231. }
  232. }
  233. tx.Commit();
  234. }
  235. catch (Exception E)
  236. {
  237. tx.Rollback();
  238. throw new Exception(E.Message);
  239. }
  240. command.Dispose();
  241. //string[][] param_array = new string[dt.Columns.Count][];
  242. //for (int i = 0; i < dt.Columns.Count; i++)
  243. //{
  244. // param_array[i] = new string[dt.Rows.Count];
  245. //}
  246. ////变量所有的行,如果有主键为空的则移除,不为空的进行参数的拼接
  247. //for (int i = dt.Rows.Count - 1; i >= 0; i--)
  248. //{
  249. // //当为新添加行的时候才去设置参数,设置过后索引+1
  250. // for (int j = 0; j < dt.Columns.Count; j++)
  251. // {
  252. // param_array[j][i] = dt.Rows[i][j].ToString();
  253. // }
  254. //}
  255. //for (int i = 0; i < dt.Columns.Count; i++)
  256. //{
  257. // command.Parameters.AddWithValue(dt.Columns[i].ColumnName, param_array[i]);
  258. //}
  259. //command.ExecuteNonQuery();
  260. }
  261. public void BatchInsert(string tableName, DataTable dt)
  262. {
  263. List<string> columnList = new List<string>();
  264. foreach (DataColumn one in dt.Columns)
  265. {
  266. columnList.Add(one.ColumnName);
  267. }
  268. OleDbDataAdapter adapter = new OleDbDataAdapter();
  269. adapter.SelectCommand = new OleDbCommand("select pib_id from " + tableName, this._odcConnection);
  270. using (OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter))
  271. {
  272. adapter.InsertCommand = builder.GetInsertCommand();
  273. for (int i = 0; i < adapter.InsertCommand.Parameters.Count; i++)
  274. {
  275. Console.WriteLine(adapter.InsertCommand.Parameters[i].ParameterName);
  276. }
  277. foreach (string one in columnList)
  278. {
  279. Console.WriteLine(one.ToLower());
  280. adapter.InsertCommand.Parameters.Add(new OleDbParameter(one.ToLower(), "Test"));
  281. }
  282. Console.WriteLine(adapter.Update(dt));
  283. }
  284. }
  285. /// <summary>
  286. /// 获取指定表的记录的条数 ,带条件
  287. /// </summary>
  288. /// <returns></returns>
  289. public int getRowCount(string TableName, string Condition)
  290. {
  291. DataTable dt = new DataTable();
  292. string sql = "select count(1) from " + TableName + " where " + Condition;
  293. command = new OleDbCommand(sql, this._odcConnection);
  294. Reconnect(command);
  295. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  296. ad.Fill(dt);
  297. ad.Dispose();
  298. command.Dispose();
  299. return int.Parse(dt.Rows[0][0].ToString());
  300. }
  301. /// <summary>
  302. /// 获取指定表的记录的条数 ,不带条件
  303. /// </summary>
  304. /// <param name="TableName"></param>
  305. /// <returns></returns>
  306. public int getRowCount(string TableName)
  307. {
  308. DataTable dt = new DataTable();
  309. string sql = "select count(1) from " + TableName;
  310. command = new OleDbCommand(sql, this._odcConnection);
  311. Reconnect(command);
  312. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  313. ad.Fill(dt);
  314. ad.Dispose();
  315. command.Dispose();
  316. return int.Parse(dt.Rows[0][0].ToString());
  317. }
  318. /// <summary>
  319. /// 通过表名和获取单行的记录
  320. /// </summary>
  321. public DataTable getFieldsDataByCondition(string TableName, string[] Fields, string Condition)
  322. {
  323. DataTable dt = new DataTable();
  324. string sql = "select ";
  325. sql += AddField(Fields);
  326. sql += " from " + TableName + " where " + Condition + " and rownum=1";
  327. command = new OleDbCommand(sql, this._odcConnection);
  328. Reconnect(command);
  329. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  330. try
  331. {
  332. ad.Fill(dt);
  333. }
  334. catch (Exception)
  335. {
  336. this._odcConnection = new OleDbConnection(this._connectionString);
  337. this._odcConnection.Open();
  338. command = new OleDbCommand(sql, this._odcConnection);
  339. ad = new OleDbDataAdapter();
  340. ad.SelectCommand = command;
  341. ad.Fill(dt);
  342. }
  343. ad.Dispose();
  344. command.Dispose();
  345. return dt;
  346. }
  347. /// <summary>
  348. /// 将DataTable导入到指定的表中
  349. /// </summary>
  350. /// <param name="DataTable"></param>
  351. /// <param name="TableName"></param>
  352. public void InsertDataTable(DataTable DataTable, string TableName)
  353. {
  354. for (int i = 0; i < DataTable.Rows.Count; i++)
  355. {
  356. for (int j = 0; j < DataTable.Columns.Count; j++)
  357. {
  358. }
  359. }
  360. }
  361. /// <summary>
  362. /// 按分页获取数据
  363. /// </summary>
  364. /// <param name="TableName">表名</param>
  365. /// <param name="Fields">查询字段</param>
  366. /// <param name="CurrentPage">当前页面</param>
  367. /// <param name="PageSize">页面展示条数</param>
  368. /// <param name="Caller"></param>
  369. /// <returns></returns>
  370. // SELECT * FROM (SELECT A.* FROM (SELECT* FROM datalist) A WHERE ROWNUM <= 50) WHERE ROWNUM >= 21
  371. public DataTable getFieldsDatasByPageing(string TableName, string Fields, int CurrentPage, int PageSize, string Caller, params string[] condition)
  372. {
  373. DataTable dt = new DataTable();
  374. StringBuilder sql = new StringBuilder();
  375. //先查询出配置出去的列
  376. //获取查询的列
  377. string[] caption = GetCaptionFromField(Fields);
  378. //获取对应列的描述
  379. string[] field = GetField(Fields);
  380. sql.Append(" select * from (select RowNum RN, A.* from (select ");
  381. sql.Append(AddField(caption));
  382. if (condition.Length > 0)
  383. {
  384. if (condition[0] != null && condition[0].Trim() != "")
  385. sql.Append(" from " + TableName + " where " + condition[0] + " ) A where ROWNUM <=" + CurrentPage * PageSize + ") where RN>" + (CurrentPage - 1) * PageSize);
  386. else
  387. sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
  388. }
  389. command = new OleDbCommand(sql.ToString(), this._odcConnection);
  390. Reconnect(command);
  391. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  392. ad.Fill(dt);
  393. ad.Dispose();
  394. command.Dispose();
  395. dt.Columns.RemoveAt(0);
  396. foreach (DataColumn dc in dt.Columns)
  397. {
  398. dc.ColumnName = field[dt.Columns.IndexOf(dc)];
  399. dc.Caption = caption[dt.Columns.IndexOf(dc)];
  400. }
  401. return dt;
  402. }
  403. /// <summary>
  404. /// 通过表名,字段和条件获取DataTable类型的数据
  405. /// </summary>
  406. public DataTable getFieldsDatasByCondition(string TableName, string[] Fields, string Condition)
  407. {
  408. DataTable dt = new DataTable();
  409. string sql = "select ";
  410. sql += AddField(Fields);
  411. sql += " from " + TableName + " where " + Condition;
  412. command = new OleDbCommand(sql, this._odcConnection);
  413. Reconnect(command);
  414. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  415. try
  416. {
  417. ad.Fill(dt);
  418. }
  419. catch (Exception)
  420. {
  421. this._odcConnection = new OleDbConnection(this._connectionString);
  422. this._odcConnection.Open();
  423. command = new OleDbCommand(sql, this._odcConnection);
  424. ad = new OleDbDataAdapter();
  425. ad.SelectCommand = command;
  426. ad.Fill(dt);
  427. }
  428. ad.Dispose();
  429. command.Dispose();
  430. return dt;
  431. }
  432. /// <summary>
  433. /// 通过表名,字段获取DataTable类型的数据
  434. /// </summary>
  435. public DataTable getFieldsDatas(string TableName, string Fields)
  436. {
  437. DataTable dt = new DataTable();
  438. string sql = "select ";
  439. sql += Fields;
  440. sql += " from " + TableName;
  441. command = new OleDbCommand(sql, this._odcConnection);
  442. Reconnect(command);
  443. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  444. ad.SelectCommand = command;
  445. try
  446. {
  447. ad.Fill(dt);
  448. }
  449. catch (Exception)
  450. {
  451. this._odcConnection = new OleDbConnection(this._connectionString);
  452. this._odcConnection.Open();
  453. command = new OleDbCommand(sql, this._odcConnection);
  454. ad = new OleDbDataAdapter();
  455. ad.SelectCommand = command;
  456. ad.Fill(dt);
  457. }
  458. ad.Dispose();
  459. command.Dispose();
  460. return dt;
  461. }
  462. /// <summary>
  463. /// 获取DbFind的数据的DataTable的结构
  464. /// </summary>
  465. /// <param name="field"></param>
  466. /// <param name="caller"></param>
  467. /// <returns></returns>
  468. public DataTable GetDbFindDataTable(string field, string caller)
  469. {
  470. string sql = "select * from dbfindsetui where ds_caller='" + caller + "' and ds_whichui='" + field + "'";
  471. DataTable dt = (DataTable)ExecuteSql(sql, "select");
  472. if (dt.Rows.Count != 0)
  473. {
  474. //通过#号分割字段
  475. string[] dbfield = dt.Rows[0]["ds_findtoui"].ToString().Split('#');
  476. string[] cnfield = dt.Rows[0]["ds_dbcaption"].ToString().Split('#');
  477. //获取查询要查询的Table
  478. string dbtable = dt.Rows[0]["ds_tables"].ToString();
  479. //拼接查询的字段
  480. for (int i = 0; i < dbfield.Length; i++)
  481. {
  482. dbfield[i] = dbfield[i].Split(',')[0];
  483. }
  484. //新建一个空的DataTable
  485. DataTable dt1 = new DataTable();
  486. //往空的DataTable添加结构,ColumnName是中文,Caption是实际的字段名称
  487. for (int i = 0; i < cnfield.Length; i++)
  488. {
  489. dt1.Columns.Add(cnfield[i]);
  490. dt1.Columns[i].Caption = dbfield[i];
  491. }
  492. //返回一个带有结构的空的DataTable
  493. //DbFind.BindTable1 = dbtable;
  494. return dt1;
  495. }
  496. else
  497. {
  498. return null;
  499. }
  500. }
  501. /// <summary>
  502. /// 检测内容是否存在
  503. /// </summary>
  504. /// <param name="TableName"></param>
  505. /// <param name="Condition"></param>
  506. /// <returns></returns>
  507. public bool CheckExist(string TableName, string Condition)
  508. {
  509. string sql = "select count(1) from " + TableName + " where " + Condition;
  510. command = new OleDbCommand(sql, this._odcConnection);
  511. Reconnect(command);
  512. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  513. DataTable dt = new DataTable();
  514. try
  515. {
  516. ad.Fill(dt);
  517. }
  518. catch (Exception)
  519. {
  520. this._odcConnection = new OleDbConnection(this._connectionString);
  521. this._odcConnection.Open();
  522. command = new OleDbCommand(sql, this._odcConnection);
  523. ad = new OleDbDataAdapter();
  524. ad.SelectCommand = command;
  525. ad.Fill(dt);
  526. }
  527. ad.Dispose();
  528. command.Dispose();
  529. return int.Parse(dt.Rows[0][0].ToString()) > 0;
  530. }
  531. /// <summary>
  532. /// 直接执行SQL,同时传入SQL的类型
  533. /// </summary>
  534. /// <param name="SQL"></param>
  535. /// <param name="Type"></param>
  536. /// <returns></returns>
  537. public object ExecuteSql(string SQL, string Type, params object[] names)
  538. {
  539. object result = null;
  540. command = new OleDbCommand(SQL, this._odcConnection);
  541. Reconnect(command);
  542. //用来拼接参数的
  543. if (names.Length > 0)
  544. {
  545. string[] par = SQL.Split(':');
  546. //用来存参数的数组
  547. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  548. for (int i = 0; i < par.Length - 1; i++)
  549. {
  550. //新建一个char类型的数组用来存储每个字节的变量
  551. char[] c = par[i + 1].ToCharArray();
  552. addpar[i] = new StringBuilder();
  553. for (int j = 0; j < c.Length; j++)
  554. {
  555. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  556. {
  557. addpar[i].Append(c[j]);
  558. }
  559. else
  560. {
  561. break;
  562. }
  563. }
  564. }
  565. for (int i = 0; i < addpar.Length; i++)
  566. command.Parameters.Add(new OleDbParameter(addpar[i].ToString(), names[i]));
  567. }
  568. switch (Type.ToUpper())
  569. {
  570. case "SELECT":
  571. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  572. result = new DataTable();
  573. try
  574. {
  575. ad.Fill((DataTable)result);
  576. }
  577. catch (Exception)
  578. {
  579. this._odcConnection = new OleDbConnection(this._connectionString);
  580. this._odcConnection.Open();
  581. command = new OleDbCommand(SQL, this._odcConnection);
  582. ad = new OleDbDataAdapter();
  583. ad.SelectCommand = command;
  584. ad.Fill((DataTable)result);
  585. }
  586. break;
  587. case "DELETE":
  588. try
  589. {
  590. result = command.ExecuteNonQuery();
  591. }
  592. catch (Exception)
  593. {
  594. command.Connection = new OleDbConnection(this._connectionString);
  595. command.Connection.Open();
  596. result = command.ExecuteNonQuery();
  597. }
  598. break;
  599. case "UPDATE":
  600. try
  601. {
  602. result = command.ExecuteNonQuery();
  603. }
  604. catch (Exception)
  605. {
  606. command.Connection = new OleDbConnection(this._connectionString);
  607. command.Connection.Open();
  608. result = command.ExecuteNonQuery();
  609. }
  610. break;
  611. case "INSERT":
  612. try
  613. {
  614. result = command.ExecuteNonQuery();
  615. }
  616. catch (Exception)
  617. {
  618. command.Connection = new OleDbConnection(this._connectionString);
  619. command.Connection.Open();
  620. result = command.ExecuteNonQuery();
  621. }
  622. break;
  623. }
  624. command.Dispose();
  625. return result;
  626. }
  627. /// <summary>
  628. /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
  629. /// </summary>
  630. /// <param name="SQL"></param>
  631. public void CheckSQL(string SQL)
  632. {
  633. SQL = SQL.Replace("?", ":Param");
  634. command = new OleDbCommand(SQL, this._odcConnection);
  635. command.ExecuteNonQuery();
  636. command.Dispose();
  637. }
  638. public int GetDistinctRowCount(string TableName, string Field)
  639. {
  640. DataTable dt = new DataTable();
  641. string sql = "select distinct count('" + Field + "') from " + TableName;
  642. command = new OleDbCommand(sql, this._odcConnection);
  643. Reconnect(command);
  644. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  645. try
  646. {
  647. ad.Fill(dt);
  648. }
  649. catch (Exception)
  650. {
  651. this._odcConnection = new OleDbConnection(this._connectionString);
  652. this._odcConnection.Open();
  653. command = new OleDbCommand(sql, this._odcConnection);
  654. ad = new OleDbDataAdapter();
  655. ad.SelectCommand = command;
  656. ad.Fill(dt);
  657. }
  658. ad.Dispose();
  659. command.Dispose();
  660. return int.Parse(dt.Rows[0][0].ToString());
  661. }
  662. /// <summary>
  663. /// 根据Caller获取流水号
  664. /// </summary>
  665. /// <param name="Caller"></param>
  666. /// <returns></returns>
  667. public string GetSerialNumberByCaller(string Caller)
  668. {
  669. string SerialNumber = getFieldDataByCondition("MaxNumbers", "mn_number", "mn_tablename='" + Caller + "'").ToString();
  670. UpdateByCondition("MaxNumbers", "mn_number=mn_number+1", "mn_tablename='" + Caller + "'");
  671. return SerialNumber;
  672. }
  673. /// <summary>
  674. /// 通过序列的名称获取序列
  675. /// </summary>
  676. /// <param name="SeqName"></param>
  677. /// <returns></returns>
  678. public string GetSEQ(string SeqName)
  679. {
  680. DataTable dt = new DataTable();
  681. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
  682. return dt.Rows[0][0].ToString();
  683. }
  684. /// <summary>
  685. /// 通过序列的名称获取序列
  686. /// </summary>
  687. /// <param name="SeqName"></param>
  688. /// <returns></returns>
  689. public string[] GetSEQ(string SeqName, int Num)
  690. {
  691. DataTable dt = new DataTable();
  692. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".nextval FROM DUAL CONNECT BY LEVEL<=" + Num, "select");
  693. string[] SerialNum = new string[dt.Rows.Count];
  694. for (int i = 0; i < dt.Rows.Count; i++)
  695. {
  696. SerialNum[i] = dt.Rows[i][0].ToString();
  697. }
  698. return SerialNum;
  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. }