|
|
@@ -16,7 +16,7 @@ namespace UAS_MesInterface
|
|
|
//用户选择的数据库的连接字符串
|
|
|
public static OracleConnection connection = null;
|
|
|
//用户选择的数据库的连接字符串
|
|
|
- OracleCommand command = null;
|
|
|
+ OracleCommand command = new OracleCommand();
|
|
|
/// <summary>
|
|
|
/// 执行构造函数的时候打开数据库的链接
|
|
|
/// </summary>
|
|
|
@@ -24,21 +24,55 @@ namespace UAS_MesInterface
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (Environment.ToUpper() == "FORMAl")
|
|
|
+ //如果选择的是默认数据则直接用配置文件的信息连接,否则选择数据库的账套信息
|
|
|
+ if (Environment.ToUpper() == "MES")
|
|
|
{
|
|
|
- ConnectionStrings = "Password=select!#%*(;User ID=MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=117.25.180.218)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
|
|
|
+ ConnectionStrings = "Password=select!#%*(;User ID=MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.230.200)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
|
|
|
}
|
|
|
- else if (Environment.ToUpper() == "TEST")
|
|
|
+ else if (Environment.ToUpper() == "MES_TEST")
|
|
|
{
|
|
|
- ConnectionStrings = "Password=select!#%*(;User ID=MES_TEST;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=117.25.180.218)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
|
|
|
+ ConnectionStrings = "Password=select!#%*(;User ID=MES_TEST;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.230.200)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
|
|
|
}
|
|
|
- connection = new OracleConnection(ConnectionStrings);
|
|
|
- connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ command.Connection.Open();
|
|
|
+ command.CommandTimeout = 0;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
+ if (Environment.ToUpper() == "MES")
|
|
|
+ {
|
|
|
+ ConnectionStrings = "Password=select!#%*(;User ID=MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.230.200)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
|
|
|
+ }
|
|
|
+ else if (Environment.ToUpper() == "MES_TEST")
|
|
|
+ {
|
|
|
+ ConnectionStrings = "Password=select!#%*(;User ID=MES_TEST;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.230.200)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.Connection = connection;
|
|
|
+ command.Connection.Open();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ command.CommandTimeout = 0;
|
|
|
}
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 根据表名获取该表字段数据类型
|
|
|
+ /// </summary>
|
|
|
+ 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;
|
|
|
+ Reconnect(command);
|
|
|
+ OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取第一行第一列的信息
|
|
|
@@ -47,13 +81,26 @@ namespace UAS_MesInterface
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select " + Field + " from " + TableName + " where " + Condition;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
- ad.Fill(dt);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
+
|
|
|
if (dt.Rows.Count > 0)
|
|
|
{
|
|
|
return dt.Rows[0][0];
|
|
|
@@ -64,6 +111,41 @@ namespace UAS_MesInterface
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 执行打印的SQL
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="SQL">SQL语句</param>
|
|
|
+ /// <param name="Parameters">动态添加的参数,主要根据条码枪扫描获取</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public object ExecutePrintSQL(string SQL, params string[] Parameters)
|
|
|
+ {
|
|
|
+ command.Parameters.Clear();
|
|
|
+ //按照?拆分数据,然后以:Param替换问号,同时添加参数
|
|
|
+ string[] Param = SQL.Split('?');
|
|
|
+ int ParamNum = Param.Length - 1;
|
|
|
+ //条码打印必然存在需要维护的参数
|
|
|
+ if (ParamNum > 0)
|
|
|
+ {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < ParamNum; i++)
|
|
|
+ {
|
|
|
+ sb.Append(Param[i] + ":Param" + i);
|
|
|
+ }
|
|
|
+ command.CommandText = sb.ToString();
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
+ for (int i = 0; i < ParamNum; i++)
|
|
|
+ {
|
|
|
+ command.Parameters.Add("Param" + i, OracleDbType.Varchar2, Parameters[i], ParameterDirection.Input);
|
|
|
+ }
|
|
|
+ OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
+ DataTable dt = new DataTable();
|
|
|
+ ad.Fill(dt);
|
|
|
+ ad.Dispose();
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+ return "参数错误,请检查SQL语句";
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取指定表的记录的条数 ,带条件
|
|
|
/// </summary>
|
|
|
@@ -72,12 +154,12 @@ namespace UAS_MesInterface
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString());
|
|
|
}
|
|
|
|
|
|
@@ -90,12 +172,12 @@ namespace UAS_MesInterface
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
string sql = "select count(1) from " + TableName;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString());
|
|
|
}
|
|
|
|
|
|
@@ -108,12 +190,25 @@ namespace UAS_MesInterface
|
|
|
string sql = "select ";
|
|
|
sql += AddField(Fields);
|
|
|
sql += " from " + TableName + " where " + Condition + " and rownum=1";
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill(dt);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
+
|
|
|
return dt;
|
|
|
}
|
|
|
|
|
|
@@ -145,12 +240,13 @@ namespace UAS_MesInterface
|
|
|
else
|
|
|
sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
|
|
|
}
|
|
|
- command = new OracleCommand(sql.ToString(), connection);
|
|
|
+ command.CommandText = sql.ToString();
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.Fill(dt);
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
+
|
|
|
dt.Columns.RemoveAt(0);
|
|
|
foreach (DataColumn dc in dt.Columns)
|
|
|
{
|
|
|
@@ -169,12 +265,25 @@ namespace UAS_MesInterface
|
|
|
string sql = "select ";
|
|
|
sql += AddField(Fields);
|
|
|
sql += " from " + TableName + " where " + Condition;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill(dt);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
+
|
|
|
return dt;
|
|
|
}
|
|
|
|
|
|
@@ -187,301 +296,26 @@ namespace UAS_MesInterface
|
|
|
string sql = "select ";
|
|
|
sql += Fields;
|
|
|
sql += " from " + TableName;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
ad.SelectCommand = command;
|
|
|
- ad.Fill(dt);
|
|
|
- foreach (DataColumn dc in dt.Columns)
|
|
|
- {
|
|
|
- dc.Caption = "测试测试";
|
|
|
- }
|
|
|
- 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>
|
|
|
- /// 获取配置列表中的数据,支持DaatList,Form,DetailGrid
|
|
|
- /// </summary>
|
|
|
- /// <param name="Caller"></param>
|
|
|
- /// <param name="Type"></param>
|
|
|
- /// <param name="condition"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public DataTable GetConfigureData(string Caller, string Type, string condition)
|
|
|
- {
|
|
|
- DataTable dt = new DataTable();
|
|
|
- //用于拼接SQL语句
|
|
|
- StringBuilder Sql = new StringBuilder();
|
|
|
- //用于设置不同Type时设置对应表的字段
|
|
|
- string getField = "";
|
|
|
- string getCaption = "";
|
|
|
- string getTable = "";
|
|
|
- switch (Type.ToUpper())
|
|
|
- {
|
|
|
- case "DATALIST":
|
|
|
- getField = "dld_field"; getCaption = "dld_caption"; getTable = "dld_table";
|
|
|
- Sql.Append("select * from datalistdetail where dld_caller='" + Caller + "'");
|
|
|
- break;
|
|
|
- case "FORM":
|
|
|
- getField = "fd_field"; getCaption = "fd_caption"; getTable = "fd_table";
|
|
|
- Sql.Append("select * from formdetail where fd_foid=( select fo_id from form where fo_caller='" + Caller + "')");
|
|
|
- break;
|
|
|
- case "DETAILGRID":
|
|
|
- getField = "dg_field"; getCaption = "dg_caption"; getTable = "dg_table";
|
|
|
- Sql.Append("select * from detailgrid where dg_caller='" + Caller + "'");
|
|
|
- break;
|
|
|
- }
|
|
|
- command = new OracleCommand(Sql.ToString(), connection);
|
|
|
- OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill(dt);
|
|
|
- //清除掉之前的内容重新拼接
|
|
|
- Sql.Clear();
|
|
|
- Sql.Append("select ");
|
|
|
- string[] field = new string[dt.Rows.Count];
|
|
|
- string[] caption = new string[dt.Rows.Count];
|
|
|
- DataTable dt1 = new DataTable();
|
|
|
- //记录描述和字段名称
|
|
|
- foreach (DataRow dr in dt.Rows)
|
|
|
- {
|
|
|
- field[dt.Rows.IndexOf(dr)] = dr[getCaption].ToString();
|
|
|
- caption[dt.Rows.IndexOf(dr)] = dr[getField].ToString();
|
|
|
- Sql.Append(dr[getField] + ",");
|
|
|
- }
|
|
|
- //调用substring是为了去除之前拼接多出来的一个逗号
|
|
|
- string sql = Sql.Remove(Sql.Length - 1, 1).ToString() + " from " + dt.Rows[0][getTable] + " where " + condition;
|
|
|
- //调用一个新的构造DataTable用来存放返回的数据
|
|
|
- dt1 = (DataTable)ExecuteSql(sql, "select");
|
|
|
- //给DataTable加上列名和描述,列名是中文字段,描述是数据库实际的字段名称
|
|
|
- for (int i = 0; i < field.Length; i++)
|
|
|
- {
|
|
|
- dt1.Columns[i].ColumnName = field[i];
|
|
|
- dt1.Columns[i].Caption = caption[i];
|
|
|
- }
|
|
|
- //返回的第一条数据是SQL,后面的是实际的列名
|
|
|
- ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
- return dt1;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 查询配置的字段,Type是查询DataList,Form还是DetailGrid
|
|
|
- /// </summary>
|
|
|
- /// <param name="Caller"></param>
|
|
|
- /// <param name="Type"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public DataTable GetConfigureData(string Caller, string Type)
|
|
|
- {
|
|
|
- DataTable dt = new DataTable();
|
|
|
- //用于拼接SQL语句
|
|
|
- StringBuilder Sql = new StringBuilder();
|
|
|
- //用于设置不同Type时设置对应表的字段
|
|
|
- string getField = "";
|
|
|
- string getCaption = "";
|
|
|
- string getTable = "";
|
|
|
- switch (Type.ToUpper())
|
|
|
- {
|
|
|
- case "DATALIST":
|
|
|
- getField = "dld_field"; getCaption = "dld_caption"; getTable = "dld_table";
|
|
|
- Sql.Append("select * from datalistdetail where dld_caller='" + Caller + "'");
|
|
|
- break;
|
|
|
- case "FORM":
|
|
|
- getField = "fd_field"; getCaption = "fd_caption"; getTable = "fd_table";
|
|
|
- Sql.Append("select * from formdetail where fd_foid=( select fo_id from form where fo_caller='" + Caller + "')");
|
|
|
- break;
|
|
|
- case "DETAILGRID":
|
|
|
- getField = "dg_field"; getCaption = "dg_caption"; getTable = "dg_table";
|
|
|
- Sql.Append("select * from detailgrid where dg_caller='" + Caller + "'");
|
|
|
- break;
|
|
|
- }
|
|
|
- command = new OracleCommand(Sql.ToString(), connection);
|
|
|
- OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill(dt);
|
|
|
- //清除掉之前的内容重新拼接
|
|
|
- Sql.Clear();
|
|
|
- Sql.Append("select ");
|
|
|
- //用于记录实际的列名,+1的目的是为了存放SQL
|
|
|
- string[] field = new string[dt.Rows.Count];
|
|
|
- string[] caption = new string[dt.Rows.Count];
|
|
|
- DataTable dt1 = new DataTable();
|
|
|
- foreach (DataRow dr in dt.Rows)
|
|
|
+ try
|
|
|
{
|
|
|
- field[dt.Rows.IndexOf(dr)] = dr[getCaption].ToString();
|
|
|
- caption[dt.Rows.IndexOf(dr)] = dr[getField].ToString();
|
|
|
- Sql.Append(dr[getField] + ",");
|
|
|
+ ad.Fill(dt);
|
|
|
}
|
|
|
- string sql = Sql.Remove(Sql.Length - 1, 1).ToString() + " from " + dt.Rows[0][getTable];
|
|
|
- dt1 = (DataTable)ExecuteSql(sql, "select");
|
|
|
- //设置DataTable的列名和描述
|
|
|
- for (int i = 0; i < field.Length; i++)
|
|
|
+ catch (Exception)
|
|
|
{
|
|
|
- dt1.Columns[i].ColumnName = field[i];
|
|
|
- dt1.Columns[i].Caption = caption[i];
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
}
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
- return dt1;
|
|
|
+ return dt;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -493,13 +327,13 @@ namespace UAS_MesInterface
|
|
|
public bool CheckExist(string TableName, string Condition)
|
|
|
{
|
|
|
string sql = "select count(1) from " + TableName + " where " + Condition;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
@@ -511,8 +345,10 @@ namespace UAS_MesInterface
|
|
|
/// <returns></returns>
|
|
|
public object ExecuteSql(string SQL, string Type, params object[] names)
|
|
|
{
|
|
|
+ command.Parameters.Clear();
|
|
|
object result = null;
|
|
|
- command = new OracleCommand(SQL, connection);
|
|
|
+ command.CommandText = SQL;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
//用来拼接参数的
|
|
|
if (names.Length > 0)
|
|
|
@@ -543,22 +379,59 @@ namespace UAS_MesInterface
|
|
|
switch (Type.ToUpper())
|
|
|
{
|
|
|
case "SELECT":
|
|
|
- result = new DataTable();
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill((DataTable)result);
|
|
|
- ad.Dispose();
|
|
|
+ result = new DataTable();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill((DataTable)result);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill((DataTable)result);
|
|
|
+ }
|
|
|
break;
|
|
|
case "DELETE":
|
|
|
- result = command.ExecuteNonQuery();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
break;
|
|
|
case "UPDATE":
|
|
|
- result = command.ExecuteNonQuery();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
break;
|
|
|
case "INSERT":
|
|
|
- result = command.ExecuteNonQuery();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
- command.Dispose();
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -569,21 +442,33 @@ namespace UAS_MesInterface
|
|
|
public void CheckSQL(string SQL)
|
|
|
{
|
|
|
SQL = SQL.Replace("?", ":Param");
|
|
|
- command = new OracleCommand(SQL, connection);
|
|
|
+ command.CommandText = SQL;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
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 OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill(dt);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
return int.Parse(dt.Rows[0][0].ToString());
|
|
|
}
|
|
|
|
|
|
@@ -607,13 +492,25 @@ namespace UAS_MesInterface
|
|
|
/// <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 = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
command.ArrayBindCount = DeleteID.Length;
|
|
|
command.Parameters.Add(new OracleParameter("DeleteID", OracleDbType.Long, DeleteID, ParameterDirection.Input));
|
|
|
- command.ExecuteNonQuery();
|
|
|
- command.Dispose();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.ArrayBindCount = 0;
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.ArrayBindCount = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -628,6 +525,23 @@ namespace UAS_MesInterface
|
|
|
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 (select 1 from OQCITEMSAMPLES where rownum<" + (Num + 1) + ")", "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)
|
|
|
@@ -734,7 +648,8 @@ namespace UAS_MesInterface
|
|
|
/// <param name="names"></param>
|
|
|
public void BatchInsert(string sql, params object[][] names)
|
|
|
{
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.Parameters.Clear();
|
|
|
+ command.CommandText = sql;
|
|
|
Reconnect(command);
|
|
|
command.ArrayBindCount = names[1].Length;
|
|
|
//因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
|
|
|
@@ -743,13 +658,24 @@ namespace UAS_MesInterface
|
|
|
{
|
|
|
command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
|
|
|
}
|
|
|
- command.ExecuteNonQuery();
|
|
|
- command.Dispose();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.ArrayBindCount = 0;
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.ArrayBindCount = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
|
|
|
{
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.Parameters.Clear();
|
|
|
+ command.CommandText = sql;
|
|
|
Reconnect(command);
|
|
|
command.ArrayBindCount = param1[0].Length;
|
|
|
//因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
|
|
|
@@ -758,28 +684,18 @@ namespace UAS_MesInterface
|
|
|
{
|
|
|
command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, param1[i], ParameterDirection.Input));
|
|
|
}
|
|
|
- command.ExecuteNonQuery();
|
|
|
- command.Dispose();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 查询DataList配置的字段
|
|
|
- /// </summary>
|
|
|
- /// <param name="TableName"></param>
|
|
|
- /// <param name="Caller"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public string GetDataList(string TableName, string Caller)
|
|
|
- {
|
|
|
- DataTable dt = new DataTable();
|
|
|
- string SQL = " select listagg(dld_field,',') within group (order by dld_id) from datalistdetail where dld_caller='" + Caller + "'";
|
|
|
- command = new OracleCommand(SQL, connection);
|
|
|
- Reconnect(command);
|
|
|
- OracleDataAdapter ad = new OracleDataAdapter(command);
|
|
|
- ad.Fill(dt);
|
|
|
- ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
- return dt.Rows[0][0].ToString();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.ArrayBindCount = 0;
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ command.ArrayBindCount = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -837,7 +753,6 @@ namespace UAS_MesInterface
|
|
|
return dt1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 通过条件更新
|
|
|
/// </summary>
|
|
|
@@ -847,10 +762,19 @@ namespace UAS_MesInterface
|
|
|
public string UpdateByCondition(string TableName, string update, string condition)
|
|
|
{
|
|
|
string sql = "update " + TableName + " set " + update + " where " + condition;
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
- command.ExecuteNonQuery();
|
|
|
- command.Dispose();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
return sql;
|
|
|
}
|
|
|
|
|
|
@@ -861,17 +785,25 @@ namespace UAS_MesInterface
|
|
|
/// <param name="param"></param>
|
|
|
public void CallProcedure(string ProcedureName, ref string[] param)
|
|
|
{
|
|
|
- command = new OracleCommand(ProcedureName);
|
|
|
- command.Connection = connection;
|
|
|
- Reconnect(command);
|
|
|
+ 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));
|
|
|
- command.ExecuteNonQuery();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
for (int i = 0; i < command.Parameters.Count; i++)
|
|
|
param[i] = command.Parameters[i].Value.ToString();
|
|
|
- command.Dispose();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -881,8 +813,6 @@ namespace UAS_MesInterface
|
|
|
public void ExecuteSQLTran(params string[] SQL)
|
|
|
{
|
|
|
OracleTransaction tx = connection.BeginTransaction();
|
|
|
- command = new OracleCommand();
|
|
|
- command.Connection = connection;
|
|
|
command.Transaction = tx;
|
|
|
try
|
|
|
{
|
|
|
@@ -891,7 +821,17 @@ namespace UAS_MesInterface
|
|
|
if (!string.IsNullOrEmpty(sql))
|
|
|
{
|
|
|
command.CommandText = sql;
|
|
|
- command.ExecuteNonQuery();
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ command.Connection = new OracleConnection(ConnectionStrings);
|
|
|
+ command.Connection.Open();
|
|
|
+ command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
tx.Commit();
|
|
|
@@ -901,7 +841,6 @@ namespace UAS_MesInterface
|
|
|
tx.Rollback();
|
|
|
throw new Exception(E.Message);
|
|
|
}
|
|
|
- command.Dispose();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -951,20 +890,32 @@ namespace UAS_MesInterface
|
|
|
public object GetLabelParam(string sql)
|
|
|
{
|
|
|
DataTable dt = new DataTable();
|
|
|
- command = new OracleCommand(sql, connection);
|
|
|
+ command.CommandText = sql;
|
|
|
+ command.CommandType = CommandType.Text;
|
|
|
Reconnect(command);
|
|
|
OracleDataAdapter ad = new OracleDataAdapter();
|
|
|
ad.SelectCommand = command;
|
|
|
- ad.Fill(dt);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ connection = new OracleConnection(ConnectionStrings);
|
|
|
+ connection.Open();
|
|
|
+ command.Connection = connection;
|
|
|
+ ad = new OracleDataAdapter();
|
|
|
+ ad.SelectCommand = command;
|
|
|
+ ad.Fill(dt);
|
|
|
+ }
|
|
|
if (dt.Rows.Count > 0)
|
|
|
{
|
|
|
ad.Dispose();
|
|
|
- command.Dispose();
|
|
|
+
|
|
|
return dt.Rows[0][0];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- command.Dispose();
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
@@ -980,6 +931,7 @@ namespace UAS_MesInterface
|
|
|
//新建一个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] != ')')
|
|
|
@@ -999,6 +951,14 @@ namespace UAS_MesInterface
|
|
|
return param;
|
|
|
}
|
|
|
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ if (command != null)
|
|
|
+ command.Dispose();
|
|
|
+ if (connection != null)
|
|
|
+ connection.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
private void Reconnect(OracleCommand cmd)
|
|
|
{
|
|
|
if (cmd.Connection.State == ConnectionState.Closed)
|