|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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;
-
- }
-
- /// <summary>
- /// 查询操作
- /// </summary>
- /// <param name="sql"></param>
- /// <returns></returns>
- 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
- c.COLUMN_NAME AS ColumnName,
- c.DATA_TYPE AS DataType,
- c.DATA_LENGTH AS 字段长度,
- c.NULLABLE AS 是否允许为空,
- com.COMMENTS AS remark
- FROM
- USER_TAB_COLUMNS c
- LEFT JOIN
- USER_COL_COMMENTS com
- ON
- c.TABLE_NAME = com.TABLE_NAME
- AND c.COLUMN_NAME = com.COLUMN_NAME
- WHERE
- c.TABLE_NAME = '{0}';", strTableName);
- try
- {
- dt = DamengHelper.ExecuteDataSet(strSql).Tables[0];
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- }
- return dt;
-
-
- }
- }
- }
|