You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.2 KiB
C#

using System;
using System.IO;
using System.Data;
using Mesnac.Equips;
using Mesnac.Equips.BaseInfo;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
namespace Mesnac.Equip.ConfigInfo.MSSQL.Database
{
public class Equip : BaseEquip
{
SqlConnection readconn = new SqlConnection();
public override bool Open()
{
lock (this)
{
if (readconn == null)
{
readconn = new SqlConnection();
}
if (readconn.State == System.Data.ConnectionState.Open)
{
return true;
}
readconn.ConnectionString = ((Mesnac.Equips.Connection.Database.ConnType)this.Main.ConnType).ConnectionString;
readconn.Open();
if (readconn != null && readconn.State == System.Data.ConnectionState.Open)
{
this.Main.ConnType.Connection = readconn;
return true;
}
this.Main.ConnType.Connection = null;
return false;
}
}
public override bool Read(string block, int start, int len, out object[] buff)
{
lock (this)
{
buff = new object[len];
if (!this.Open())
{
return false;
}
//SqlCommand sqlcmd = readconn.CreateCommand();
SqlDataAdapter adapter = null;
string sqlstr = block.ToString();
try
{
//sqlcmd.CommandType = System.Data.CommandType.Text;
//sqlcmd.CommandText = sqlstr;
//DataTable dt = new DataTable();
//using (SqlDataReader reader = sqlcmd.ExecuteReader())
//{
// dt.Load(reader);
// buff = new object[dt.Rows.Count];
// for (int i = 0; i < dt.Rows.Count; i++)
// {
// buff[i] = dt.Rows[i];
// }
// reader.Close();
// reader.Dispose();
//}
adapter = new SqlDataAdapter(sqlstr, readconn);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
buff = new object[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
buff[i] = dt.Rows[i];
}
return true;
}
catch(Exception ex)
{
ICSharpCode.Core.LoggingService.Error(String.Format("读取数据库设备变量失败!-({0})", ex.Message));
//ICSharpCode.Core.LoggingService.Error(ex.StackTrace);
return false;
}
}
}
public override bool Write(int block, int start, object[] buff)
{
lock (this)
{
if (!this.Open())
{
return false;
}
SqlCommand command = readconn.CreateCommand();
string str = "UPDATE dbo.PLC SET ssValue=@ssValue WHERE ssBlock=@ssBlock AND ssAddress=@ssAddress";
try
{
command.CommandType = CommandType.Text;
command.CommandText = str;
for (int i = 0; i < buff.Length; i++)
{
command.Parameters.Clear();
object obj2 = buff[i];
if (obj2 == null)
{
obj2 = DBNull.Value;
}
command.Parameters.AddWithValue("@ssValue", obj2);
command.Parameters.AddWithValue("@ssBlock", block.ToString());
command.Parameters.AddWithValue("@ssAddress", (start + i).ToString());
command.ExecuteNonQuery();
}
return true;
}
catch(Exception ex)
{
ICSharpCode.Core.LoggingService.Error(String.Format("写入数据库设备变量失败!-({0})", ex.Message));
//ICSharpCode.Core.LoggingService.Error(ex.StackTrace);
return false;
}
}
}
public override void Close()
{
lock (this)
{
try
{
if (readconn != null)
{
readconn.Close();
readconn.Dispose();
readconn = null;
}
}
catch(Exception ex)
{
ICSharpCode.Core.LoggingService.Error(String.Format("关闭数据库设备连接失败!-({0})", ex.Message));
ICSharpCode.Core.LoggingService.Error(ex.StackTrace);
}
}
}
}
}