You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
5.3 KiB
198 lines
5.3 KiB
using System;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.IO;
|
|
|
|
|
|
namespace coilPosition
|
|
{
|
|
class DatCon : IDisposable
|
|
{
|
|
private SqlConnection con1;
|
|
private SqlConnection con2;
|
|
private string str1;
|
|
private string str2;
|
|
|
|
public DatCon()
|
|
{
|
|
//string[] alllines = File.ReadAllLines("ip.txt");
|
|
//string strServer = alllines[0].ToString();
|
|
//string strDatabase = alllines[1].ToString();
|
|
//string strUid = alllines[2].ToString();
|
|
//string strPwd = alllines[3].ToString();
|
|
|
|
//str = "Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";Persist Security Info=True;User ID=" + strUid + ";Password=" + strPwd;
|
|
str1 = "Data Source=10.114.48.30;Initial Catalog=scJCIFisB8YB;Persist Security Info=True;User ID=X89MES;Password=x89mes";
|
|
str2 = "Data Source=10.114.50.240;Initial Catalog=B9MES_TEST;Persist Security Info=True;User ID=b9mes;Password=MESmes123";
|
|
}
|
|
|
|
private void open1()
|
|
{
|
|
try
|
|
{
|
|
if (con1 == null)
|
|
{
|
|
con1 = new SqlConnection(str1);
|
|
con1.Open();
|
|
//sqltra = con.BeginTransaction();//开始事务
|
|
}
|
|
else
|
|
{
|
|
if (con1.State.Equals(ConnectionState.Closed))
|
|
{
|
|
con1.Open();
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void open2()
|
|
{
|
|
try
|
|
{
|
|
if (con2 == null)
|
|
{
|
|
con2 = new SqlConnection(str2);
|
|
con2.Open();
|
|
//sqltra = con.BeginTransaction();//开始事务
|
|
}
|
|
else
|
|
{
|
|
if (con2.State.Equals(ConnectionState.Closed))
|
|
{
|
|
con2.Open();
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void close1()
|
|
{
|
|
try
|
|
{
|
|
if (con1.State.Equals(ConnectionState.Open))
|
|
{
|
|
con1.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void close2()
|
|
{
|
|
try
|
|
{
|
|
if (con2.State.Equals(ConnectionState.Open))
|
|
{
|
|
con2.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public DataTable getTable1(string strSql)
|
|
{
|
|
try
|
|
{
|
|
open1();
|
|
SqlCommand com = new SqlCommand(strSql, con1);
|
|
SqlDataAdapter sda = new SqlDataAdapter(com);
|
|
DataSet ds = new DataSet();
|
|
sda.Fill(ds);
|
|
close1();
|
|
return ds.Tables[0];
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public DataTable getTable2(string strSql)
|
|
{
|
|
try
|
|
{
|
|
open2();
|
|
SqlCommand com = new SqlCommand(strSql, con2);
|
|
SqlDataAdapter sda = new SqlDataAdapter(com);
|
|
DataSet ds = new DataSet();
|
|
sda.Fill(ds);
|
|
close2();
|
|
return ds.Tables[0];
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public bool comExecute1(string str)
|
|
{
|
|
try
|
|
{
|
|
open1();
|
|
SqlCommand com = new SqlCommand(str, con1);
|
|
//com.Transaction = sqltra;//,在执行SQL时,
|
|
int s = com.ExecuteNonQuery();
|
|
//sqltra.Commit();
|
|
close1();
|
|
if (s > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//sqltra.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool comExecute2(string str)
|
|
{
|
|
try
|
|
{
|
|
open2();
|
|
SqlCommand com = new SqlCommand(str, con2);
|
|
//com.Transaction = sqltra;//,在执行SQL时,
|
|
int s = com.ExecuteNonQuery();
|
|
//sqltra.Commit();
|
|
close2();
|
|
if (s > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//sqltra.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void writeLog(string logStr)
|
|
{
|
|
FileStream fs=new FileStream("d:\\log.txt",FileMode.Create);
|
|
byte[] data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yy-MM-dd")+" : "+logStr);
|
|
fs.Write(data,0,data.Length);
|
|
fs.Flush();
|
|
fs.Close();
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|
|
|