|
|
@@ -30,8 +30,8 @@ namespace UAS_MES.DataOperate
|
|
|
connection = new OracleConnection(ConnectionStrings);
|
|
|
else
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
- connection.Open();
|
|
|
command.Connection = connection;
|
|
|
+ command.Connection.Open();
|
|
|
command.CommandTimeout = 0;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
@@ -40,8 +40,15 @@ namespace UAS_MES.DataOperate
|
|
|
connection = new OracleConnection(ConnectionStrings);
|
|
|
else
|
|
|
connection = new OracleConnection(DBConnectionString);
|
|
|
- connection.Open();
|
|
|
- command.Connection = connection;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.Connection = connection;
|
|
|
+ command.Connection.Open();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
command.CommandTimeout = 0;
|
|
|
LogManager.DoLog(e.Message);
|
|
|
}
|
|
|
@@ -69,6 +76,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;
|
|
|
Reconnect(command);
|
|
|
@@ -178,6 +186,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;
|
|
|
Reconnect(command);
|
|
|
@@ -253,6 +262,7 @@ 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;
|
|
|
Reconnect(command);
|
|
|
@@ -284,6 +294,7 @@ namespace UAS_MES.DataOperate
|
|
|
string sql = "select ";
|
|
|
sql += Fields;
|
|
|
sql += " from " + TableName;
|
|
|
+ Console.WriteLine(sql);
|
|
|
command.CommandText = sql;
|
|
|
command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
@@ -306,128 +317,6 @@ namespace UAS_MES.DataOperate
|
|
|
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)
|
|
|
- {
|
|
|
- command.Parameters.Clear();
|
|
|
- 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.CommandText = sb.ToString();
|
|
|
- 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();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 检测内容是否存在
|
|
|
/// </summary>
|
|
|
@@ -486,6 +375,7 @@ 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":
|
|
|
@@ -611,12 +501,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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -768,12 +660,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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -792,12 +686,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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -891,6 +787,7 @@ namespace UAS_MES.DataOperate
|
|
|
command.Parameters.Clear();
|
|
|
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));
|
|
|
@@ -900,6 +797,7 @@ namespace UAS_MES.DataOperate
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
+ Console.WriteLine(command.Parameters.Count);
|
|
|
command.Connection = new OracleConnection(DBConnectionString);
|
|
|
command.Connection.Open();
|
|
|
command.ExecuteNonQuery();
|