|
|
@@ -5,7 +5,6 @@ using System.Collections.Generic;
|
|
|
using System.Configuration;
|
|
|
using System.Data;
|
|
|
using System.Text;
|
|
|
-using System.Windows;
|
|
|
using UAS_MES.PublicMethod;
|
|
|
|
|
|
namespace UAS_MES.DataOperate
|
|
|
@@ -17,7 +16,8 @@ namespace UAS_MES.DataOperate
|
|
|
//用户选择的数据库的连接字符串
|
|
|
public static string DBConnectionString;
|
|
|
public static OracleConnection connection = null;
|
|
|
- OracleCommand command = new OracleCommand();
|
|
|
+ OracleCommand command = null;
|
|
|
+ int ReconnectTime = 0;
|
|
|
/// <summary>
|
|
|
/// 执行构造函数的时候打开数据库的链接
|
|
|
/// </summary>
|
|
|
@@ -30,28 +30,9 @@ namespace UAS_MES.DataOperate
|
|
|
connection = new OracleConnection(ConnectionStrings);
|
|
|
else
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
- command.Connection = connection;
|
|
|
- command.Connection.Open();
|
|
|
- command.CommandTimeout = 0;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- if (DBConnectionString == null || DBConnectionString == ConnectionStrings)
|
|
|
- connection = new OracleConnection(ConnectionStrings);
|
|
|
- else
|
|
|
- connection = new OracleConnection(DBConnectionString);
|
|
|
- try
|
|
|
- {
|
|
|
- command.Connection = connection;
|
|
|
- command.Connection.Open();
|
|
|
- }
|
|
|
- catch (Exception)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- command.CommandTimeout = 0;
|
|
|
- LogManager.DoLog(e.Message);
|
|
|
+ connection.Open();
|
|
|
}
|
|
|
+ catch (Exception e) { LogManager.DoLog(e.Message); }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -60,12 +41,12 @@ namespace UAS_MES.DataOperate
|
|
|
public DataTable GetColumnDataType(string TableName)
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
- command.CommandText = "select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')";
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return dt;
|
|
|
}
|
|
|
|
|
|
@@ -76,9 +57,7 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select " + Field + " from " + TableName + " where " + Condition;
|
|
|
- Console.WriteLine(sql);
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
@@ -90,13 +69,13 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
ad.Fill(dt);
|
|
|
}
|
|
|
ad.Dispose();
|
|
|
-
|
|
|
+ command.Dispose();
|
|
|
if (dt.Rows.Count > 0)
|
|
|
{
|
|
|
return dt.Rows[0][0];
|
|
|
@@ -115,7 +94,6 @@ namespace UAS_MES.DataOperate
|
|
|
/// <returns></returns>
|
|
|
public object ExecutePrintSQL(string SQL, params string[] Parameters)
|
|
|
{
|
|
|
- command.Parameters.Clear();
|
|
|
//按照?拆分数据,然后以:Param替换问号,同时添加参数
|
|
|
string[] Param = SQL.Split('?');
|
|
|
int ParamNum = Param.Length - 1;
|
|
|
@@ -127,8 +105,8 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
sb.Append(Param[i] + ":Param" + i);
|
|
|
}
|
|
|
- command.CommandText = sb.ToString();
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sb.ToString(), connection);
|
|
|
+
|
|
|
for (int i = 0; i < ParamNum; i++)
|
|
|
{
|
|
|
command.Parameters.Add("Param" + i, OracleDbType.Varchar2, Parameters[i], ParameterDirection.Input);
|
|
|
@@ -137,6 +115,7 @@ namespace UAS_MES.DataOperate
|
|
|
DataTable dt = new DataTable();
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return dt;
|
|
|
}
|
|
|
return "参数错误,请检查SQL语句";
|
|
|
@@ -150,12 +129,12 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString());
|
|
|
}
|
|
|
|
|
|
@@ -168,12 +147,13 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select count(1) from " + TableName;
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
+
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString());
|
|
|
}
|
|
|
|
|
|
@@ -186,9 +166,7 @@ namespace UAS_MES.DataOperate
|
|
|
string sql = "select ";
|
|
|
sql += AddField(Fields);
|
|
|
sql += " from " + TableName + " where " + Condition + " and rownum=1";
|
|
|
- Console.WriteLine(sql);
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
try
|
|
|
@@ -199,16 +177,32 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
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>
|
|
|
@@ -237,13 +231,12 @@ namespace UAS_MES.DataOperate
|
|
|
else
|
|
|
sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
|
|
|
}
|
|
|
- command.CommandText = sql.ToString();
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql.ToString(), connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
-
|
|
|
+ command.Dispose();
|
|
|
dt.Columns.RemoveAt(0);
|
|
|
foreach (DataColumn dc in dt.Columns)
|
|
|
{
|
|
|
@@ -262,9 +255,8 @@ namespace UAS_MES.DataOperate
|
|
|
string sql = "select ";
|
|
|
sql += AddField(Fields);
|
|
|
sql += " from " + TableName + " where " + Condition;
|
|
|
- Console.WriteLine(sql);
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
try
|
|
|
@@ -275,13 +267,13 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
ad.Fill(dt);
|
|
|
}
|
|
|
ad.Dispose();
|
|
|
-
|
|
|
+ command.Dispose();
|
|
|
return dt;
|
|
|
}
|
|
|
|
|
|
@@ -294,9 +286,7 @@ namespace UAS_MES.DataOperate
|
|
|
string sql = "select ";
|
|
|
sql += Fields;
|
|
|
sql += " from " + TableName;
|
|
|
- Console.WriteLine(sql);
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.SelectCommand = command;
|
|
|
@@ -308,15 +298,177 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
ad.Fill(dt);
|
|
|
}
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return dt;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 根据DataTable和指定的表名更新数据,如果需要保存新增的数据则需要传递一条Insert的SQL
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="DataTable"></param>
|
|
|
+ /// <param name="TableName"></param>
|
|
|
+ /// <param name="Condition"></param>
|
|
|
+ public void UpDateTableByCondition(DataTable DataTable, string TableName, string PrimaryKey, params string[] sql)
|
|
|
+ {
|
|
|
+ if (DataTable == 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 = DataTable.Columns.Count - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ if (!sb.ToString().Contains(DataTable.Columns[i].ColumnName.ToUpper()))
|
|
|
+ {
|
|
|
+ DataTable.Columns.RemoveAt(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.Clear();
|
|
|
+ //计算有多少个是新加的行,根据主键为空来进行判断
|
|
|
+ int NewRowCount = 0;
|
|
|
+ for (int i = 0; i < DataTable.Rows.Count; i++)
|
|
|
+ {
|
|
|
+ if (DataTable.Rows[i][PrimaryKey] == null || DataTable.Rows[i][PrimaryKey].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 = DataTable.Rows.Count - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ if (DataTable.Rows[i][PrimaryKey] == null || DataTable.Rows[i][PrimaryKey].ToString() == "")
|
|
|
+ {
|
|
|
+ //当为新添加行的时候才去设置参数,设置过后索引+1
|
|
|
+ for (int j = 0; j < paramsNum; j++)
|
|
|
+ {
|
|
|
+ param_array[j][num] = DataTable.Rows[i][param[j]].ToString();
|
|
|
+ }
|
|
|
+ DataTable.Rows.RemoveAt(i);
|
|
|
+ num++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ BatchInsertDataTable(sql[0], param, param_array);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //不是新增行的启用更新的方法
|
|
|
+ sb.Append("update " + TableName + " set ");
|
|
|
+ //拼接语句,特殊处理日期
|
|
|
+
|
|
|
+ foreach (DataColumn dc in DataTable.Columns)
|
|
|
+ {
|
|
|
+ if (!dc.DataType.ToString().Equals("System.DateTime"))
|
|
|
+ {
|
|
|
+ sb.Append(dc.Caption + "=:" + dc.Caption + ",");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sb.Append(dc.Caption + "=:" + dc.Caption + ",");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.Remove(sb.Length - 1, 1);
|
|
|
+ sb.Append(" where " + PrimaryKey + "=:" + PrimaryKey);
|
|
|
+ command = new OracleCommand(sb.ToString(), connection);
|
|
|
+ OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
+ // 参数的长度是DataTable的行数决定的
|
|
|
+ command.ArrayBindCount = DataTable.Rows.Count;
|
|
|
+ //默认全部是Varchar2类型的
|
|
|
+ OracleDbType ob = OracleDbType.Varchar2;
|
|
|
+ for (int i = 0; i < DataTable.Columns.Count; i++)
|
|
|
+ {
|
|
|
+ object[] param = new object[DataTable.Rows.Count];
|
|
|
+ for (int j = 0; j < DataTable.Rows.Count; j++)
|
|
|
+ {
|
|
|
+ DateTime dt = DateTime.Now;
|
|
|
+ if (DateTime.TryParse(DataTable.Rows[j][i].ToString(), out dt))
|
|
|
+ {
|
|
|
+ param[j] = dt;
|
|
|
+ ob = OracleDbType.Date;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ob = OracleDbType.Varchar2;
|
|
|
+ param[j] = DataTable.Rows[j][i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //添加批量更新的参数
|
|
|
+ command.Parameters.Add(new OracleParameter(DataTable.Columns[i].Caption, ob, param, ParameterDirection.Input));
|
|
|
+ }
|
|
|
+ ad.UpdateCommand = command;
|
|
|
+ ad.Update(DataTable);
|
|
|
+ ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <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>
|
|
|
@@ -326,13 +478,13 @@ namespace UAS_MES.DataOperate
|
|
|
public bool CheckExist(string TableName, string Condition)
|
|
|
{
|
|
|
string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
DataTable dt = new DataTable();
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString()) > 0;
|
|
|
}
|
|
|
|
|
|
@@ -344,10 +496,8 @@ namespace UAS_MES.DataOperate
|
|
|
/// <returns></returns>
|
|
|
public object ExecuteSql(string SQL, string Type, params object[] names)
|
|
|
{
|
|
|
- command.Parameters.Clear();
|
|
|
object result = null;
|
|
|
- command.CommandText = SQL;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(SQL, connection);
|
|
|
Reconnect(command);
|
|
|
//用来拼接参数的
|
|
|
if (names.Length > 0)
|
|
|
@@ -375,7 +525,6 @@ namespace UAS_MES.DataOperate
|
|
|
for (int i = 0; i < addpar.Length; i++)
|
|
|
command.Parameters.Add(new OracleParameter(addpar[i].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
|
|
|
}
|
|
|
- Console.WriteLine(SQL);
|
|
|
switch (Type.ToUpper())
|
|
|
{
|
|
|
case "SELECT":
|
|
|
@@ -389,7 +538,7 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(SQL, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
ad.Fill((DataTable)result);
|
|
|
@@ -432,6 +581,7 @@ namespace UAS_MES.DataOperate
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
+ command.Dispose();
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -442,17 +592,16 @@ namespace UAS_MES.DataOperate
|
|
|
public void CheckSQL(string SQL)
|
|
|
{
|
|
|
SQL = SQL.Replace("?", ":Param");
|
|
|
- command.CommandText = SQL;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(SQL, connection);
|
|
|
command.ExecuteNonQuery();
|
|
|
+ command.Dispose();
|
|
|
}
|
|
|
|
|
|
public int GetDistinctRowCount(string TableName, string Field)
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select distinct count('" + Field + "') from " + TableName;
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
try
|
|
|
@@ -463,12 +612,13 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
ad.Fill(dt);
|
|
|
}
|
|
|
ad.Dispose();
|
|
|
+ command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString());
|
|
|
}
|
|
|
|
|
|
@@ -492,25 +642,23 @@ namespace UAS_MES.DataOperate
|
|
|
/// <param name="DeleteID">需要删除主键ID的数组</param>
|
|
|
public void DeleteDataByID(string TableName, string ID, string[] DeleteID)
|
|
|
{
|
|
|
- command.Parameters.Clear();
|
|
|
string sql = "delete from " + TableName + " where " + ID + " =:DeleteID";
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
command.ArrayBindCount = DeleteID.Length;
|
|
|
command.Parameters.Add(new OracleParameter("DeleteID", OracleDbType.Long, DeleteID, ParameterDirection.Input));
|
|
|
try
|
|
|
{
|
|
|
command.ExecuteNonQuery();
|
|
|
- command.ArrayBindCount = 0;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
command.Connection = new OracleConnection(DBConnectionString);
|
|
|
command.Connection.Open();
|
|
|
command.ExecuteNonQuery();
|
|
|
- command.ArrayBindCount = 0;
|
|
|
}
|
|
|
+ command.Dispose();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -648,10 +796,10 @@ namespace UAS_MES.DataOperate
|
|
|
/// <param name="names"></param>
|
|
|
public void BatchInsert(string sql, params object[][] names)
|
|
|
{
|
|
|
- command.Parameters.Clear();
|
|
|
- command.CommandText = sql;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
command.ArrayBindCount = names[1].Length;
|
|
|
+
|
|
|
//因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
|
|
|
//将第一个数组的下标固定为0作为循环添加的参数的名称
|
|
|
for (int i = 1; i <= names[0].Length; i++)
|
|
|
@@ -661,21 +809,19 @@ namespace UAS_MES.DataOperate
|
|
|
try
|
|
|
{
|
|
|
command.ExecuteNonQuery();
|
|
|
- command.ArrayBindCount = 0;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
command.Connection = new OracleConnection(DBConnectionString);
|
|
|
command.Connection.Open();
|
|
|
command.ExecuteNonQuery();
|
|
|
- command.ArrayBindCount = 0;
|
|
|
}
|
|
|
+ command.Dispose();
|
|
|
}
|
|
|
|
|
|
public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
|
|
|
{
|
|
|
- command.Parameters.Clear();
|
|
|
- command.CommandText = sql;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
command.ArrayBindCount = param1[0].Length;
|
|
|
//因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
|
|
|
@@ -687,15 +833,14 @@ namespace UAS_MES.DataOperate
|
|
|
try
|
|
|
{
|
|
|
command.ExecuteNonQuery();
|
|
|
- command.ArrayBindCount = 0;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
command.Connection = new OracleConnection(DBConnectionString);
|
|
|
command.Connection.Open();
|
|
|
command.ExecuteNonQuery();
|
|
|
- command.ArrayBindCount = 0;
|
|
|
}
|
|
|
+ command.Dispose();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -753,6 +898,7 @@ namespace UAS_MES.DataOperate
|
|
|
return dt1;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 通过条件更新
|
|
|
/// </summary>
|
|
|
@@ -762,8 +908,7 @@ namespace UAS_MES.DataOperate
|
|
|
public string UpdateByCondition(string TableName, string update, string condition)
|
|
|
{
|
|
|
string sql = "update " + TableName + " set " + update + " where " + condition;
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
try
|
|
|
{
|
|
|
@@ -775,6 +920,7 @@ namespace UAS_MES.DataOperate
|
|
|
command.Connection.Open();
|
|
|
command.ExecuteNonQuery();
|
|
|
}
|
|
|
+ command.Dispose();
|
|
|
return sql;
|
|
|
}
|
|
|
|
|
|
@@ -785,11 +931,11 @@ namespace UAS_MES.DataOperate
|
|
|
/// <param name="param"></param>
|
|
|
public void CallProcedure(string ProcedureName, ref string[] param)
|
|
|
{
|
|
|
- command.Parameters.Clear();
|
|
|
+ command = new OracleCommand(ProcedureName);
|
|
|
+ command.Connection = connection;
|
|
|
+ Reconnect(command);
|
|
|
command.CommandText = ProcedureName;
|
|
|
command.CommandType = CommandType.StoredProcedure;
|
|
|
- command.ArrayBindCount = 0;
|
|
|
- Reconnect(command);
|
|
|
for (int i = 0; i < param.Length; i++)
|
|
|
command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, 200, param[i], ParameterDirection.InputOutput));
|
|
|
try
|
|
|
@@ -798,14 +944,13 @@ namespace UAS_MES.DataOperate
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
- Console.WriteLine(command.Parameters.Count);
|
|
|
command.Connection = new OracleConnection(DBConnectionString);
|
|
|
command.Connection.Open();
|
|
|
command.ExecuteNonQuery();
|
|
|
}
|
|
|
for (int i = 0; i < command.Parameters.Count; i++)
|
|
|
param[i] = command.Parameters[i].Value.ToString();
|
|
|
-
|
|
|
+ command.Dispose();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -815,6 +960,8 @@ namespace UAS_MES.DataOperate
|
|
|
public void ExecuteSQLTran(params string[] SQL)
|
|
|
{
|
|
|
OracleTransaction tx = connection.BeginTransaction();
|
|
|
+ command = new OracleCommand();
|
|
|
+ command.Connection = connection;
|
|
|
command.Transaction = tx;
|
|
|
try
|
|
|
{
|
|
|
@@ -823,7 +970,6 @@ namespace UAS_MES.DataOperate
|
|
|
if (!String.IsNullOrEmpty(sql))
|
|
|
{
|
|
|
command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
try
|
|
|
{
|
|
|
command.ExecuteNonQuery();
|
|
|
@@ -843,6 +989,7 @@ namespace UAS_MES.DataOperate
|
|
|
tx.Rollback();
|
|
|
throw new Exception(E.Message);
|
|
|
}
|
|
|
+ command.Dispose();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -892,8 +1039,7 @@ namespace UAS_MES.DataOperate
|
|
|
public object GetLabelParam(string sql)
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
- command.CommandText = sql;
|
|
|
- command.CommandType = CommandType.Text;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
@@ -905,7 +1051,7 @@ namespace UAS_MES.DataOperate
|
|
|
{
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ command = new OracleCommand(sql, connection);
|
|
|
ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
ad.Fill(dt);
|
|
|
@@ -913,11 +1059,12 @@ namespace UAS_MES.DataOperate
|
|
|
if (dt.Rows.Count > 0)
|
|
|
{
|
|
|
ad.Dispose();
|
|
|
-
|
|
|
+ command.Dispose();
|
|
|
return dt.Rows[0][0];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ command.Dispose();
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
@@ -955,10 +1102,7 @@ namespace UAS_MES.DataOperate
|
|
|
|
|
|
public void Dispose()
|
|
|
{
|
|
|
- if (command != null)
|
|
|
- command.Dispose();
|
|
|
- if (connection != null)
|
|
|
- connection.Dispose();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private void Reconnect(OracleCommand cmd)
|