123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284 |
- using Oracle.ManagedDataAccess.Client;
- using System;
- using System.Data;
- using System.Text;
- using System.Collections;
- using UAS_LabelMachine.PublicMethod;
- using System.Collections.Generic;
- namespace UAS_LabelMachine
- {
- public class DataHelper
- {
- ////海创外网地址
- //private readonly string ConnectionStrings = "Data Source=cmityj.gicp.net/orcl;User ID=HC_TEST;PassWord=select!#%*(";
- ////海创ERP地址
- //public static readonly string ERPAddesss = "http://cmityj.gicp.net:8099/ERP/";
- ////海创FTP
- //public static readonly string FTPAddress = "ftp://cmityj.gicp.net|HCFTP|Admin1@#";
- //public static readonly string InnerFTPAddress = "ftp://192.168.1.8|HCFTP|Admin1@#";
- ////Oracle端口
- //public static readonly string OraclePort = "1521";
- ////需要显示的账套
- //public static readonly string Masters = "HC_TEST";
- ////凯而高地址
- //private readonly string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=KRG_TEST;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=112.25.154.186)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
- ////凯而高ERP地址
- //public static readonly string ERPAddesss = "http://112.25.154.186:8099/ERP/";
- ////凯而高FTP
- //public static readonly string FTPAddress = "ftp://112.25.154.186:21|KEGFTP|Admin1@#";
- //public static readonly string InnerFTPAddress = "ftp://112.25.154.186:21|HCFTP|Admin1@#";
- ////Oracle端口
- //public static readonly string OraclePort = "1521";
- ////需要显示的账套
- //public static readonly string Masters = "GOLDEN";
- ////高登地址
- //private readonly string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=DATACENTER;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=183.238.39.179)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
- ////高登ERP地址
- //public static readonly string ERPAddesss = "http://183.238.39.179:8099/ERP/";
- ////高登TP
- //public static readonly string InnerFTPAddress = "ftp://192.168.0.158:21|GOLDENMES|Admin1@#";
- //public static readonly string FTPAddress = "ftp://183.238.39.179:21|GOLDENMES|Admin1@#";
- ////Oracle端口
- //public static readonly string OraclePort = "1521";
- ////需要显示的账套
- //public static readonly string Masters = "GOLDEN";
- ////怡海能达外网地址
- //private readonly string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=DATACENTER;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=sz.hi-mantech.com)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
- ////怡海能达ERP地址
- //public static readonly string ERPAddesss = "http://sz.hi-mantech.com:8099/ERP/";
- ////怡海能达FTP
- //public static readonly string FTPAddress = "ftp://sz.hi-mantech.com:46688|yhndftp|Stga28ytG8";
- ////Oracle端口
- //public static readonly string OraclePort = "1521";
- ////需要显示的账套
- //public static readonly string Masters = "YHND_SZ,YHND_HK,DATACENTER";
- //华商龙外网地址
- private readonly string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=WMS_YHND;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.1.81.2)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
- //华商龙ERP地址
- public static readonly string ERPAddesss = "http://10.1.81.2/wms_yhnd/";
- //华商龙FTP
- public static readonly string FTPAddress = "ftp://218.18.115.198:21|Print|Administrator1@#";
- public static readonly string InnerFTPAddress = "ftp://218.18.115.198:21|Print|Administrator1@#";
- //Oracle端口
- public static readonly string OraclePort = "1521";
- //需要显示的账套
- public static readonly string Masters = "XSQ,N_YITOA_YWC,ZXN,N_YITOA_LHCT,N_YITOA_CT,N_CHL,N_YITOA_HK,N_HUASL_SHTL,N_WILICHK,N_WILIC,N_E_SHINE_SH,N_SHBJ,N_BJKG,N_HUASL,N_HUASL_QD,N_HUASL_XM,N_HUASL_SZ";
- ////UAS测试地址
- //private readonly string ConnectionStrings = "Data Source=192.168.253.6/orcl;User ID=UAS_TEST;PassWord=select!#%*(";
- ////UAS测试地址
- //public static readonly string ERPAddesss = "http://192.168.253.6/uas_test/";
- ////UAS测试FTP
- //public static readonly string FTPAddress = "ftp://192.168.253.9/PrintFile|mesconfig|Administrator1@#";
- ////Oracle端口
- //public static readonly string OraclePort = "1521";
- ////需要显示的账套
- //public static readonly string Masters = "UAS_TEST";
- public static string DBConnectionString;
- static OracleConnection connection = null;
- static OracleCommand command = null;
- /// <summary>
- /// 重置连接的次数
- /// </summary>
- int ReconnectTime = 0;
- /// <summary>
- /// 执行构造函数的时候打开数据库的链接
- /// </summary>
- public DataHelper()
- {
- try
- {
- //如果选择的是默认数据则直接用配置文件的信息连接,否则选择数据库的账套信息
- if (DBConnectionString == null || DBConnectionString == ConnectionStrings)
- {
- DBConnectionString = ConnectionStrings;
- connection = new OracleConnection(ConnectionStrings);
- }
- else
- {
- connection = new OracleConnection(DBConnectionString);
- }
- connection.Open();
- }
- catch (Exception e) { LogManager.DoLog(e.Message); }
- }
- /// <summary>
- ///
- /// </summary>
- public void Dispose()
- {
- connection.Dispose();
- }
- /// <summary>
- /// 根据表名获取该表字段数据类型
- /// </summary>
- public DataTable GetColumnDataType(string TableName)
- {
- DataTable dt = new DataTable();
- command = new OracleCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- ad.Dispose();
- command.Dispose();
- return dt;
- }
- public object GetLabelParam(string sql)
- {
- DataTable dt = new DataTable();
- command = new OracleCommand(sql, connection);
- OracleDataAdapter 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 ""; }
- }
- /// <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 OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- try
- {
- ad.Fill(dt);
- ReconnectTime = 0;
- }
- catch (Exception)
- {
- if (ReconnectTime == 0)
- {
- ReconnectTime++;
- connection = new OracleConnection(DBConnectionString);
- //成功执行后将重复连接数置为0
- dt = (DataTable)getFieldDataByCondition(TableName, Field, Condition);
- }
- }
- if (dt.Rows.Count > 0)
- {
- ad.Dispose();
- command.Dispose();
- return dt.Rows[0][0];
- }
- else
- {
- ad.Dispose();
- command.Dispose();
- return "";
- }
- }
- /// <summary>
- /// 执行打印的SQL
- /// </summary>
- /// <param name="SQL">SQL语句</param>
- /// <param name="Parameters">动态添加的参数,主要根据条码枪扫描获取</param>
- /// <returns></returns>
- public object ExecutePrintSQL(string SQL, params string[] Parameters)
- {
- //按照?拆分数据,然后以: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 = new OracleCommand(sb.ToString(), connection);
- 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();
- command.Dispose();
- return dt;
- }
- return "参数错误,请检查SQL语句";
- }
- /// <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 OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- int count = int.Parse(dt.Rows[0][0].ToString());
- command.Dispose();
- return count;
- }
- /// <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 OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- int count = int.Parse(dt.Rows[0][0].ToString());
- command.Dispose();
- return count;
- }
- /// <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 OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- command.Dispose();
- return dt;
- }
- /// <summary>
- /// 通过表名和获取单行的记录
- /// </summary>
- public DataTable getFieldsDatasByCondition(string sql)
- {
- DataTable dt = new DataTable();
- command = new OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- 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].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 OracleCommand(sql.ToString(), connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- dt.Columns.RemoveAt(0);
- foreach (DataColumn dc in dt.Columns)
- {
- dc.ColumnName = field[dt.Columns.IndexOf(dc)];
- dc.Caption = caption[dt.Columns.IndexOf(dc)];
- }
- command.Dispose();
- 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 OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- command.Dispose();
- return dt;
- }
- /// <summary>
- /// 通过表名,字段获取DataTable类型的数据
- /// </summary>
- public string[] getFieldsDatas(string TableName, string Fields)
- {
- ArrayList arr = new ArrayList();
- DataTable dt = new DataTable();
- string sql = "select ";
- sql += Fields;
- sql += " from " + TableName;
- command = new OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.SelectCommand = command;
- ad.Fill(dt);
- foreach (DataRow dc in dt.Rows)
- {
- arr.Add(dc[Fields]);
- }
- ad.Dispose();
- command.Dispose();
- return (string[])arr.ToArray(typeof(string));
- }
- /// <summary>
- /// 通过表名,字段获取DataTable类型的数据
- /// </summary>
- public string[] getFieldsDatas(string TableName, string Fields, string Condition)
- {
- ArrayList arr = new ArrayList();
- DataTable dt = new DataTable();
- string sql = "select ";
- sql += Fields;
- sql += " from " + TableName + " where " + Condition;
- command = new OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.SelectCommand = command;
- ad.Fill(dt);
- foreach (DataRow dc in dt.Rows)
- {
- arr.Add(dc[Fields]);
- }
- ad.Dispose();
- command.Dispose();
- return (string[])arr.ToArray(typeof(string));
- }
- /// <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);
- command.Dispose();
- ad.Dispose();
- }
- 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>
- /// 获取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,后面的是实际的列名
- 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)
- {
- field[dt.Rows.IndexOf(dr)] = dr[getCaption].ToString();
- caption[dt.Rows.IndexOf(dr)] = dr[getField].ToString();
- Sql.Append(dr[getField] + ",");
- }
- 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++)
- {
- dt1.Columns[i].ColumnName = field[i];
- dt1.Columns[i].Caption = caption[i];
- }
- ad.Dispose();
- command.Dispose();
- return dt1;
- }
- /// <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 OracleCommand(sql, connection);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- DataTable dt = new DataTable();
- ad.Fill(dt);
- if (int.Parse(dt.Rows[0][0].ToString()) > 0)
- {
- command.Dispose();
- ad.Dispose();
- return true;
- }
- else
- {
- command.Dispose();
- ad.Dispose();
- return false;
- }
- }
- /// <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 OracleCommand(SQL, connection);
- Console.WriteLine(SQL);
- //用来拼接参数的
- 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 OracleParameter(addpar[i].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
- }
- }
- switch (Type.ToUpper())
- {
- case "SELECT":
- try
- {
- result = new DataTable();
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill((DataTable)result);
- ad.Dispose();
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command.Connection = connection;
- result = new DataTable();
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill((DataTable)result);
- ad.Dispose();
- }
- break;
- case "DELETE":
- result = command.ExecuteNonQuery();
- break;
- case "UPDATE":
- result = command.ExecuteNonQuery();
- break;
- case "INSERT":
- command.ExecuteNonQuery();
- break;
- }
- command.Dispose();
- return result;
- }
- /// <summary>
- /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
- /// </summary>
- /// <param name="SQL"></param>
- public bool CheckSQL(string SQL, out string ErrorMessage)
- {
- ErrorMessage = "";
- try
- {
- SQL = SQL.Replace("?", ":Param");
- command = new OracleCommand(SQL, connection);
- command.ExecuteNonQuery();
- command.Dispose();
- return true;
- }
- catch (Exception e)
- {
- command.Dispose();
- ErrorMessage = e.Message;
- return false;
- }
- }
- public int GetDistinctRowCount(string TableName, string Field)
- {
- DataTable dt = new DataTable();
- string sql = "select distinct count('" + Field + "') from " + TableName;
- OracleDataAdapter ad = new OracleDataAdapter(new OracleCommand(sql, connection));
- ad.Fill(dt);
- 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>
- /// 根据主键ID删除表的数据
- /// </summary>
- /// <param name="TableName">表名</param>
- /// <param name="ID">主键</param>
- /// <param name="DeleteID">需要删除主键ID的数组</param>
- public void DeleteDataByID(string TableName, string ID, string[] DeleteID)
- {
- string sql = "delete from " + TableName + " where " + ID + " =:DeleteID";
- command = new OracleCommand(sql, connection);
- command.ArrayBindCount = DeleteID.Length;
- command.Parameters.Add(new OracleParameter("DeleteID", OracleDbType.Long, DeleteID, ParameterDirection.Input));
- command.ExecuteNonQuery();
- command.Dispose();
- }
- /// <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>
- /// 批量通过SQL来执行插入操作 ,参数的第一个数一个string[]数组,用来传递需要添加的参数的名称
- /// 之后的是名称参数数组对应的 ,所有的插入参数数据长度必须是一致的
- /// </summary>
- /// <param name="sql"></param>
- /// <param name="names"></param>
- public void BatchInsert(string sql, params object[][] names)
- {
- command = new OracleCommand(sql, connection);
- command.ArrayBindCount = names[1].Length;
- //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
- //将第一个数组的下标固定为0作为循环添加的参数的名称
- //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
- //将第一个数组的下标固定为0作为循环添加的参数的名称
- for (int i = 1; i <= names[0].Length; i++)
- {
- if (names[0][i - 1].ToString().ToUpper().Contains("DATE") && !names[0][i - 1].ToString().ToUpper().Contains("DATECODE"))
- {
- DateTime[] time = new DateTime[names[i].Length];
- try
- {
- for (int j = 0; j < names[i].Length; j++)
- {
- time[j] = Convert.ToDateTime(names[i][j]);
- }
- command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Date, time, ParameterDirection.Input));
- }
- catch (Exception)
- {
- command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
- }
- }
- else
- {
- command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
- }
- }
- command.ExecuteNonQuery();
- command.Dispose();
- }
- public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
- {
- command = new OracleCommand(sql, connection);
- command.ArrayBindCount = param1[0].Length;
- //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
- //将第一个数组的下标固定为0作为循环添加的参数的名称
- for (int i = 0; i < param.Length; i++)
- {
- 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);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- command.Dispose();
- return dt.Rows[0][0].ToString();
- }
- /// <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 * 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 void UpdateByCondition(string TableName, string update, string condition)
- {
- string sql = "update " + TableName + " set " + update + " where " + condition;
- command = new OracleCommand(sql, connection);
- command.ExecuteNonQuery();
- command.Dispose();
- }
- /// <summary>
- /// 调用存储过程
- /// </summary>
- /// <param name="ProcedureName"></param>
- /// <param name="param"></param>
- public void CallProcedure(string ProcedureName, ref string[] param)
- {
- command = new OracleCommand(ProcedureName);
- command.Connection = connection;
- command.CommandText = ProcedureName;
- command.CommandType = CommandType.StoredProcedure;
- for (int i = 0; i < param.Length; i++)
- command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, 1000, param[i], ParameterDirection.InputOutput));
- try
- {
- command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- 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>
- /// 调用存储过程
- /// </summary>
- /// <param name="ProcedureName"></param>
- /// <param name="param"></param>
- public void CallProcedure(string ProcedureName, DataTable upload)
- {
- StringBuilder sql = new StringBuilder();
- sql.Append("declare barcode BarCodeUpload_arr;begin barcode:=BarCodeUpload_arr(");
- for (int i = 0; i < upload.Rows.Count; i++)
- {
- //特殊语句,传递回拼接的ID给到跟新本地数据
- sql.Append("BarCodeUpload(");
- for (int j = 0; j < upload.Columns.Count; j++)
- {
- sql.Append("'" + upload.Rows[i][j].ToString() + "',");
- }
- sql.Remove(sql.Length - 1, 1);
- sql.Append("),");
- }
- sql.Remove(sql.Length - 1, 1);
- sql.Append(");sp_uploadbarcode(barcode);END;");
- command = new OracleCommand();
- command.Connection = connection;
- command.CommandText = sql.ToString();
- try
- {
- command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command.Connection = connection;
- command.ExecuteNonQuery();
- }
- command.Dispose();
- }
- /// <summary>
- /// 出现异常进行回滚的执行方法
- /// </summary>
- /// <param name="SQL"></param>
- public void ExecuteSQLTran(params string[] SQL)
- {
- OracleTransaction tx = connection.BeginTransaction();
- command = new OracleCommand();
- command.Connection = connection;
- command.Transaction = tx;
- try
- {
- foreach (string sql in SQL)
- {
- if (!String.IsNullOrEmpty(sql))
- {
- command.CommandText = sql;
- command.ExecuteNonQuery();
- }
- }
- tx.Commit();
- }
- catch (OracleException E)
- {
- tx.Rollback();
- throw new Exception(E.Message);
- }
- }
- /// <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 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;
- }
- }
- }
|