|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using Mesnac.Equips;
|
|
|
|
|
using Mesnac.Maths;
|
|
|
|
|
|
|
|
|
|
namespace Mesnac.Math.Database
|
|
|
|
|
{
|
|
|
|
|
public class ExecuteSql : 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 sqlstr = string.Empty;
|
|
|
|
|
if ((buff.Length > 0)
|
|
|
|
|
&& (buff[0] != null)
|
|
|
|
|
&& (!string.IsNullOrWhiteSpace(buff[0].ToString())))
|
|
|
|
|
{
|
|
|
|
|
sqlstr = buff[0].ToString();
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(sqlstr))
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
DbConnection conn = equip.Main.ConnType.Connection as DbConnection;
|
|
|
|
|
if (conn == null)
|
|
|
|
|
{
|
|
|
|
|
return this.NullResult;
|
|
|
|
|
}
|
|
|
|
|
bool isOpen = true;
|
|
|
|
|
if (conn.State != ConnectionState.Open)
|
|
|
|
|
{
|
|
|
|
|
isOpen = false;
|
|
|
|
|
conn.Open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataTable Result = new DataTable();
|
|
|
|
|
DbCommand sqlcmd = conn.CreateCommand();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
sqlcmd.CommandType = System.Data.CommandType.Text;
|
|
|
|
|
sqlcmd.CommandText = sqlstr;
|
|
|
|
|
Result.Load(sqlcmd.ExecuteReader());
|
|
|
|
|
if (!isOpen)
|
|
|
|
|
{
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
Result.TableName = sqlstr;
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ExecuteSqlXmlToTable : IMath
|
|
|
|
|
{
|
|
|
|
|
private object NullResult = new DataTable();
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "执行SQL反序列化"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 sqlstr = string.Empty;
|
|
|
|
|
if ((buff.Length > 0)
|
|
|
|
|
&& (buff[0] != null)
|
|
|
|
|
&& (!string.IsNullOrWhiteSpace(buff[0].ToString())))
|
|
|
|
|
{
|
|
|
|
|
sqlstr = buff[0].ToString();
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(sqlstr))
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
DbConnection conn = equip.Main.ConnType.Connection as DbConnection;
|
|
|
|
|
if (conn == null)
|
|
|
|
|
{
|
|
|
|
|
return this.NullResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isOpen = true;
|
|
|
|
|
if (conn.State != ConnectionState.Open)
|
|
|
|
|
{
|
|
|
|
|
isOpen = false;
|
|
|
|
|
conn.Open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataTable Result = new DataTable();
|
|
|
|
|
DbCommand sqlcmd = conn.CreateCommand();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
sqlcmd.CommandType = System.Data.CommandType.Text;
|
|
|
|
|
sqlcmd.CommandText = sqlstr;
|
|
|
|
|
object obj = sqlcmd.ExecuteScalar();
|
|
|
|
|
string xml = string.Empty;
|
|
|
|
|
if (obj != null && obj != DBNull.Value)
|
|
|
|
|
{
|
|
|
|
|
xml = obj.ToString();
|
|
|
|
|
}
|
|
|
|
|
if (!isOpen)
|
|
|
|
|
{
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(xml))
|
|
|
|
|
{
|
|
|
|
|
return this.NullResult;
|
|
|
|
|
}
|
|
|
|
|
Result = DeserializeDataTable(xml);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
Result.TableName = sqlstr;
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DataTable DeserializeDataTable(string pXml)
|
|
|
|
|
{
|
|
|
|
|
StringReader strReader = new StringReader(pXml);
|
|
|
|
|
XmlReader xmlReader = XmlReader.Create(strReader);
|
|
|
|
|
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
|
|
|
|
|
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
|
|
|
|
|
return dt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|