12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163 |
- using NPOI.SS.Formula.Functions;
- using Oracle.ManagedDataAccess.Client;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using UAS_MES_NEW.PublicMethod;
- namespace UAS_MES_NEW.DataOperate
- {
- class DataHelper
- {
-
- private string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=113.98.196.181)(PORT=1520)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
-
- public static string DBConnectionString;
- public static OracleConnection connection = null;
- OracleCommand command = null;
- int ReconnectTime = 0;
-
-
-
- public DataHelper()
- {
- try
- {
-
- if (DBConnectionString == null || DBConnectionString == ConnectionStrings)
- connection = new OracleConnection(ConnectionStrings);
- else
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- }
- catch (Exception e) { LogManager.DoLog(e.Message); }
- }
-
-
-
- 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);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- ad.Dispose();
- return dt;
- }
-
-
-
- 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);
- Console.WriteLine(sql);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- ad.Dispose();
- if (dt.Rows.Count > 0)
- {
- return dt.Rows[0][0];
- }
- else
- {
- return "";
- }
- }
-
-
-
-
-
-
- public object ExecutePrintSQL(string SQL, params string[] Parameters)
- {
-
- 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();
- return dt;
- }
- return "参数错误,请检查SQL语句";
- }
-
-
-
-
- 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);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- ad.Dispose();
- return int.Parse(dt.Rows[0][0].ToString());
- }
-
-
-
-
-
- public int getRowCount(string TableName)
- {
- DataTable dt = new DataTable();
- string sql = "select count(1) from " + TableName;
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- ad.Dispose();
- return int.Parse(dt.Rows[0][0].ToString());
- }
-
-
-
- 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);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- ad.Dispose();
- return dt;
- }
-
-
-
-
-
- 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++)
- {
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- public DataTable getFieldsDatasByPageing(string TableName, string Fields, int CurrentPage, int PageSize, string Caller, params string[] condition)
- {
- DataTable dt = new DataTable();
- StringBuilder sql = new StringBuilder();
-
-
- string[] caption = GetCaptionFromField(Fields);
-
- string[] field = GetField(Fields);
- sql.Append(" select * from (select RowNum RN, A.* from (select ");
- sql.Append(AddField(caption));
- if (condition.Length > 0)
- {
- if (condition[0] != null && condition[0].Trim() != "")
- sql.Append(" from " + TableName + " where " + condition[0] + " ) A where ROWNUM <=" + CurrentPage * PageSize + ") where RN>" + (CurrentPage - 1) * PageSize);
- else
- sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
- }
- command = new OracleCommand(sql.ToString(), connection);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.Fill(dt);
- ad.Dispose();
- dt.Columns.RemoveAt(0);
- foreach (DataColumn dc in dt.Columns)
- {
- dc.ColumnName = field[dt.Columns.IndexOf(dc)];
- dc.Caption = caption[dt.Columns.IndexOf(dc)];
- }
- return dt;
- }
-
-
-
- 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);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- ad.Dispose();
- return dt;
- }
-
-
-
- public DataTable getFieldsDatas(string TableName, string Fields)
- {
- DataTable dt = new DataTable();
- string sql = "select ";
- sql += Fields;
- sql += " from " + TableName;
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- ad.SelectCommand = command;
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- ad.Dispose();
- return dt;
- }
-
-
-
-
-
-
- public void UpDateTableByCondition(DataTable DataTable, string TableName, string PrimaryKey, params string[] sql)
- {
- if (DataTable == null)
- {
- return;
- }
- StringBuilder sb = new StringBuilder();
-
- 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() == "")
- {
-
- 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);
-
- command.ArrayBindCount = DataTable.Rows.Count;
-
- 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();
- }
-
-
-
-
-
-
- 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('#');
-
- string dbtable = dt.Rows[0]["ds_tables"].ToString();
-
- for (int i = 0; i < dbfield.Length; i++)
- {
- dbfield[i] = dbfield[i].Split(',')[0];
- }
-
- DataTable dt1 = new DataTable();
-
- for (int i = 0; i < cnfield.Length; i++)
- {
- dt1.Columns.Add(cnfield[i]);
- dt1.Columns[i].Caption = dbfield[i];
- }
-
-
- return dt1;
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
- public bool CheckExist(string TableName, string Condition)
- {
- string sql = "select count(1) from " + TableName + " where " + Condition;
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- DataTable dt = new DataTable();
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- ad.Dispose();
- Console.WriteLine(dt.Rows[0][0].ToString());
- Console.WriteLine(int.Parse(dt.Rows[0][0].ToString()) > 0);
- return int.Parse(dt.Rows[0][0].ToString()) > 0;
- }
-
-
-
-
-
-
- public object ExecuteSql(string SQL, string Type, params object[] names)
- {
- object result = null;
- command = new OracleCommand(SQL, connection);
- Reconnect(command);
-
- if (names.Length > 0)
- {
- string[] par = SQL.Split(':');
-
- StringBuilder[] addpar = new StringBuilder[par.Length - 1];
- for (int i = 0; i < par.Length - 1; i++)
- {
-
- char[] 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));
- }
- Console.WriteLine(SQL);
- switch (Type.ToUpper())
- {
- case "SELECT":
- OracleDataAdapter ad = new OracleDataAdapter(command);
- result = new DataTable();
- try
- {
- ad.Fill((DataTable)result);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(SQL, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill((DataTable)result);
- }
- break;
- case "DELETE":
- try
- {
- result = command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- result = command.ExecuteNonQuery();
- }
- break;
- case "UPDATE":
- try
- {
- result = command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- result = command.ExecuteNonQuery();
- }
- break;
- case "INSERT":
- try
- {
- result = command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- result = command.ExecuteNonQuery();
- }
- break;
- }
- return result;
- }
-
-
-
-
- public void CheckSQL(string SQL)
- {
- SQL = SQL.Replace("?", ":Param");
- command = new OracleCommand(SQL, connection);
- command.ExecuteNonQuery();
- }
- public int GetDistinctRowCount(string TableName, string Field)
- {
- DataTable dt = new DataTable();
- string sql = "select distinct count('" + Field + "') from " + TableName;
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter(command);
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- ad.Dispose();
- return int.Parse(dt.Rows[0][0].ToString());
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- public void DeleteDataByID(string TableName, string ID, string[] DeleteID)
- {
- string sql = "delete from " + TableName + " where " + ID + " =:DeleteID";
- 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();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- command.ExecuteNonQuery();
- }
- }
-
-
-
-
-
- public string GetSEQ(string SeqName)
- {
- DataTable dt = new DataTable();
- dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".NEXTVAL FROM DUAL", "select");
- return dt.Rows[0][0].ToString();
- }
-
-
-
-
-
- public string[] GetSEQ(string SeqName, int Num)
- {
- DataTable dt = new DataTable();
- dt = (DataTable)ExecuteSql("SELECT " + SeqName + ".nextval FROM DUAL CONNECT BY LEVEL<=" + Num, "select");
- string[] SerialNum = new string[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- SerialNum[i] = dt.Rows[i][0].ToString();
- }
- return SerialNum;
- }
- public void SaveDataTable(DataTable dt, string TableName, string ID, params string[] sql)
- {
- if (dt == null)
- {
- return;
- }
- StringBuilder sb = new StringBuilder();
-
- DataTable 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() == "")
- {
-
- 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());
- }
-
-
-
-
-
-
- public void BatchInsert(string sql, params object[][] names)
- {
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- command.ArrayBindCount = names[1].Length;
-
-
- for (int i = 1; i <= names[0].Length; i++)
- {
- command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
- }
- try
- {
- command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- command.ExecuteNonQuery();
- }
- }
- public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
- {
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- command.ArrayBindCount = param1[0].Length;
-
-
- for (int i = 0; i < param.Length; i++)
- {
- command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, param1[i], ParameterDirection.Input));
- }
- try
- {
- command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- command.ExecuteNonQuery();
- }
- }
-
-
-
-
-
-
- public object GetConfig(string Code, string Caller)
- {
- DataTable dt = new DataTable();
- string sql = "select Data from configs where code='" + Code + "' and caller='" + Caller + "'";
- dt = (DataTable)ExecuteSql(sql, "select");
- if (dt.Rows.Count == 0)
- {
- return "";
- }
- else
- {
- return dt.Rows[0]["Data"];
- }
- }
-
- 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;
- }
-
-
-
-
-
-
- public string UpdateByCondition(string TableName, string update, string condition)
- {
- string sql = "update " + TableName + " set " + update + " where " + condition;
- command = new OracleCommand(sql, connection);
- Console.WriteLine(sql);
- Reconnect(command);
- try
- {
- command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- command.ExecuteNonQuery();
- }
- return sql;
- }
-
-
-
-
-
- public void CallProcedure(string ProcedureName, ref string[] param)
- {
- command = new OracleCommand(ProcedureName);
- command.Connection = connection;
- Reconnect(command);
- 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, 4000, 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();
- }
- public void CallProcedure(string ProcedureName, string[] ParamName, ref string[] param)
- {
- command = new OracleCommand(ProcedureName);
- command.Connection = connection;
- Reconnect(command);
- command.CommandText = ProcedureName;
- command.CommandType = CommandType.StoredProcedure;
- for (int i = 0; i < param.Length; i++)
- {
- command.Parameters.Add(new OracleParameter(ParamName[i], OracleDbType.Varchar2, 200, param[i], ParameterDirection.InputOutput));
-
- }
- 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();
- }
-
-
-
-
- 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))
- {
- Console.WriteLine(sql);
- command.CommandText = sql;
- try
- {
- command.ExecuteNonQuery();
- }
- catch (Exception)
- {
- command.Connection = new OracleConnection(DBConnectionString);
- command.Connection.Open();
- command.ExecuteNonQuery();
- }
- }
- }
- tx.Commit();
- }
- catch (Exception E)
- {
- tx.Rollback();
- throw new Exception(E.Message);
- }
- }
-
-
-
-
-
- private string AddField(string[] Fields)
- {
- string sql = " ";
- foreach (string field in Fields)
- {
- sql += field + ",";
- }
- return sql.Substring(0, sql.Length - 1);
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- private static string[] GetField(string field)
- {
- string[] fields = field.Split(',');
- for (int i = 0; i < fields.Length; i++)
- {
- fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
- }
- return fields;
- }
- public object GetLabelParam(string sql)
- {
- DataTable dt = new DataTable();
- command = new OracleCommand(sql, connection);
- Reconnect(command);
- OracleDataAdapter ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- try
- {
- ad.Fill(dt);
- }
- catch (Exception)
- {
- connection = new OracleConnection(DBConnectionString);
- connection.Open();
- command = new OracleCommand(sql, connection);
- ad = new OracleDataAdapter();
- ad.SelectCommand = command;
- ad.Fill(dt);
- }
- if (dt.Rows.Count > 0)
- {
- ad.Dispose();
- return dt.Rows[0][0];
- }
- else
- {
- return "";
- }
- }
- public static string[] GetParamFromSQL(string SQL)
- {
- string[] par = SQL.Split(':');
-
- StringBuilder[] addpar = new StringBuilder[par.Length - 1];
- string[] param = new string[par.Length - 1];
- for (int i = 0; i < par.Length - 1; i++)
- {
-
- char[] 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;
- }
- public void Dispose()
- {
- }
- private void Reconnect(OracleCommand cmd)
- {
- if (cmd.Connection.State == ConnectionState.Closed)
- {
- cmd.Connection.Open();
- LogManager.DoLog("超时重连");
- }
- }
- }
- }
|