|
@@ -0,0 +1,935 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Data;
|
|
|
+using System.Data.SQLite;
|
|
|
+using System.IO;
|
|
|
+using System.Text;
|
|
|
+using System.Windows.Forms;
|
|
|
+
|
|
|
+namespace UAS_LabelMachine
|
|
|
+{
|
|
|
+ class SqliteDBHelper
|
|
|
+ {
|
|
|
+ private string _fileName;
|
|
|
+ private string _connectionString;
|
|
|
+ private SQLiteConnection _odcConnection = null;
|
|
|
+ private SQLiteCommand command = null;
|
|
|
+
|
|
|
+ public SqliteDBHelper(string fileName)
|
|
|
+ {
|
|
|
+ this._fileName = fileName;
|
|
|
+ this._connectionString = @"Data Source=" + fileName + ";";
|
|
|
+ Open();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Open()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+
|
|
|
+ this._odcConnection.Open();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ MessageBox.Show(ex.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Close()
|
|
|
+ {
|
|
|
+ this._odcConnection.Close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable GetDataTable(string sql)
|
|
|
+ {
|
|
|
+ DataTable ds = new DataTable();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ adapter.Fill(ds);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
|
|
|
+ }
|
|
|
+ adapter.Dispose();
|
|
|
+ return ds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public DataSet GetDataSet(string sql)
|
|
|
+ {
|
|
|
+ DataSet ds = new DataSet();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ adapter.Fill(ds);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
|
|
|
+ }
|
|
|
+ adapter.Dispose();
|
|
|
+ return ds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Reconnect(SQLiteCommand cmd)
|
|
|
+ {
|
|
|
+ if (cmd.Connection.State == ConnectionState.Closed)
|
|
|
+ {
|
|
|
+ cmd.Connection.Open();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable GetColumnDataType(string TableName)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ command = new SQLiteCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public object getFieldDataByCondition(string TableName, string Field, string Condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select " + Field + " from " + TableName + " where " + Condition;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ return dt.Rows[0][0];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
|
|
|
+ {
|
|
|
+ command = new SQLiteCommand(sql, _odcConnection);
|
|
|
+ SQLiteDataAdapter adapter = new SQLiteDataAdapter();
|
|
|
+ adapter.UpdateBatchSize = param1[0].Length;
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i < param.Length; i++)
|
|
|
+ {
|
|
|
+ command.Parameters.Add(new SQLiteParameter(param[i].ToString(), param1[i]));
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void DeleleFile(string accessFile)
|
|
|
+ {
|
|
|
+ var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string tempFile = Path.Combine(Path.GetDirectoryName(accessFile), Path.GetRandomFileName() + Path.GetExtension(accessFile));
|
|
|
+ dbe.CompactDatabase(accessFile, tempFile);
|
|
|
+ FileInfo temp = new FileInfo(tempFile);
|
|
|
+ temp.CopyTo(accessFile, true);
|
|
|
+ temp.Delete();
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Console.WriteLine("Error: " + e.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void SaveDataTable(DataTable dt, string TableName)
|
|
|
+ {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ sb.Clear();
|
|
|
+ sb.Append("insert into " + TableName + " (");
|
|
|
+ string field = "";
|
|
|
+ string valuefield = "";
|
|
|
+ for (int i = 0; i < dt.Columns.Count; i++)
|
|
|
+ {
|
|
|
+ if (i != dt.Columns.Count - 1)
|
|
|
+ {
|
|
|
+ field += dt.Columns[i].ColumnName + ",";
|
|
|
+ valuefield += "?,";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ field += dt.Columns[i].ColumnName + "";
|
|
|
+ valuefield += "?";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.Append(field + ") values(" + valuefield + ")");
|
|
|
+ SQLiteTransaction tx = this._odcConnection.BeginTransaction();
|
|
|
+ command = new SQLiteCommand();
|
|
|
+ command.Connection = this._odcConnection;
|
|
|
+ command.Transaction = tx;
|
|
|
+ string sql = sb.ToString();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
+ {
|
|
|
+ command.CommandText = sql;
|
|
|
+ for (int j = 0; j < dt.Columns.Count; j++)
|
|
|
+ {
|
|
|
+ command.Parameters.AddWithValue(dt.Columns[j].ColumnName, dt.Rows[i][j]);
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.Parameters.Clear();
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ MessageBox.Show(e.Message.ToString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tx.Commit();
|
|
|
+ }
|
|
|
+ catch (Exception E)
|
|
|
+ {
|
|
|
+ tx.Rollback();
|
|
|
+ throw new Exception(E.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void BatchInsert(string tableName, DataTable dt)
|
|
|
+ {
|
|
|
+ List<string> columnList = new List<string>();
|
|
|
+ foreach (DataColumn one in dt.Columns)
|
|
|
+ {
|
|
|
+ columnList.Add(one.ColumnName);
|
|
|
+ }
|
|
|
+ SQLiteDataAdapter adapter = new SQLiteDataAdapter();
|
|
|
+ adapter.SelectCommand = new SQLiteCommand("select pib_id from " + tableName, this._odcConnection);
|
|
|
+ using (SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter))
|
|
|
+ {
|
|
|
+ adapter.InsertCommand = builder.GetInsertCommand();
|
|
|
+ foreach (string one in columnList)
|
|
|
+ {
|
|
|
+ adapter.InsertCommand.Parameters.Add(new SQLiteParameter(one.ToLower(), "Test"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public int getRowCount(string TableName, string Condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public int getRowCount(string TableName)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select count(1) from " + TableName;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable getFieldsDataByCondition(string TableName, string[] Fields, string Condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select ";
|
|
|
+ sql += AddField(Fields);
|
|
|
+ sql += " from " + TableName + " where " + Condition + " and rownum=1";
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable getFieldsDatasByPageing(string TableName, string Fields, int CurrentPage, int PageSize, string Caller, params string[] condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ StringBuilder sql = new StringBuilder();
|
|
|
+
|
|
|
+
|
|
|
+ string[] caption = GetCaptionFromField(Fields);
|
|
|
+
|
|
|
+ string[] field = GetField(Fields);
|
|
|
+ sql.Append(" select * from (select RowNum RN, A.* from (select ");
|
|
|
+ sql.Append(AddField(caption));
|
|
|
+ if (condition.Length > 0)
|
|
|
+ {
|
|
|
+ if (condition[0] != null && condition[0].Trim() != "")
|
|
|
+ sql.Append(" from " + TableName + " where " + condition[0] + " ) A where ROWNUM <=" + CurrentPage * PageSize + ") where RN>" + (CurrentPage - 1) * PageSize);
|
|
|
+ else
|
|
|
+ sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
|
|
|
+ }
|
|
|
+ command = new SQLiteCommand(sql.ToString(), this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ dt.Columns.RemoveAt(0);
|
|
|
+ foreach (DataColumn dc in dt.Columns)
|
|
|
+ {
|
|
|
+ dc.ColumnName = field[dt.Columns.IndexOf(dc)];
|
|
|
+ dc.Caption = caption[dt.Columns.IndexOf(dc)];
|
|
|
+ }
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable getFieldsDatasByCondition(string TableName, string[] Fields, string Condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select ";
|
|
|
+ sql += AddField(Fields);
|
|
|
+ sql += " from " + TableName + " where " + Condition;
|
|
|
+
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable getFieldsDatas(string TableName, string Fields)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select ";
|
|
|
+ sql += Fields;
|
|
|
+ sql += " from " + TableName;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable GetDbFindDataTable(string field, string caller)
|
|
|
+ {
|
|
|
+ string sql = "select * from dbfindsetui where ds_caller='" + caller + "' and ds_whichui='" + field + "'";
|
|
|
+ DataTable dt = (DataTable)ExecuteSql(sql, "select");
|
|
|
+ if (dt.Rows.Count != 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ string[] dbfield = dt.Rows[0]["ds_findtoui"].ToString().Split('#');
|
|
|
+ string[] cnfield = dt.Rows[0]["ds_dbcaption"].ToString().Split('#');
|
|
|
+
|
|
|
+ string dbtable = dt.Rows[0]["ds_tables"].ToString();
|
|
|
+
|
|
|
+ for (int i = 0; i < dbfield.Length; i++)
|
|
|
+ {
|
|
|
+ dbfield[i] = dbfield[i].Split(',')[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ DataTable dt1 = new DataTable();
|
|
|
+
|
|
|
+ for (int i = 0; i < cnfield.Length; i++)
|
|
|
+ {
|
|
|
+ dt1.Columns.Add(cnfield[i]);
|
|
|
+ dt1.Columns[i].Caption = dbfield[i];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return dt1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public bool CheckExist(string TableName, string Condition)
|
|
|
+ {
|
|
|
+ string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString()) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public object ExecuteSql(string SQL, string Type, params object[] names)
|
|
|
+ {
|
|
|
+ object result = null;
|
|
|
+ command = new SQLiteCommand(SQL, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+
|
|
|
+ if (names.Length > 0)
|
|
|
+ {
|
|
|
+ string[] par = SQL.Split(':');
|
|
|
+
|
|
|
+ StringBuilder[] addpar = new StringBuilder[par.Length - 1];
|
|
|
+ for (int i = 0; i < par.Length - 1; i++)
|
|
|
+ {
|
|
|
+
|
|
|
+ char[] c = par[i + 1].ToCharArray();
|
|
|
+ addpar[i] = new StringBuilder();
|
|
|
+ for (int j = 0; j < c.Length; j++)
|
|
|
+ {
|
|
|
+ if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
|
|
|
+ {
|
|
|
+ addpar[i].Append(c[j]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < addpar.Length; i++)
|
|
|
+ command.Parameters.Add(new SQLiteParameter(addpar[i].ToString(), names[i]));
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (Type.ToUpper())
|
|
|
+ {
|
|
|
+ case "SELECT":
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ result = new DataTable();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill((DataTable)result);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(SQL, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill((DataTable)result);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "DELETE":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new SQLiteConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "UPDATE":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new SQLiteConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "INSERT":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new SQLiteConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void CheckSQL(string SQL)
|
|
|
+ {
|
|
|
+ SQL = SQL.Replace("?", ":Param");
|
|
|
+ command = new SQLiteCommand(SQL, this._odcConnection);
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public int GetDistinctRowCount(string TableName, string Field)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select distinct count('" + Field + "') from " + TableName;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public string GetSerialNumberByCaller(string Caller)
|
|
|
+ {
|
|
|
+ string SerialNumber = getFieldDataByCondition("MaxNumbers", "mn_number", "mn_tablename='" + Caller + "'").ToString();
|
|
|
+ UpdateByCondition("MaxNumbers", "mn_number=mn_number+1", "mn_tablename='" + Caller + "'");
|
|
|
+ return SerialNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public string GetSEQ(string SeqName)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
|
|
|
+ return dt.Rows[0][0].ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public string[] GetSEQ(string SeqName, int Num)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".nextval FROM DUAL CONNECT BY LEVEL<=" + Num, "select");
|
|
|
+ string[] SerialNum = new string[dt.Rows.Count];
|
|
|
+ for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
+ {
|
|
|
+ SerialNum[i] = dt.Rows[i][0].ToString();
|
|
|
+ }
|
|
|
+ return SerialNum;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public object GetConfig(string Code, string Caller)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select Data from configs where code='" + Code + "' and caller='" + Caller + "'";
|
|
|
+ dt = (DataTable)ExecuteSql(sql, "select");
|
|
|
+ if (dt.Rows.Count == 0)
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return dt.Rows[0]["Data"];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public DataTable DataTypeColumnToDataTable(DataTable dt)
|
|
|
+ {
|
|
|
+ DataTable dt1 = new DataTable();
|
|
|
+ dt1.Rows.Add();
|
|
|
+ foreach (DataRow dr in dt.Rows)
|
|
|
+ {
|
|
|
+ dt1.Columns.Add(dr[0].ToString());
|
|
|
+ int index = dt.Rows.IndexOf(dr);
|
|
|
+ if (dr[1].ToString() == "NUMBER")
|
|
|
+ {
|
|
|
+ dt1.Rows[0][index] = 0;
|
|
|
+ }
|
|
|
+ if (dr[1].ToString() == "VARCHAR2")
|
|
|
+ {
|
|
|
+ dt1.Rows[0][index] = "这是一段文字";
|
|
|
+ }
|
|
|
+ if (dr[1].ToString() == "DATE")
|
|
|
+ {
|
|
|
+ dt1.Rows[0][index] = DateTime.Now.ToString("yyyy-MM-dd");
|
|
|
+ }
|
|
|
+ if (dr[1].ToString() == "FLOAT")
|
|
|
+ {
|
|
|
+ dt1.Rows[0][index] = 1.0;
|
|
|
+ }
|
|
|
+ if (dr[1].ToString() == "CLOB")
|
|
|
+ {
|
|
|
+ dt1.Rows[0][index] = "一段长文字";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dt1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public int UpdateByCondition(string TableName, string update, string condition)
|
|
|
+ {
|
|
|
+ string sql = "update " + TableName + " set " + update + " where " + condition;
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ int Rowcount = 0;
|
|
|
+ Reconnect(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Rowcount = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new SQLiteConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ Rowcount = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ return Rowcount;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void ExecuteSQLTran(params string[] SQL)
|
|
|
+ {
|
|
|
+ SQLiteTransaction tx = this._odcConnection.BeginTransaction();
|
|
|
+ command = new SQLiteCommand();
|
|
|
+ command.Connection = this._odcConnection;
|
|
|
+ command.Transaction = tx;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ foreach (string sql in SQL)
|
|
|
+ {
|
|
|
+ if (!String.IsNullOrEmpty(sql))
|
|
|
+ {
|
|
|
+ command.CommandText = sql;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new SQLiteConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tx.Commit();
|
|
|
+ }
|
|
|
+ catch (Exception E)
|
|
|
+ {
|
|
|
+ tx.Rollback();
|
|
|
+ throw new Exception(E.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private string AddField(string[] Fields)
|
|
|
+ {
|
|
|
+ string sql = " ";
|
|
|
+ foreach (string field in Fields)
|
|
|
+ {
|
|
|
+ sql += field + ",";
|
|
|
+ }
|
|
|
+ return sql.Substring(0, sql.Length - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static string[] GetCaptionFromField(string field)
|
|
|
+ {
|
|
|
+ string[] caption = field.Split(',');
|
|
|
+ for (int i = 0; i < caption.Length; i++)
|
|
|
+ {
|
|
|
+ caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
|
|
|
+ }
|
|
|
+ return caption;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static string[] GetField(string field)
|
|
|
+ {
|
|
|
+ string[] fields = field.Split(',');
|
|
|
+ for (int i = 0; i < fields.Length; i++)
|
|
|
+ {
|
|
|
+ fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
|
|
|
+ }
|
|
|
+ return fields;
|
|
|
+ }
|
|
|
+
|
|
|
+ public object GetLabelParam(string sql)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ SQLiteDataAdapter ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new SQLiteConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new SQLiteCommand(sql, this._odcConnection);
|
|
|
+ ad = new SQLiteDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ ad.Dispose();
|
|
|
+
|
|
|
+ return dt.Rows[0][0];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string[] GetParamFromSQL(string SQL)
|
|
|
+ {
|
|
|
+ string[] par = SQL.Split(':');
|
|
|
+
|
|
|
+ StringBuilder[] addpar = new StringBuilder[par.Length - 1];
|
|
|
+ string[] param = new string[par.Length - 1];
|
|
|
+ for (int i = 0; i < par.Length - 1; i++)
|
|
|
+ {
|
|
|
+
|
|
|
+ char[] c = par[i + 1].ToCharArray();
|
|
|
+ addpar[i] = new StringBuilder();
|
|
|
+
|
|
|
+ for (int j = 0; j < c.Length; j++)
|
|
|
+ {
|
|
|
+ if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
|
|
|
+ {
|
|
|
+ addpar[i].Append(c[j]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < par.Length - 1; i++)
|
|
|
+ {
|
|
|
+ param[i] = addpar[i].ToString();
|
|
|
+ }
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void AddColumFromDataTable(DataTable dt, string tablename)
|
|
|
+ {
|
|
|
+ DataTable dt2 = (DataTable)ExecuteSql("select * from " + tablename + " limit 0,1", "select");
|
|
|
+ foreach (DataColumn item in dt.Columns)
|
|
|
+ {
|
|
|
+ if (!dt2.Columns.Contains(item.ToString()))
|
|
|
+ {
|
|
|
+ switch (item.DataType.ToString())
|
|
|
+ {
|
|
|
+ case "System.String":
|
|
|
+ ExecuteSql("alter table " + tablename + " add [" + item.ToString() + "] varchar(200) NULL", "update");
|
|
|
+ break;
|
|
|
+ case "System.Decimal":
|
|
|
+ ExecuteSql("alter table " + tablename + " add [" + item.ToString() + "] number NULL", "update");
|
|
|
+ break;
|
|
|
+ case "System.DateTime":
|
|
|
+ ExecuteSql("alter table " + tablename + " add [" + item.ToString() + "] date NULL", "update");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|