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.

152 lines
5.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Maths;
using System.Data;
using Mesnac.Equips;
using System.Data.Common;
using System.Data.SqlClient;
namespace Mesnac.Math.Feeding
{
public class AlarmInfo : IMath
{
private object NullResult = new DataTable();
public string Name
{
get { return "报警信息"; }
}
public string Caption
{
get
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("必须填写使用数据库连接的设备名称");
sb.AppendLine("例:");
sb.Append(" ").Append(Name).AppendLine("(A1)");
return sb.ToString();
}
}
public object Execute(string[] parameters, object[] buff)
{
string id = string.Empty;
if ((buff.Length > 0)
&& (buff[0] != null)
&& (!string.IsNullOrWhiteSpace(buff[0].ToString())))
{
id = buff[0].ToString();
}
if (string.IsNullOrWhiteSpace(id))
{
return this.NullResult;
}
if (parameters.Length < 1)
{
return this.NullResult;
}
string equipName = parameters[0];
BaseEquip equip = null;
if (!Mesnac.Equips.Factory.Instance.AllEquips.TryGetValue(equipName, out equip))
{
return this.NullResult;
}
DataTable Result = new DataTable();
try
{
DbConnection conn = equip.Main.ConnType.Connection as DbConnection;
if (conn == null)
{
return this.NullResult;
}
using (SqlConnection sqlCon = new SqlConnection(conn.ConnectionString))
{
sqlCon.Open();
string sqlstr = "SELECT ssValue FROM SysKeyValue WHERE ssKey='AlarmInfo'";
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlCon;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = sqlstr;
object obj = sqlcmd.ExecuteScalar();
string strSql2 = string.Empty;
if (obj != null && obj != DBNull.Value)
{
strSql2 = obj.ToString();
if (string.IsNullOrWhiteSpace(strSql2))
{
return this.NullResult;
}
SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSql2, sqlCon);
DataSet ds = new DataSet();
sqlAdapter.Fill(ds);
Result = ds.Tables[0];
}
sqlCon.Close();
}
#region 旧代码在多线程访问数据库下容易发生DataReader未关闭错误
//bool isOpen = true;
//if (conn.State != ConnectionState.Open)
//{
// isOpen = false;
// conn.Open();
//}
//DbCommand sqlcmd = conn.CreateCommand();
//sqlcmd.CommandType = System.Data.CommandType.Text;
//string sqlstr = "SELECT ssValue FROM SysKeyValue WHERE ssKey='AlarmInfo'";
//sqlcmd.CommandText = sqlstr;
//object obj = sqlcmd.ExecuteScalar();
//string strSql2 = string.Empty;
//if (obj != null && obj != DBNull.Value)
//{
// strSql2 = obj.ToString();
// sqlcmd.Parameters.Clear();
// sqlcmd.CommandText = strSql2;
// using (DbDataReader reader = sqlcmd.ExecuteReader())
// {
// for (int i = 0; i < reader.FieldCount; i++)
// {
// Result.Columns.Add(reader.GetName(i));
// }
// while (reader.Read())
// {
// DataRow row = Result.NewRow();
// for (int i = 0; i < reader.FieldCount; i++)
// {
// row[reader.GetName(i)] = reader[i];
// }
// Result.Rows.Add(row);
// }
// reader.Close();
// }
// sqlcmd.Dispose();
//}
//if (!isOpen)
//{
// conn.Close();
//}
//if (string.IsNullOrWhiteSpace(strSql2))
//{
// return this.NullResult;
//}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("发生错误在Mesnac.Math.Fedding.AlarmInfo" + ex.Message, ex);
}
Result.TableName = String.Format("T_{0}_{1}", "AlarmInfo", Guid.NewGuid().ToString());
return Result;
}
}
}