AccessDBHelper.cs 35 KB

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