GetPLC_Data/GetData_PLC/OracleLink.cs

165 lines
5.2 KiB
C#
Raw Normal View History

2025-11-08 00:17:36 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
#pragma warning disable 0618
namespace GetData_PLC
{
public class OracleLink
{
private static string strCon_jlxt = "data source=orcl;user id=cxc;password=cxc";
private static string strCon_Dtjk = "data source=TRQSCADA;user id=dtjk;password=Dtjk_123";
public static void SaveDataTable(string sql, DataTable _TempTable, string strDataBaseName, string strErr)
{
// Create a new Oracle command
OracleCommand command = null;
string ConnectionStrings = "";
if (strDataBaseName == "JLXT") ConnectionStrings = strCon_jlxt;
if (strDataBaseName == "DTJK") ConnectionStrings = strCon_Dtjk;
try
{
//Create a connection
using (OracleConnection connection = new OracleConnection(ConnectionStrings))
{
command = new OracleCommand(sql, connection);
OracleDataAdapter adapter = new OracleDataAdapter(command);
OracleCommandBuilder cb = new OracleCommandBuilder(adapter);
adapter.Update(_TempTable);
strErr = "";
//return _TempTable;
}
}
catch (Exception ex)
{
strErr = " oracle保存表格错误" + ex.Message;
}
}
public static string getStationName(string strID)
{
DataTable dtStation = new DataTable();
dtStation = ExecuteDataTable("select * from SYS_ORGANISE where ORG_ID='" + strID + "'", "JLXT", "");
if (dtStation.Rows.Count > 0)
{
return dtStation.Rows[0]["ORG_NAME"].ToString();
}
else
{
return "";
}
}
public static string getStationName(string strID,ref string strDeptID)
{
DataTable dtStation = new DataTable();
dtStation = ExecuteDataTable("select * from SYS_ORGANISE where ORG_ID='" + strID + "'", "JLXT", "");
if (dtStation.Rows.Count > 0)
{
strDeptID = dtStation.Rows[0]["PARENT_ORG_ID"].ToString();
return dtStation.Rows[0]["ORG_NAME"].ToString();
}
else
{
return "";
}
}
public static string getDeptID(string strID)
{
DataTable dtStation = new DataTable();
dtStation = ExecuteDataTable("select * from SYS_ORGANISE where ORG_ID='" + strID + "'", "JLXT", "");
if (dtStation.Rows.Count > 0)
{
return dtStation.Rows[0]["PARENT_ORG_ID"].ToString();
}
else
{
return "";
}
}
public static DataTable ExecuteDataTable(string sql,string strDataBaseName,string strError)
{
// Create a new Oracle command
OracleCommand command = null;
string ConnectionStrings="";
if (strDataBaseName == "JLXT") ConnectionStrings = strCon_jlxt;
if (strDataBaseName == "DTJK") ConnectionStrings = strCon_Dtjk;
try
{
//Create a connection
using (OracleConnection connection = new OracleConnection(ConnectionStrings))
{
command = new OracleCommand(sql, connection);
OracleDataAdapter adapter = new OracleDataAdapter(command);
DataSet ds = new DataSet();
//System.Threading.Thread.Sleep(10)
adapter.Fill(ds);
//if(ds.Tables.Count<1)
// return;
DataTable dt = ds.Tables[0].Copy();
ds.Dispose();
strError = "";
return dt;
}
}
catch (Exception ex)
{
strError = " oracle查询表格出错" + ex.Message;
return null;
}
}
public static int ExecuteNonQuery(string sql, string strDataBaseName, string strError)
{
string ConnectionStrings = "";
if (strDataBaseName == "JLXT") ConnectionStrings = strCon_jlxt;
if (strDataBaseName == "DTJK") ConnectionStrings = strCon_Dtjk;
using (OracleConnection con = new OracleConnection(ConnectionStrings))
{
int Num;
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
Num = cmd.ExecuteNonQuery();
con.Close();
return Num;
}
}
public static void strAppend(string strFilePath, string strMessage)
{
using (StreamWriter sw = File.AppendText(strFilePath))
{
sw.WriteLine(strMessage);
sw.Flush();
sw.Close();
}
}
}
}