Files
lserp_cs_5.0/插件库/Zhaizj.Framework/Data/DbChecker/SQLServerDatabaseChecker.cs
T
2026-07-10 15:25:05 +08:00

68 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using Zhaizj.Framework.ORM;
namespace Zhaizj.Framework.Data {
internal class SQLServerDatabaseChecker : IDatabaseChecker {
private static readonly ILog logger = LogManager.GetLogger( typeof( SQLServerDatabaseChecker ) );
private String _connectionString;
private DatabaseType _databaseType;
public String ConnectionString {
get { return _connectionString; }
set { _connectionString = value; }
}
public DatabaseType DatabaseType {
get { return _databaseType; }
set { _databaseType = value; }
}
private List<String> existTables = new List<String>();
public void CheckDatabase() {
if (strUtil.IsNullOrEmpty( _connectionString )) {
throw new Exception( "[sqlserver] connection String is not found" );
}
IDatabaseDialect dialect = DataFactory.GetDialect( DatabaseType.SqlServer );
if (strUtil.IsNullOrEmpty( dialect.GetConnectionItem( _connectionString, ConnectionItemType.Server ) )) {
throw new Exception( "[sqlserver] address is empty" );
}
if (strUtil.IsNullOrEmpty( dialect.GetConnectionItem( _connectionString, ConnectionItemType.Database ) )) {
throw new Exception( "[sqlserver] database is empty" );
}
}
public void CheckTable( MappingClass mapping, String db ) {
logger.Info( "[sqlserver] begin check table" );
SqlConnection connection = new SqlConnection( _connectionString );
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "SELECT OBJECT_NAME(id) as name FROM sysobjects WHERE xtype='U' AND OBJECTPROPERTY(id, 'IsMSShipped') = 0";
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
existTables.Add( reader["name"].ToString() );
logger.Info( "table found" + reader["name"].ToString() );
}
reader.Close();
existTables = new SqlServerTableBuilder().CheckMappingTableIsExist( cmd, db, existTables, mapping );
connection.Close();
}
public List<String> GetTables() {
return existTables;
}
}
}