AccessDBHelper.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. /// <summary>
  174. /// 插入DataTable到Access数据库
  175. /// </summary>
  176. /// <param name="dt"></param>
  177. /// <param name="TableName"></param>
  178. public void SaveDataTable(DataTable dt, string TableName)
  179. {
  180. StringBuilder sb = new StringBuilder();
  181. //预防插入的DataTable中存在不属于该表的列,在进行下一步操作之前全部剔除
  182. //DataTable data = (DataTable)ExecuteSql("select top 1 * from " + TableName, "select");
  183. ////将所有的字段拼接起来
  184. //for (int i = 0; i < data.Columns.Count; i++)
  185. //{
  186. // sb.Append("#" + data.Columns[i].ColumnName.ToString().ToUpper());
  187. //}
  188. ////移除掉所有不属于该表的列
  189. //for (int i = dt.Columns.Count - 1; i >= 0; i--)
  190. //{
  191. // if (!sb.ToString().Contains(dt.Columns[i].ColumnName.ToUpper()))
  192. // {
  193. // dt.Columns.RemoveAt(i);
  194. // }
  195. //}
  196. sb.Clear();
  197. sb.Append("insert into " + TableName + " (");
  198. string field = "";
  199. string valuefield = "";
  200. for (int i = 0; i < dt.Columns.Count; i++)
  201. {
  202. if (i != dt.Columns.Count - 1)
  203. {
  204. field += dt.Columns[i].ColumnName + ",";
  205. valuefield += "?,";
  206. }
  207. else
  208. {
  209. field += dt.Columns[i].ColumnName + "";
  210. valuefield += "?";
  211. }
  212. }
  213. sb.Append(field + ") values(" + valuefield + ")");
  214. OleDbTransaction tx = this._odcConnection.BeginTransaction();
  215. command = new OleDbCommand();
  216. command.Connection = this._odcConnection;
  217. command.Transaction = tx;
  218. string sql = sb.ToString();
  219. try
  220. {
  221. for (int i = 0; i < dt.Rows.Count; i++)
  222. {
  223. command.CommandText = sql;
  224. for (int j = 0; j < dt.Columns.Count; j++)
  225. {
  226. command.Parameters.AddWithValue(dt.Columns[j].ColumnName, dt.Rows[i][j]);
  227. }
  228. try
  229. {
  230. command.ExecuteNonQuery();
  231. command.Parameters.Clear();
  232. }
  233. catch (Exception e)
  234. {
  235. MessageBox.Show(e.Message.ToString());
  236. }
  237. }
  238. tx.Commit();
  239. }
  240. catch (Exception E)
  241. {
  242. tx.Rollback();
  243. throw new Exception(E.Message);
  244. }
  245. command.Dispose();
  246. //string[][] param_array = new string[dt.Columns.Count][];
  247. //for (int i = 0; i < dt.Columns.Count; i++)
  248. //{
  249. // param_array[i] = new string[dt.Rows.Count];
  250. //}
  251. ////变量所有的行,如果有主键为空的则移除,不为空的进行参数的拼接
  252. //for (int i = dt.Rows.Count - 1; i >= 0; i--)
  253. //{
  254. // //当为新添加行的时候才去设置参数,设置过后索引+1
  255. // for (int j = 0; j < dt.Columns.Count; j++)
  256. // {
  257. // param_array[j][i] = dt.Rows[i][j].ToString();
  258. // }
  259. //}
  260. //for (int i = 0; i < dt.Columns.Count; i++)
  261. //{
  262. // command.Parameters.AddWithValue(dt.Columns[i].ColumnName, param_array[i]);
  263. //}
  264. //command.ExecuteNonQuery();
  265. }
  266. public void BatchInsert(string tableName, DataTable dt)
  267. {
  268. List<string> columnList = new List<string>();
  269. foreach (DataColumn one in dt.Columns)
  270. {
  271. columnList.Add(one.ColumnName);
  272. }
  273. OleDbDataAdapter adapter = new OleDbDataAdapter();
  274. adapter.SelectCommand = new OleDbCommand("select pib_id from " + tableName, this._odcConnection);
  275. using (OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter))
  276. {
  277. adapter.InsertCommand = builder.GetInsertCommand();
  278. for (int i = 0; i < adapter.InsertCommand.Parameters.Count; i++)
  279. {
  280. Console.WriteLine(adapter.InsertCommand.Parameters[i].ParameterName);
  281. }
  282. foreach (string one in columnList)
  283. {
  284. Console.WriteLine(one.ToLower());
  285. adapter.InsertCommand.Parameters.Add(new OleDbParameter(one.ToLower(), "Test"));
  286. }
  287. Console.WriteLine(adapter.Update(dt));
  288. }
  289. }
  290. /// <summary>
  291. /// 获取指定表的记录的条数 ,带条件
  292. /// </summary>
  293. /// <returns></returns>
  294. public int getRowCount(string TableName, string Condition)
  295. {
  296. DataTable dt = new DataTable();
  297. string sql = "select count(1) from " + TableName + " where " + Condition;
  298. command = new OleDbCommand(sql, this._odcConnection);
  299. Reconnect(command);
  300. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  301. ad.Fill(dt);
  302. ad.Dispose();
  303. command.Dispose();
  304. return int.Parse(dt.Rows[0][0].ToString());
  305. }
  306. /// <summary>
  307. /// 获取指定表的记录的条数 ,不带条件
  308. /// </summary>
  309. /// <param name="TableName"></param>
  310. /// <returns></returns>
  311. public int getRowCount(string TableName)
  312. {
  313. DataTable dt = new DataTable();
  314. string sql = "select count(1) from " + TableName;
  315. command = new OleDbCommand(sql, this._odcConnection);
  316. Reconnect(command);
  317. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  318. ad.Fill(dt);
  319. ad.Dispose();
  320. command.Dispose();
  321. return int.Parse(dt.Rows[0][0].ToString());
  322. }
  323. /// <summary>
  324. /// 通过表名和获取单行的记录
  325. /// </summary>
  326. public DataTable getFieldsDataByCondition(string TableName, string[] Fields, string Condition)
  327. {
  328. DataTable dt = new DataTable();
  329. string sql = "select ";
  330. sql += AddField(Fields);
  331. sql += " from " + TableName + " where " + Condition + " and rownum=1";
  332. command = new OleDbCommand(sql, this._odcConnection);
  333. Reconnect(command);
  334. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  335. try
  336. {
  337. ad.Fill(dt);
  338. }
  339. catch (Exception)
  340. {
  341. this._odcConnection = new OleDbConnection(this._connectionString);
  342. this._odcConnection.Open();
  343. command = new OleDbCommand(sql, this._odcConnection);
  344. ad = new OleDbDataAdapter();
  345. ad.SelectCommand = command;
  346. ad.Fill(dt);
  347. }
  348. ad.Dispose();
  349. command.Dispose();
  350. return dt;
  351. }
  352. /// <summary>
  353. /// 将DataTable导入到指定的表中
  354. /// </summary>
  355. /// <param name="DataTable"></param>
  356. /// <param name="TableName"></param>
  357. public void InsertDataTable(DataTable DataTable, string TableName)
  358. {
  359. for (int i = 0; i < DataTable.Rows.Count; i++)
  360. {
  361. for (int j = 0; j < DataTable.Columns.Count; j++)
  362. {
  363. }
  364. }
  365. }
  366. /// <summary>
  367. /// 按分页获取数据
  368. /// </summary>
  369. /// <param name="TableName">表名</param>
  370. /// <param name="Fields">查询字段</param>
  371. /// <param name="CurrentPage">当前页面</param>
  372. /// <param name="PageSize">页面展示条数</param>
  373. /// <param name="Caller"></param>
  374. /// <returns></returns>
  375. // SELECT * FROM (SELECT A.* FROM (SELECT* FROM datalist) A WHERE ROWNUM <= 50) WHERE ROWNUM >= 21
  376. public DataTable getFieldsDatasByPageing(string TableName, string Fields, int CurrentPage, int PageSize, string Caller, params string[] condition)
  377. {
  378. DataTable dt = new DataTable();
  379. StringBuilder sql = new StringBuilder();
  380. //先查询出配置出去的列
  381. //获取查询的列
  382. string[] caption = GetCaptionFromField(Fields);
  383. //获取对应列的描述
  384. string[] field = GetField(Fields);
  385. sql.Append(" select * from (select RowNum RN, A.* from (select ");
  386. sql.Append(AddField(caption));
  387. if (condition.Length > 0)
  388. {
  389. if (condition[0] != null && condition[0].Trim() != "")
  390. sql.Append(" from " + TableName + " where " + condition[0] + " ) A where ROWNUM <=" + CurrentPage * PageSize + ") where RN>" + (CurrentPage - 1) * PageSize);
  391. else
  392. sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
  393. }
  394. command = new OleDbCommand(sql.ToString(), this._odcConnection);
  395. Reconnect(command);
  396. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  397. ad.Fill(dt);
  398. ad.Dispose();
  399. command.Dispose();
  400. dt.Columns.RemoveAt(0);
  401. foreach (DataColumn dc in dt.Columns)
  402. {
  403. dc.ColumnName = field[dt.Columns.IndexOf(dc)];
  404. dc.Caption = caption[dt.Columns.IndexOf(dc)];
  405. }
  406. return dt;
  407. }
  408. /// <summary>
  409. /// 通过表名,字段和条件获取DataTable类型的数据
  410. /// </summary>
  411. public DataTable getFieldsDatasByCondition(string TableName, string[] Fields, string Condition)
  412. {
  413. DataTable dt = new DataTable();
  414. string sql = "select ";
  415. sql += AddField(Fields);
  416. sql += " from " + TableName + " where " + Condition;
  417. command = new OleDbCommand(sql, this._odcConnection);
  418. Reconnect(command);
  419. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  420. try
  421. {
  422. ad.Fill(dt);
  423. }
  424. catch (Exception)
  425. {
  426. this._odcConnection = new OleDbConnection(this._connectionString);
  427. this._odcConnection.Open();
  428. command = new OleDbCommand(sql, this._odcConnection);
  429. ad = new OleDbDataAdapter();
  430. ad.SelectCommand = command;
  431. ad.Fill(dt);
  432. }
  433. ad.Dispose();
  434. command.Dispose();
  435. return dt;
  436. }
  437. /// <summary>
  438. /// 通过表名,字段获取DataTable类型的数据
  439. /// </summary>
  440. public DataTable getFieldsDatas(string TableName, string Fields)
  441. {
  442. DataTable dt = new DataTable();
  443. string sql = "select ";
  444. sql += Fields;
  445. sql += " from " + TableName;
  446. command = new OleDbCommand(sql, this._odcConnection);
  447. Reconnect(command);
  448. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  449. ad.SelectCommand = command;
  450. try
  451. {
  452. ad.Fill(dt);
  453. }
  454. catch (Exception)
  455. {
  456. this._odcConnection = new OleDbConnection(this._connectionString);
  457. this._odcConnection.Open();
  458. command = new OleDbCommand(sql, this._odcConnection);
  459. ad = new OleDbDataAdapter();
  460. ad.SelectCommand = command;
  461. ad.Fill(dt);
  462. }
  463. ad.Dispose();
  464. command.Dispose();
  465. return dt;
  466. }
  467. /// <summary>
  468. /// 获取DbFind的数据的DataTable的结构
  469. /// </summary>
  470. /// <param name="field"></param>
  471. /// <param name="caller"></param>
  472. /// <returns></returns>
  473. public DataTable GetDbFindDataTable(string field, string caller)
  474. {
  475. string sql = "select * from dbfindsetui where ds_caller='" + caller + "' and ds_whichui='" + field + "'";
  476. DataTable dt = (DataTable)ExecuteSql(sql, "select");
  477. if (dt.Rows.Count != 0)
  478. {
  479. //通过#号分割字段
  480. string[] dbfield = dt.Rows[0]["ds_findtoui"].ToString().Split('#');
  481. string[] cnfield = dt.Rows[0]["ds_dbcaption"].ToString().Split('#');
  482. //获取查询要查询的Table
  483. string dbtable = dt.Rows[0]["ds_tables"].ToString();
  484. //拼接查询的字段
  485. for (int i = 0; i < dbfield.Length; i++)
  486. {
  487. dbfield[i] = dbfield[i].Split(',')[0];
  488. }
  489. //新建一个空的DataTable
  490. DataTable dt1 = new DataTable();
  491. //往空的DataTable添加结构,ColumnName是中文,Caption是实际的字段名称
  492. for (int i = 0; i < cnfield.Length; i++)
  493. {
  494. dt1.Columns.Add(cnfield[i]);
  495. dt1.Columns[i].Caption = dbfield[i];
  496. }
  497. //返回一个带有结构的空的DataTable
  498. //DbFind.BindTable1 = dbtable;
  499. return dt1;
  500. }
  501. else
  502. {
  503. return null;
  504. }
  505. }
  506. /// <summary>
  507. /// 检测内容是否存在
  508. /// </summary>
  509. /// <param name="TableName"></param>
  510. /// <param name="Condition"></param>
  511. /// <returns></returns>
  512. public bool CheckExist(string TableName, string Condition)
  513. {
  514. string sql = "select count(1) from " + TableName + " where " + Condition;
  515. command = new OleDbCommand(sql, this._odcConnection);
  516. Reconnect(command);
  517. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  518. DataTable dt = new DataTable();
  519. try
  520. {
  521. ad.Fill(dt);
  522. }
  523. catch (Exception)
  524. {
  525. this._odcConnection = new OleDbConnection(this._connectionString);
  526. this._odcConnection.Open();
  527. command = new OleDbCommand(sql, this._odcConnection);
  528. ad = new OleDbDataAdapter();
  529. ad.SelectCommand = command;
  530. ad.Fill(dt);
  531. }
  532. ad.Dispose();
  533. command.Dispose();
  534. return int.Parse(dt.Rows[0][0].ToString()) > 0;
  535. }
  536. /// <summary>
  537. /// 直接执行SQL,同时传入SQL的类型
  538. /// </summary>
  539. /// <param name="SQL"></param>
  540. /// <param name="Type"></param>
  541. /// <returns></returns>
  542. public object ExecuteSql(string SQL, string Type, params object[] names)
  543. {
  544. object result = null;
  545. command = new OleDbCommand(SQL, this._odcConnection);
  546. Reconnect(command);
  547. //用来拼接参数的
  548. if (names.Length > 0)
  549. {
  550. string[] par = SQL.Split(':');
  551. //用来存参数的数组
  552. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  553. for (int i = 0; i < par.Length - 1; i++)
  554. {
  555. //新建一个char类型的数组用来存储每个字节的变量
  556. char[] c = par[i + 1].ToCharArray();
  557. addpar[i] = new StringBuilder();
  558. for (int j = 0; j < c.Length; j++)
  559. {
  560. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  561. {
  562. addpar[i].Append(c[j]);
  563. }
  564. else
  565. {
  566. break;
  567. }
  568. }
  569. }
  570. for (int i = 0; i < addpar.Length; i++)
  571. command.Parameters.Add(new OleDbParameter(addpar[i].ToString(), names[i]));
  572. }
  573. switch (Type.ToUpper())
  574. {
  575. case "SELECT":
  576. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  577. result = new DataTable();
  578. try
  579. {
  580. ad.Fill((DataTable)result);
  581. }
  582. catch (Exception)
  583. {
  584. this._odcConnection = new OleDbConnection(this._connectionString);
  585. this._odcConnection.Open();
  586. command = new OleDbCommand(SQL, this._odcConnection);
  587. ad = new OleDbDataAdapter();
  588. ad.SelectCommand = command;
  589. ad.Fill((DataTable)result);
  590. }
  591. break;
  592. case "DELETE":
  593. try
  594. {
  595. result = command.ExecuteNonQuery();
  596. }
  597. catch (Exception)
  598. {
  599. command.Connection = new OleDbConnection(this._connectionString);
  600. command.Connection.Open();
  601. result = command.ExecuteNonQuery();
  602. }
  603. break;
  604. case "UPDATE":
  605. try
  606. {
  607. result = command.ExecuteNonQuery();
  608. }
  609. catch (Exception)
  610. {
  611. command.Connection = new OleDbConnection(this._connectionString);
  612. command.Connection.Open();
  613. result = command.ExecuteNonQuery();
  614. }
  615. break;
  616. case "INSERT":
  617. try
  618. {
  619. result = command.ExecuteNonQuery();
  620. }
  621. catch (Exception)
  622. {
  623. command.Connection = new OleDbConnection(this._connectionString);
  624. command.Connection.Open();
  625. result = command.ExecuteNonQuery();
  626. }
  627. break;
  628. }
  629. command.Dispose();
  630. return result;
  631. }
  632. /// <summary>
  633. /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
  634. /// </summary>
  635. /// <param name="SQL"></param>
  636. public void CheckSQL(string SQL)
  637. {
  638. SQL = SQL.Replace("?", ":Param");
  639. command = new OleDbCommand(SQL, this._odcConnection);
  640. command.ExecuteNonQuery();
  641. command.Dispose();
  642. }
  643. public int GetDistinctRowCount(string TableName, string Field)
  644. {
  645. DataTable dt = new DataTable();
  646. string sql = "select distinct count('" + Field + "') from " + TableName;
  647. command = new OleDbCommand(sql, this._odcConnection);
  648. Reconnect(command);
  649. OleDbDataAdapter ad = new OleDbDataAdapter(command);
  650. try
  651. {
  652. ad.Fill(dt);
  653. }
  654. catch (Exception)
  655. {
  656. this._odcConnection = new OleDbConnection(this._connectionString);
  657. this._odcConnection.Open();
  658. command = new OleDbCommand(sql, this._odcConnection);
  659. ad = new OleDbDataAdapter();
  660. ad.SelectCommand = command;
  661. ad.Fill(dt);
  662. }
  663. ad.Dispose();
  664. command.Dispose();
  665. return int.Parse(dt.Rows[0][0].ToString());
  666. }
  667. /// <summary>
  668. /// 根据Caller获取流水号
  669. /// </summary>
  670. /// <param name="Caller"></param>
  671. /// <returns></returns>
  672. public string GetSerialNumberByCaller(string Caller)
  673. {
  674. string SerialNumber = getFieldDataByCondition("MaxNumbers", "mn_number", "mn_tablename='" + Caller + "'").ToString();
  675. UpdateByCondition("MaxNumbers", "mn_number=mn_number+1", "mn_tablename='" + Caller + "'");
  676. return SerialNumber;
  677. }
  678. /// <summary>
  679. /// 通过序列的名称获取序列
  680. /// </summary>
  681. /// <param name="SeqName"></param>
  682. /// <returns></returns>
  683. public string GetSEQ(string SeqName)
  684. {
  685. DataTable dt = new DataTable();
  686. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
  687. return dt.Rows[0][0].ToString();
  688. }
  689. /// <summary>
  690. /// 通过序列的名称获取序列
  691. /// </summary>
  692. /// <param name="SeqName"></param>
  693. /// <returns></returns>
  694. public string[] GetSEQ(string SeqName, int Num)
  695. {
  696. DataTable dt = new DataTable();
  697. dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".nextval FROM DUAL CONNECT BY LEVEL<=" + Num, "select");
  698. string[] SerialNum = new string[dt.Rows.Count];
  699. for (int i = 0; i < dt.Rows.Count; i++)
  700. {
  701. SerialNum[i] = dt.Rows[i][0].ToString();
  702. }
  703. return SerialNum;
  704. }
  705. /// <summary>
  706. /// 取Configs表中的配置,进行该客户是否执行某个操作
  707. /// </summary>
  708. /// <param name="Code"></param>
  709. /// <param name="Caller"></param>
  710. /// <returns></returns>
  711. public object GetConfig(string Code, string Caller)
  712. {
  713. DataTable dt = new DataTable();
  714. string sql = "select Data from configs where code='" + Code + "' and caller='" + Caller + "'";
  715. dt = (DataTable)ExecuteSql(sql, "select");
  716. if (dt.Rows.Count == 0)
  717. {
  718. return "";
  719. }
  720. else
  721. {
  722. return dt.Rows[0]["Data"];
  723. }
  724. }
  725. //将数据类型的列类型转换为DataTable
  726. public DataTable DataTypeColumnToDataTable(DataTable dt)
  727. {
  728. DataTable dt1 = new DataTable();
  729. dt1.Rows.Add();
  730. foreach (DataRow dr in dt.Rows)
  731. {
  732. dt1.Columns.Add(dr[0].ToString());
  733. int index = dt.Rows.IndexOf(dr);
  734. if (dr[1].ToString() == "NUMBER")
  735. {
  736. dt1.Rows[0][index] = 0;
  737. }
  738. if (dr[1].ToString() == "VARCHAR2")
  739. {
  740. dt1.Rows[0][index] = "这是一段文字";
  741. }
  742. if (dr[1].ToString() == "DATE")
  743. {
  744. dt1.Rows[0][index] = DateTime.Now.ToString("yyyy-MM-dd");
  745. }
  746. if (dr[1].ToString() == "FLOAT")
  747. {
  748. dt1.Rows[0][index] = 1.0;
  749. }
  750. if (dr[1].ToString() == "CLOB")
  751. {
  752. dt1.Rows[0][index] = "一段长文字";
  753. }
  754. }
  755. return dt1;
  756. }
  757. /// <summary>
  758. /// 通过条件更新
  759. /// </summary>
  760. /// <param name="TableName"></param>
  761. /// <param name="update"></param>
  762. /// <param name="condition"></param>
  763. public int UpdateByCondition(string TableName, string update, string condition)
  764. {
  765. string sql = "update " + TableName + " set " + update + " where " + condition;
  766. command = new OleDbCommand(sql, this._odcConnection);
  767. int Rowcount = 0;
  768. Reconnect(command);
  769. try
  770. {
  771. Rowcount = command.ExecuteNonQuery();
  772. }
  773. catch (Exception)
  774. {
  775. command.Connection = new OleDbConnection(this._connectionString);
  776. command.Connection.Open();
  777. Rowcount = command.ExecuteNonQuery();
  778. }
  779. command.Dispose();
  780. return Rowcount;
  781. }
  782. /// <summary>
  783. /// 调用存储过程
  784. /// </summary>
  785. /// <param name="ProcedureName"></param>
  786. /// <param name="param"></param>
  787. public void CallProcedure(string ProcedureName, ref string[] param)
  788. {
  789. command = new OleDbCommand(ProcedureName);
  790. command.Connection = this._odcConnection;
  791. Reconnect(command);
  792. command.CommandText = ProcedureName;
  793. command.CommandType = CommandType.StoredProcedure;
  794. for (int i = 0; i < param.Length; i++)
  795. command.Parameters.Add(new OleDbParameter(param[i].ToString(), OleDbType.VarChar, 200, param[i]));
  796. try
  797. {
  798. command.ExecuteNonQuery();
  799. }
  800. catch (Exception)
  801. {
  802. command.Connection = new OleDbConnection(this._connectionString);
  803. command.Connection.Open();
  804. command.ExecuteNonQuery();
  805. }
  806. for (int i = 0; i < command.Parameters.Count; i++)
  807. param[i] = command.Parameters[i].Value.ToString();
  808. command.Dispose();
  809. }
  810. /// <summary>
  811. /// 出现异常进行回滚的执行方法
  812. /// </summary>
  813. /// <param name="SQL"></param>
  814. public void ExecuteSQLTran(params string[] SQL)
  815. {
  816. OleDbTransaction tx = this._odcConnection.BeginTransaction();
  817. command = new OleDbCommand();
  818. command.Connection = this._odcConnection;
  819. command.Transaction = tx;
  820. try
  821. {
  822. foreach (string sql in SQL)
  823. {
  824. if (!String.IsNullOrEmpty(sql))
  825. {
  826. command.CommandText = sql;
  827. try
  828. {
  829. command.ExecuteNonQuery();
  830. }
  831. catch (Exception)
  832. {
  833. command.Connection = new OleDbConnection(this._connectionString);
  834. command.Connection.Open();
  835. command.ExecuteNonQuery();
  836. }
  837. }
  838. }
  839. tx.Commit();
  840. }
  841. catch (Exception E)
  842. {
  843. tx.Rollback();
  844. throw new Exception(E.Message);
  845. }
  846. command.Dispose();
  847. }
  848. /// <summary>
  849. /// 用于将string 的数组转换成SQL的查询内容
  850. /// </summary>
  851. /// <param name="Fields"></param>
  852. /// <returns></returns>
  853. private string AddField(string[] Fields)
  854. {
  855. string sql = " ";
  856. foreach (string field in Fields)
  857. {
  858. sql += field + ",";
  859. }
  860. return sql.Substring(0, sql.Length - 1);
  861. }
  862. /// <summary>
  863. /// 通过查询的内容获取到字段的描述
  864. /// </summary>
  865. /// <param name="field"></param>
  866. /// <returns></returns>
  867. private static string[] GetCaptionFromField(string field)
  868. {
  869. string[] caption = field.Split(',');
  870. for (int i = 0; i < caption.Length; i++)
  871. {
  872. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  873. }
  874. return caption;
  875. }
  876. /// <summary>
  877. /// 通过查询的语句获取查询的字段
  878. /// </summary>
  879. /// <param name="field"></param>
  880. /// <returns></returns>
  881. private static string[] GetField(string field)
  882. {
  883. string[] fields = field.Split(',');
  884. for (int i = 0; i < fields.Length; i++)
  885. {
  886. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  887. }
  888. return fields;
  889. }
  890. public object GetLabelParam(string sql)
  891. {
  892. DataTable dt = new DataTable();
  893. command = new OleDbCommand(sql, this._odcConnection);
  894. Reconnect(command);
  895. OleDbDataAdapter ad = new OleDbDataAdapter();
  896. ad.SelectCommand = command;
  897. try
  898. {
  899. ad.Fill(dt);
  900. }
  901. catch (Exception)
  902. {
  903. this._odcConnection = new OleDbConnection(this._connectionString);
  904. this._odcConnection.Open();
  905. command = new OleDbCommand(sql, this._odcConnection);
  906. ad = new OleDbDataAdapter();
  907. ad.SelectCommand = command;
  908. ad.Fill(dt);
  909. }
  910. if (dt.Rows.Count > 0)
  911. {
  912. ad.Dispose();
  913. command.Dispose();
  914. return dt.Rows[0][0];
  915. }
  916. else
  917. {
  918. command.Dispose();
  919. return "";
  920. }
  921. }
  922. public static string[] GetParamFromSQL(string SQL)
  923. {
  924. string[] par = SQL.Split(':');
  925. //用来存参数的数组
  926. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  927. string[] param = new string[par.Length - 1];
  928. for (int i = 0; i < par.Length - 1; i++)
  929. {
  930. //新建一个char类型的数组用来存储每个字节的变量
  931. char[] c = par[i + 1].ToCharArray();
  932. addpar[i] = new StringBuilder();
  933. for (int j = 0; j < c.Length; j++)
  934. {
  935. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  936. {
  937. addpar[i].Append(c[j]);
  938. }
  939. else
  940. {
  941. break;
  942. }
  943. }
  944. }
  945. for (int i = 0; i < par.Length - 1; i++)
  946. {
  947. param[i] = addpar[i].ToString();
  948. }
  949. return param;
  950. }
  951. public void AddColumFromDataTable(DataTable dt, string tablename)
  952. {
  953. DataTable dt2 = (DataTable)ExecuteSql("select top 1 * from " + tablename + "", "select");
  954. foreach (var item in dt.Columns)
  955. {
  956. if (!dt2.Columns.Contains(item.ToString()))
  957. {
  958. ExecuteSql("alter table " + tablename + " add [" + item.ToString() + "] varchar(200) NULL", "update");
  959. }
  960. }
  961. }
  962. }
  963. }