SqliteDBHelper.cs 33 KB

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