SqliteDBHelper.cs 32 KB

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