using CnasSynchronousCommon;
using CnasSynchronusDAL;
using CnasSynchrousModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CNAS_DBSync
{
public class SelectTableType
{
private static string connectionStr = "";
public static DataTable MySqlsec(string strTableName)
{
MySQLDAL mysql = new MySQLDAL();
DataTable tb = mysql.GetTableTypeAndLenth(strTableName);
return tb;
}
public static DataTable Sqlserversec(string strTableName, SyncInstrumentItemInfo t)
{
DataTable dt = new DataTable();
try
{
if (t.SyncInstrumentDSInfo.Host != "")
connectionStr = $"Data Source = {t.SyncInstrumentDSInfo.Host}; Initial Catalog = {t.SyncInstrumentDSInfo.ServerName}; User Id = {t.SyncInstrumentDSInfo.UserId}; Password = {t.SyncInstrumentDSInfo.UserPwd};";
string sql = string.Format(@"SELECT
c.name AS ColumnName,
t.name AS DataType,
c.max_length AS MaxLength,
c.is_nullable AS IsNullable,
c.default_object_id AS DefaultObjectId,
ep.value AS remark
FROM
sys.columns c
JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT JOIN
sys.extended_properties ep
ON
c.object_id = ep.major_id
AND c.column_id = ep.minor_id
AND ep.name = 'MS_Description' -- 备注的属性名称
WHERE
c.object_id = OBJECT_ID('{0}'); ", strTableName); //查询字符串
dt = GetDataTable(sql, new SqlParameter[] { });
}
catch (Exception ex)
{
//发生异常,写入日志
AppLog.Error(ex.Message);
}
return dt;
}
///
/// 查询操作
///
///
///
public static DataTable GetDataTable(string sql, params SqlParameter[] sp)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
conn.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
{
sda.SelectCommand.Parameters.AddRange(sp);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
public static DataTable PostgreSql(string strTableName)
{
DataTable dt = new DataTable();
string strSql = string.Format(@"SELECT
a.attname as ColumnName,
format_type(a.atttypid, a.atttypmod) as DataType,
a.attnotnull as 非空,
col_description(a.attrelid, a.attnum) as remark
FROM
pg_class as c, pg_attribute as a
where
a.attrelid = c.oid
and
a.attnum > 0
and
c.relname = '{0}'; ", strTableName);
try
{
dt = PostgreSQLHelper.ExecuteDataSet(strSql).Tables[0];
}
catch (Exception ex)
{
AppLog.Error(ex.Message);
}
return dt;
}
public static DataTable DmSql(string strTableName)
{
DataTable dt = new DataTable();
string strSql = string.Format(@"SELECT
a.COLUMN_NAME AS ColumnName,
b.COMMENTS AS remark,
a.DATA_TYPE || CASE
WHEN a.DATA_TYPE IN('VARCHAR', 'CHAR') THEN '(' || a.DATA_LENGTH || ')'
WHEN a.DATA_TYPE IN('DECIMAL', 'NUMERIC') THEN '(' || a.DATA_PRECISION || ',' || a.DATA_SCALE || ')'
ELSE ''
END AS DataType
FROM
ALL_TAB_COLUMNS a
LEFT JOIN
ALL_COL_COMMENTS b
ON a.OWNER = b.OWNER
AND a.TABLE_NAME = b.TABLE_NAME
AND a.COLUMN_NAME = b.COLUMN_NAME
WHERE
a.TABLE_NAME = '{0}'
ORDER BY
a.COLUMN_ID;", strTableName);
try
{
dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
}
catch (Exception ex)
{
AppLog.Error(ex.Message);
}
return dt;
}
}
}