|
|
@@ -4,6 +4,7 @@ using System.Data;
|
|
|
using System.Data.OleDb;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
+using System.Windows;
|
|
|
|
|
|
namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
@@ -11,12 +12,22 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
private string _fileName;
|
|
|
private string _connectionString;
|
|
|
- private OleDbConnection _odcConnection;
|
|
|
+ private OleDbConnection _odcConnection = null;
|
|
|
+ private OleDbCommand command = null;
|
|
|
|
|
|
public AccessDBHelper(string fileName)
|
|
|
{
|
|
|
this._fileName = fileName;
|
|
|
- this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
|
|
|
+ //this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
|
|
|
+ this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public AccessDBHelper(string fileName,string password)
|
|
|
+ {
|
|
|
+ this._fileName = fileName;
|
|
|
+ //this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database Password="+password+";";
|
|
|
+ this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Jet OLEDB:Database Password=" + password + ";";
|
|
|
}
|
|
|
|
|
|
public void Open()
|
|
|
@@ -28,9 +39,9 @@ namespace UAS_MES.DataOperate
|
|
|
// 打开连接
|
|
|
this._odcConnection.Open();
|
|
|
}
|
|
|
- catch (Exception)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- throw new Exception("嘗試打开 " + this._fileName + " 失敗, 請確認文件是否存在!");
|
|
|
+ MessageBox.Show(ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -39,19 +50,945 @@ namespace UAS_MES.DataOperate
|
|
|
this._odcConnection.Close();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public DataTable GetDataTable(string sql)
|
|
|
+ {
|
|
|
+ DataTable ds = new DataTable();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter adapter = new OleDbDataAdapter(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 OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter adapter = new OleDbDataAdapter(command);
|
|
|
try
|
|
|
{
|
|
|
- OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
|
|
|
adapter.Fill(ds);
|
|
|
}
|
|
|
- catch (Exception)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- throw new Exception("sql語句: " + sql + " 執行失敗!");
|
|
|
+ throw new Exception("sql語句: " + sql + " 執行失敗!"+ex.Message);
|
|
|
}
|
|
|
+ adapter.Dispose();
|
|
|
return ds;
|
|
|
}
|
|
|
+
|
|
|
+ public void Reconnect(OleDbCommand cmd)
|
|
|
+ {
|
|
|
+ if (cmd.Connection.State == ConnectionState.Closed)
|
|
|
+ {
|
|
|
+ cmd.Connection.Open();
|
|
|
+ //超时重连
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据表名获取该表字段数据类型
|
|
|
+ /// </summary>
|
|
|
+ public DataTable GetColumnDataType(string TableName)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ command = new OleDbCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取第一行第一列的信息
|
|
|
+ /// </summary>
|
|
|
+ public object getFieldDataByCondition(string TableName, string Field, string Condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select " + Field + " from " + TableName + " where " + Condition;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ return dt.Rows[0][0];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取指定表的记录的条数 ,带条件
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public int getRowCount(string TableName, string Condition)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取指定表的记录的条数 ,不带条件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="TableName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public int getRowCount(string TableName)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select count(1) from " + TableName;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过表名和获取单行的记录
|
|
|
+ /// </summary>
|
|
|
+ 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 OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 将DataTable导入到指定的表中
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="DataTable"></param>
|
|
|
+ /// <param name="TableName"></param>
|
|
|
+ public void InsertDataTable(DataTable DataTable, string TableName)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < DataTable.Rows.Count; i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < DataTable.Columns.Count; j++)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 按分页获取数据
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="TableName">表名</param>
|
|
|
+ /// <param name="Fields">查询字段</param>
|
|
|
+ /// <param name="CurrentPage">当前页面</param>
|
|
|
+ /// <param name="PageSize">页面展示条数</param>
|
|
|
+ /// <param name="Caller"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ // SELECT * FROM (SELECT A.* FROM (SELECT* FROM datalist) A WHERE ROWNUM <= 50) WHERE ROWNUM >= 21
|
|
|
+ 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 OleDbCommand(sql.ToString(), this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ command.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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过表名,字段和条件获取DataTable类型的数据
|
|
|
+ /// </summary>
|
|
|
+ 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 OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过表名,字段获取DataTable类型的数据
|
|
|
+ /// </summary>
|
|
|
+ public DataTable getFieldsDatas(string TableName, string Fields)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select ";
|
|
|
+ sql += Fields;
|
|
|
+ sql += " from " + TableName;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取DbFind的数据的DataTable的结构
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="field"></param>
|
|
|
+ /// <param name="caller"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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('#');
|
|
|
+ //获取查询要查询的Table
|
|
|
+ string dbtable = dt.Rows[0]["ds_tables"].ToString();
|
|
|
+ //拼接查询的字段
|
|
|
+ for (int i = 0; i < dbfield.Length; i++)
|
|
|
+ {
|
|
|
+ dbfield[i] = dbfield[i].Split(',')[0];
|
|
|
+ }
|
|
|
+ //新建一个空的DataTable
|
|
|
+ DataTable dt1 = new DataTable();
|
|
|
+ //往空的DataTable添加结构,ColumnName是中文,Caption是实际的字段名称
|
|
|
+ for (int i = 0; i < cnfield.Length; i++)
|
|
|
+ {
|
|
|
+ dt1.Columns.Add(cnfield[i]);
|
|
|
+ dt1.Columns[i].Caption = dbfield[i];
|
|
|
+ }
|
|
|
+ //返回一个带有结构的空的DataTable
|
|
|
+ //DbFind.BindTable1 = dbtable;
|
|
|
+ return dt1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检测内容是否存在
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="TableName"></param>
|
|
|
+ /// <param name="Condition"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public bool CheckExist(string TableName, string Condition)
|
|
|
+ {
|
|
|
+ string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString()) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 直接执行SQL,同时传入SQL的类型
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="SQL"></param>
|
|
|
+ /// <param name="Type"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public object ExecuteSql(string SQL, string Type, params object[] names)
|
|
|
+ {
|
|
|
+ object result = null;
|
|
|
+ command = new OleDbCommand(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类型的数组用来存储每个字节的变量
|
|
|
+ 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 OleDbParameter(addpar[i].ToString(),names[i]));
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (Type.ToUpper())
|
|
|
+ {
|
|
|
+ case "SELECT":
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ result = new DataTable();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill((DataTable)result);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(SQL, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill((DataTable)result);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "DELETE":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "UPDATE":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "INSERT":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ command.Dispose();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="SQL"></param>
|
|
|
+ public void CheckSQL(string SQL)
|
|
|
+ {
|
|
|
+ SQL = SQL.Replace("?", ":Param");
|
|
|
+ command = new OleDbCommand(SQL, this._odcConnection);
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ public int GetDistinctRowCount(string TableName, string Field)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ string sql = "select distinct count('" + Field + "') from " + TableName;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return int.Parse(dt.Rows[0][0].ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据Caller获取流水号
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Caller"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过序列的名称获取序列
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="SeqName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public string GetSEQ(string SeqName)
|
|
|
+ {
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
|
|
|
+ return dt.Rows[0][0].ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过序列的名称获取序列
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="SeqName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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 void SaveDataTable(DataTable dt, string TableName, string ID, params string[] sql)
|
|
|
+ {
|
|
|
+ if (dt == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ //预防插入的DataTable中存在不属于该表的列,在进行下一步操作之前全部剔除
|
|
|
+ DataTable data = (DataTable)ExecuteSql("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", "select");
|
|
|
+ //将所有的字段拼接起来
|
|
|
+ for (int i = 0; i < data.Rows.Count; i++)
|
|
|
+ {
|
|
|
+ sb.Append("#" + data.Rows[i]["Column_Name"].ToString());
|
|
|
+ }
|
|
|
+ //移除掉所有不属于该表的列
|
|
|
+ for (int i = dt.Columns.Count - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ if (!sb.ToString().Contains(dt.Columns[i].ColumnName.ToUpper()))
|
|
|
+ {
|
|
|
+ dt.Columns.RemoveAt(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.Clear();
|
|
|
+ //计算有多少个是新加的行,根据主键为空来进行判断
|
|
|
+ int NewRowCount = 0;
|
|
|
+ for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
+ {
|
|
|
+ if (dt.Rows[i][ID] == null || dt.Rows[i][ID].ToString() == "")
|
|
|
+ {
|
|
|
+ NewRowCount = NewRowCount + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (sql.Length > 0)
|
|
|
+ {
|
|
|
+ if (NewRowCount > 0)
|
|
|
+ {
|
|
|
+ //获取参数的个数
|
|
|
+ int paramsNum = sql[0].Split(':').Length - 1;
|
|
|
+ //解析参数的数据
|
|
|
+ string[] param = GetParamFromSQL(sql[0]);
|
|
|
+ //新建一个二维数组去
|
|
|
+ string[][] param_array = new string[paramsNum][];
|
|
|
+ //实例化每个一维数组
|
|
|
+ for (int i = 0; i < paramsNum; i++)
|
|
|
+ {
|
|
|
+ param_array[i] = new string[NewRowCount];
|
|
|
+ }
|
|
|
+ //设置每列参数的索引
|
|
|
+ int num = 0;
|
|
|
+ //变量所有的行,如果有主键为空的则移除,不为空的进行参数的拼接
|
|
|
+ for (int i = dt.Rows.Count - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ if (dt.Rows[i][ID] == null || dt.Rows[i][ID].ToString() == "")
|
|
|
+ {
|
|
|
+ //当为新添加行的时候才去设置参数,设置过后索引+1
|
|
|
+ for (int j = 0; j < paramsNum; j++)
|
|
|
+ {
|
|
|
+ param_array[j][num] = dt.Rows[i][param[j]].ToString();
|
|
|
+ }
|
|
|
+ dt.Rows.RemoveAt(i);
|
|
|
+ num++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ BatchInsertDataTable(sql[0], param, param_array);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.Clear();
|
|
|
+ sb.Append("update " + TableName + " set ");
|
|
|
+ int ColumnCount = dt.Columns.Count;
|
|
|
+ int RowCount = dt.Rows.Count;
|
|
|
+ //存数据的参数
|
|
|
+ List<string[]> Parameter = new List<string[]>();
|
|
|
+ //存参数名的参数
|
|
|
+ string[] ParName = new string[ColumnCount];
|
|
|
+ for (int i = 0; i < ColumnCount; i++)
|
|
|
+ {
|
|
|
+ ParName[i] = dt.Columns[i].ColumnName;
|
|
|
+ if (i == dt.Columns.Count - 1)
|
|
|
+ sb.Append(dt.Columns[i].ColumnName + "=:" + dt.Columns[i].ColumnName);
|
|
|
+ else
|
|
|
+ sb.Append(dt.Columns[i].ColumnName + "=:" + dt.Columns[i].ColumnName + ",");
|
|
|
+ }
|
|
|
+ sb.Append(" where " + ID + " =:" + ID);
|
|
|
+ //先添加参数
|
|
|
+ Parameter.Add(ParName);
|
|
|
+ //添加参数的具体内容
|
|
|
+ for (int i = 0; i < ColumnCount; i++)
|
|
|
+ {
|
|
|
+ string[] par = new string[RowCount];
|
|
|
+ for (int j = 0; j < RowCount; j++)
|
|
|
+ {
|
|
|
+ par[j] = dt.Rows[j][i].ToString();
|
|
|
+ }
|
|
|
+ Parameter.Add(par);
|
|
|
+ }
|
|
|
+ BatchInsert(sb.ToString(), Parameter.ToArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 批量通过SQL来执行插入操作 ,参数的第一个数一个string[]数组,用来传递需要添加的参数的名称
|
|
|
+ /// 之后的是名称参数数组对应的 ,所有的插入参数数据长度必须是一致的
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sql"></param>
|
|
|
+ /// <param name="names"></param>
|
|
|
+ public void BatchInsert(string sql, params object[][] names)
|
|
|
+ {
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
|
|
|
+ //将第一个数组的下标固定为0作为循环添加的参数的名称
|
|
|
+ for (int i = 1; i <= names[0].Length; i++)
|
|
|
+ {
|
|
|
+ command.Parameters.Add(new OleDbParameter(names[0][i - 1].ToString(),names[i]));
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ command.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
|
|
|
+ {
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
|
|
|
+ //将第一个数组的下标固定为0作为循环添加的参数的名称
|
|
|
+ for (int i = 0; i < param.Length; i++)
|
|
|
+ {
|
|
|
+ command.Parameters.Add(new OleDbParameter(param[i].ToString(),param1[i]));
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ command.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 取Configs表中的配置,进行该客户是否执行某个操作
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Code"></param>
|
|
|
+ /// <param name="Caller"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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"];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //将数据类型的列类型转换为DataTable
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过条件更新
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="TableName"></param>
|
|
|
+ /// <param name="update"></param>
|
|
|
+ /// <param name="condition"></param>
|
|
|
+ public string UpdateByCondition(string TableName, string update, string condition)
|
|
|
+ {
|
|
|
+ string sql = "update " + TableName + " set " + update + " where " + condition;
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+
|
|
|
+ Reconnect(command);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ command.Dispose();
|
|
|
+ return sql;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 调用存储过程
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="ProcedureName"></param>
|
|
|
+ /// <param name="param"></param>
|
|
|
+ public void CallProcedure(string ProcedureName, ref string[] param)
|
|
|
+ {
|
|
|
+ command = new OleDbCommand(ProcedureName);
|
|
|
+ command.Connection = this._odcConnection;
|
|
|
+ Reconnect(command);
|
|
|
+ command.CommandText = ProcedureName;
|
|
|
+ command.CommandType = CommandType.StoredProcedure;
|
|
|
+ for (int i = 0; i < param.Length; i++)
|
|
|
+ command.Parameters.Add(new OleDbParameter(param[i].ToString(), OleDbType.VarChar, 200, param[i]));
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ for (int i = 0; i < command.Parameters.Count; i++)
|
|
|
+ param[i] = command.Parameters[i].Value.ToString();
|
|
|
+ command.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 出现异常进行回滚的执行方法
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="SQL"></param>
|
|
|
+ public void ExecuteSQLTran(params string[] SQL)
|
|
|
+ {
|
|
|
+ OleDbTransaction tx = this._odcConnection.BeginTransaction();
|
|
|
+ command = new OleDbCommand();
|
|
|
+ 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 OleDbConnection(this._connectionString);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tx.Commit();
|
|
|
+ }
|
|
|
+ catch (Exception E)
|
|
|
+ {
|
|
|
+ tx.Rollback();
|
|
|
+ throw new Exception(E.Message);
|
|
|
+ }
|
|
|
+ command.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 用于将string 的数组转换成SQL的查询内容
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Fields"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private string AddField(string[] Fields)
|
|
|
+ {
|
|
|
+ string sql = " ";
|
|
|
+ foreach (string field in Fields)
|
|
|
+ {
|
|
|
+ sql += field + ",";
|
|
|
+ }
|
|
|
+ return sql.Substring(0, sql.Length - 1);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 通过查询的内容获取到字段的描述
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="field"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过查询的语句获取查询的字段
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="field"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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 OleDbCommand(sql, this._odcConnection);
|
|
|
+ Reconnect(command);
|
|
|
+ OleDbDataAdapter ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ this._odcConnection = new OleDbConnection(this._connectionString);
|
|
|
+ this._odcConnection.Open();
|
|
|
+ command = new OleDbCommand(sql, this._odcConnection);
|
|
|
+ ad = new OleDbDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ return dt.Rows[0][0];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ command.Dispose();
|
|
|
+ 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类型的数组用来存储每个字节的变量
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|
|
|
}
|