SqliteDBHelper.cs 33 KB

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