AccessDBHelper.cs 36 KB

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