using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using HuizhongLibrary.Data;
using HuizhongLibrary.Safety;
using System.Data.Common;
namespace HuizhongLibrary.Common.Configuration
{
public class ConfigurationSourceSection
{
#region 读取数据库连接
///
/// 读取数据库连接
///
/// 属性名称
public static void GetConnection(string AttributeName)
{ //读取属性值
string filePath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSourceSection.LoadXml_Attribute(AppDomain.CurrentDomain.BaseDirectory + "Setup.ini", "SetupPath");
if (filePath.Substring(filePath.Length - 1) != "/") filePath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSourceSection.LoadXml_Attribute(AppDomain.CurrentDomain.BaseDirectory + "Setup.ini", "SetupPath");
XmlDocument Document = new XmlDocument();
Document.Load(filePath);
XmlNodeList nodelist = Document.SelectNodes("child::configuration/appSettings/add");
Config cf =null;
foreach (XmlNode node in nodelist)
{
if (node.Attributes["key"].Value == AttributeName)
{
if (DataCache.ConfigCache.ContainsKey(AttributeName) == false)
{
cf = new Config();
DataCache.ConfigCache.Add(cf);
}
else
{
cf = DataCache.ConfigCache[AttributeName];
}
cf.Key = AttributeName;
try
{
var node2 = node.Attributes.GetNamedItem("NameSpace");
if (node2!=null) cf.Namespace = node.Attributes["NameSpace"].Value;
}
catch
{
cf.Namespace = "System.Data.SqlClient";
}
try
{
cf.Value = CryptoPrivate.Decryp(node.Attributes["value"].Value);
}
catch
{
cf.Value = node.Attributes["value"].Value;
}
}
}
}
#endregion
#region 读取数据库连接
///
/// 读取数据库连接
///
/// 属性名称
public static void GetConnection2(string AttributeName)
{ //读取属性值
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Setup.ini";
XmlDocument Document = new XmlDocument();
Document.Load(filePath);
XmlNodeList nodelist = Document.SelectNodes("child::configuration/appSettings/add");
Config cf = null;
foreach (XmlNode node in nodelist)
{
if (node.Attributes["key"].Value == AttributeName)
{
if (DataCache.ConfigCache.ContainsKey(AttributeName) == false)
{
cf = new Config();
DataCache.ConfigCache.Add(cf);
}
else
{
cf = DataCache.ConfigCache[AttributeName];
}
cf.Key = AttributeName;
try
{
cf.Namespace = node.Attributes["NameSpace"].Value;
}
catch
{
cf.Namespace = "System.Data.SqlClient";
}
try
{
cf.Value = CryptoPrivate.Decryp(node.Attributes["value"].Value);
}
catch
{
cf.Value = node.Attributes["value"].Value;
}
}
}
}
#endregion
#region 读取XML配置文件-属性值
///
/// 返回指点节点指点属性的值,AttributeName属性名称
///
/// 配置文件路径
/// 属性名称
public static string LoadXml_Attribute(string filePath, string AttributeName)
{ //读取属性值
if (System.IO.File.Exists(filePath) == false) return "";
XmlDocument Document = new XmlDocument();
Document.Load(filePath);
XmlNodeList nodelist = Document.SelectNodes("child::configuration/appSettings/add");
foreach (XmlNode node in nodelist)
{
if (node.Attributes["key"].Value == AttributeName)
{
string KeyValue = "";
try
{
KeyValue = CryptoPrivate.Decryp(node.Attributes["value"].Value);
}
catch
{
KeyValue = node.Attributes["value"].Value;
}
return KeyValue;
}
}
return "";
}
#endregion
#region 保存XML配置文件
///
///AttributeName属性名称,AttributeValue属性值
///
/// 属性名称
/// 属性值
/// 是否加密
public static void SaveXml_Attribute(string filePath, string AttributeName, string AttributeValue, bool IsEncrypt)
{
bool bk = true;
XmlDocument Document = new XmlDocument();
Document.Load(filePath);
//XmlNodeList nodelist = Document.GetElementsByTagName(NodeTypeName);
XmlNodeList nodelist = Document.SelectNodes("child::configuration/appSettings/add");
foreach (XmlNode node in nodelist)
{
if (node.Attributes["key"].Value == AttributeName)
{
if (IsEncrypt == true) { node.Attributes["value"].Value = CryptoPrivate.Encryp(AttributeValue); }
else { node.Attributes["value"].Value = AttributeValue; }
bk = false;
}
}
if (bk == true)
{
XmlElement Element = Document.CreateElement("add");
XmlAttribute Attrib = null;
Attrib = Document.CreateAttribute("key");
Attrib.Value = AttributeName;
Element.Attributes.Append(Attrib);
Attrib = Document.CreateAttribute("value");
if (IsEncrypt == true) { Attrib.Value = CryptoPrivate.Encryp(AttributeValue); }
else { Attrib.Value = AttributeValue; }
Element.Attributes.Append(Attrib);
nodelist = Document.SelectNodes("child::configuration/appSettings");
nodelist[0].AppendChild(Element);
}
Document.Save(filePath);
}
#endregion
#region 查询服务器上所有数据库
///
///查询服务器上所有数据库
///
public static DataTable getDataBase(string connectString)
{
// string dn="";
// if (userid != "") { dn = "data source=" + datasource + ";initial catalog=master;user id=" + userid + ";password=" + password; }
// else { dn = "data source=" + datasource + ";initial catalog=master;Integrated Security=True"; }
DataTable tb = new DataTable();
SqlConnection cn = new SqlConnection(connectString);
cn.Open();
cn.ChangeDatabase("master");
SqlCommand cmd = new SqlCommand("select name from sysdatabases order by name asc", cn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
#endregion
#region 查询数据库中所有表名
///
///查询数据库中所有表名
///
public static DataTable getTables(string connectString)
{
DataTable tb = new DataTable();
SqlConnection cn = new SqlConnection(connectString);
cn.Open();
SqlCommand cmd = new SqlCommand("select * from [sysobjects] where xtype='u' order by name asc", cn);//and status>0
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
#endregion
#region 查询数据库中所有表名
///
///查询数据库中所有表名
///
public static DataTable getTables(string connectString,string DataName)
{
DataTable tb = new DataTable();
SqlConnection cn = new SqlConnection(connectString);
cn.Open();
if (DataName!="") cn.ChangeDatabase(DataName);
SqlCommand cmd = new SqlCommand("select * from [sysobjects] where xtype='u' order by name asc", cn);//and status>0
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
#endregion
#region 查询数据库中所有视图名
///
///查询数据库中所有视图名
///
public static DataTable getViews(string connectString, string DataName)
{
DataTable tb = new DataTable();
SqlConnection cn = new SqlConnection(connectString);
cn.Open();
cn.ChangeDatabase(DataName);
SqlCommand cmd = new SqlCommand("select v.[name] from sys.views as v, sys.schemas as s where v.schema_id = s.schema_id order by v.name", cn);//and status>0
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
#endregion
#region 检索列
public static DataTable getColumns(string connectString, string TableName)
{
DataTable tb = new DataTable(TableName);
SqlConnection cn = new SqlConnection(connectString);
if (cn.State == ConnectionState.Closed) { cn.Open(); }
int ver = Convert.ToInt32(cn.ServerVersion.Substring(0,2));
string tsql = @"
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,
ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end
from [sysobjects] c
inner join [syscolumns] a on c.id=a.id
inner join [systypes] b on b.xtype=a.xtype
left join [sysproperties] d on a.colid=d.smallid and a.id=d.id
where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname'
order by a.colorder
";
if (ver > 8)
{
tsql = @"
select a.ColumnName,ColumnType=replace(a.ColumnType,'(-1)','(MAX)'),a.ColumnRemark,a.Type
from (
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,
ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end,a.colorder
from [sysobjects] c
inner join [syscolumns] a on c.id=a.id
inner join [systypes] b on b.xtype=a.xtype
left join [sys].[extended_properties] d on a.colid=d.[minor_id] and a.id=d.major_id
where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname'
) a
order by a.colorder
";
}
//sql2005以上
/*
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end from [sysobjects] c inner join [syscolumns] a on c.id=a.id inner join [systypes] b on b.xtype=a.xtype left join [sys].[extended_properties] d on a.colid=d.[minor_id] and a.id=d.major_id where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname' order by a.colorder
*/
SqlCommand cmd = new SqlCommand(tsql, cn);
cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = TableName;
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
public static DataTable getColumns2(string connectString, string TableName)
{
DataTable tb = new DataTable(TableName);
SqlConnection cn = new SqlConnection(connectString);
if (cn.State == ConnectionState.Closed) { cn.Open(); }
int ver = Convert.ToInt32(cn.ServerVersion.Substring(0, 2));
string tsql = @"
select ColRemark=d.value,ColName=a.name,ColType=b.name,
ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end
from [sysobjects] c
inner join [syscolumns] a on c.id=a.id
inner join [systypes] b on b.xtype=a.xtype
left join [sysproperties] d on a.colid=d.smallid and a.id=d.id
where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname'
order by a.colorder
";
if (ver > 8)
{
tsql = @"
select a.ColName,ColumnType=replace(a.ColumnType,'(-1)','(MAX)'),a.ColRemark,a.ColType
from (
select ColRemark=d.value,ColName=a.name,ColType=b.name,
ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end,a.colorder
from [sysobjects] c
inner join [syscolumns] a on c.id=a.id
inner join [systypes] b on b.xtype=a.xtype
left join [sys].[extended_properties] d on a.colid=d.[minor_id] and a.id=d.major_id
where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname'
) a
order by a.colorder
";
}
//sql2005以上
/*
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end from [sysobjects] c inner join [syscolumns] a on c.id=a.id inner join [systypes] b on b.xtype=a.xtype left join [sys].[extended_properties] d on a.colid=d.[minor_id] and a.id=d.major_id where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname' order by a.colorder
*/
SqlCommand cmd = new SqlCommand(tsql, cn);
cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = TableName;
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
public static DataTable getColumns(string connectString, string DataName, string TableName)
{
DataTable tb = new DataTable(TableName);
SqlConnection cn = new SqlConnection(connectString);
if (cn.State == ConnectionState.Closed) { cn.Open(); }
cn.ChangeDatabase(DataName);
int ver = Convert.ToInt32(cn.ServerVersion.Substring(0, 2));
string tsql = @"
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,
ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end
from [sysobjects] c
inner join [syscolumns] a on c.id=a.id
inner join [systypes] b on b.xtype=a.xtype
left join [sysproperties] d on a.colid=d.smallid and a.id=d.id
where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname'
order by a.colorder
";
if (ver > 8)
{
tsql = @"
select a.ColumnName,ColumnType=replace(a.ColumnType,'(-1)','(MAX)'),a.ColumnRemark,a.Type
from (
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,
ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end,a.colorder
from [sysobjects] c
inner join [syscolumns] a on c.id=a.id
inner join [systypes] b on b.xtype=a.xtype
left join [sys].[extended_properties] d on a.colid=d.[minor_id] and a.id=d.major_id
where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname'
) a
order by a.colorder
";
}
//sql2005以上
/*
select ColumnRemark=d.value,ColumnName=a.name,Type=b.name,ColumnType=case when b.name='varchar' or b.name='nvarchar' then b.name+'('+cast(a.Length as varchar(10))+')' when b.name='varchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(10))+'('+cast(a.Length as varchar(10))+')' end from [sysobjects] c inner join [syscolumns] a on c.id=a.id inner join [systypes] b on b.xtype=a.xtype left join [sys].[extended_properties] d on a.colid=d.[minor_id] and a.id=d.major_id where c.xtype='u' and c.name<>'dtproperties' and c.name=@TableName and b.name<>'sysname' order by a.colorder
*/
SqlCommand cmd = new SqlCommand(tsql, cn);
cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = TableName;
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tb);
cn.Close();
return tb;
}
#endregion
#region 判断列是否存在
public static int CheckColumnName(string connectString,string TableName, string ColName)
{
string tsql = @"
select name=case when b.name='varchar' or b.name='nvarchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+')' when b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(50)) end from syscolumns a
inner join [systypes] b on a.xtype=b.xtype
where a.id=object_id('" + TableName + @"') and a.name='" + ColName + @"'
";
Database db = new Database(connectString);
db.IsCheckKeyword = false;
DbCommand cmd = db.GetSqlStringCommand(tsql);
string a = Convert.ToString(db.ExecuteScalar(cmd));
if (string.IsNullOrEmpty(a) == true) return 0; //不存在
return 1; //存在
}
#endregion
#region 判断列是否存在
public static int CheckColumnName(string TableName, string ColName)
{
string tsql = @"
select name=case when b.name='varchar' or b.name='nvarchar' or b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+')' when b.name='decimal' then b.name+'('+cast(a.prec as varchar(10))+','+cast(a.xscale as varchar(10))+')' else cast(b.name as varchar(50)) end from syscolumns a
inner join [systypes] b on a.xtype=b.xtype
where a.id=object_id('" + TableName + @"') and a.name='" + ColName + @"'
";
Database db = DatabaseFactory.CreateDatabase();
db.IsCheckKeyword = false;
DbCommand cmd = db.GetSqlStringCommand(tsql);
string a = Convert.ToString(db.ExecuteScalar(cmd));
if (string.IsNullOrEmpty(a) == true) return 0; //不存在
return 1; //存在
}
#endregion
#region 执行SQL语句
public static void ExecuteNonQuery(string tsql)
{
if (string.IsNullOrEmpty(tsql) == true) return;
Database db = DatabaseFactory.CreateDatabase();
db.IsCheckKeyword = false;
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.ExecuteNonQuery(cmd);
}
#endregion
#region 获取数据库名
public static string GetDataBaseName(string connectString)
{
SqlConnection cn = new SqlConnection(connectString);
return cn.Database;
}
#endregion
#region 表去除自增列
public static void Set_Table_AutoIncrement_ON(string tbName)
{
if (string.IsNullOrEmpty(tbName) == true) return;
string tsql = "SET IDENTITY_INSERT ["+ tbName + "] ON";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.ExecuteNonQuery(cmd);
}
#endregion
#region 表恢复自增列
public static void Set_Table_AutoIncrement_OFF(string tbName)
{
if (string.IsNullOrEmpty(tbName) == true) return;
string tsql = "SET IDENTITY_INSERT [" + tbName + "] OFF";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.ExecuteNonQuery(cmd);
}
#endregion
#region "读取XML配置文件-属性值"
///
/// 返回指点节点指点属性的值,AttributeName属性名称
///
/// 配置文件路径
/// 属性名称
public static string LoadXml_Attribute(string filePath, string AttributeName, string NameSpace)
{ //读取属性值
if (System.IO.File.Exists(filePath) == false) return "";
XmlDocument Document = new XmlDocument();
Document.Load(filePath);
XmlNodeList nodelist = Document.SelectNodes("child::configuration/appSettings/add");
foreach (XmlNode node in nodelist)
{
if (node.Attributes["key"].Value == AttributeName && node.Attributes["NameSpace"].Value == NameSpace)
{
string KeyValue = "";
try
{
KeyValue = CryptoPrivate.Decryp(node.Attributes["value"].Value);
}
catch
{
KeyValue = node.Attributes["value"].Value;
}
return KeyValue;
}
}
return "";
}
#endregion
#region "保存XML配置文件"
///
///AttributeName属性名称,AttributeValue属性值
///
/// 属性名称
/// 属性值
/// 是否加密
public static void SaveXml_Attribute(string filePath, string AttributeName, string AttributeValue, string NameSpace, bool IsEncrypt)
{
bool bk = true;
XmlDocument Document = new XmlDocument();
Document.Load(filePath);
//XmlNodeList nodelist = Document.GetElementsByTagName(NodeTypeName);
XmlNodeList nodelist = Document.SelectNodes("child::configuration/appSettings/add");
foreach (XmlNode node in nodelist)
{
if (node.Attributes["NameSpace"] == null)
{
node.Attributes.Append(Document.CreateAttribute("NameSpace"));
}
if (node.Attributes["key"].Value == AttributeName && node.Attributes["NameSpace"].Value == NameSpace)
{
if (IsEncrypt == true) { node.Attributes["value"].Value = CryptoPrivate.Encryp(AttributeValue); }
else { node.Attributes["value"].Value = AttributeValue; }
bk = false;
}
}
if (bk == true)
{
XmlElement Element = Document.CreateElement("add");
XmlAttribute Attrib = null;
Attrib = Document.CreateAttribute("key");
Attrib.Value = AttributeName;
Element.Attributes.Append(Attrib);
Attrib = Document.CreateAttribute("value");
if (IsEncrypt == true) { Attrib.Value = CryptoPrivate.Encryp(AttributeValue); }
else { Attrib.Value = AttributeValue; }
Element.Attributes.Append(Attrib);
Attrib = Document.CreateAttribute("NameSpace");
Attrib.Value = NameSpace;
Element.Attributes.Append(Attrib);
nodelist[0].ParentNode.AppendChild(Element);
}
Document.Save(filePath);
}
#endregion
}
}