SqliteDBHelper.cs 32 KB

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