public List<Table> GetDatabaseSheme(string connectionString) { var list = new List<Table>(); using (var connection = new SqlConnection(connectionString)) { string databaseName = Replace(connection.Database); connection.Open(); using (var command = new SqlCommand(_scheme, connection)) { using (SqlDataReader sqlDataReader = command.ExecuteReader()) { while (sqlDataReader.Read()) { Table table = new Table(); table.TableName = sqlDataReader[0].ToString(); table.DatabaseName = databaseName; table.DatabaseRealName = connection.Database; table.TemplateName = Replace(databaseName) + "_" + table.TableName + "_FetchEntityByIdentity"; //table.PK = GetPk(connection, table.TableName); table.ColumnNames = string.Join(",", GetAllColumns(connectionString, table.TableName).ToArray()); if (SkipSomeTables(table)) { continue; } list.Add(table); } } } foreach (var table in list) { using (var command = new SqlCommand(string.Format(_pkSql, table.TableName), connection)) { using (SqlDataReader sqlDataReader = command.ExecuteReader()) { Console.WriteLine(table.TemplateName); while (sqlDataReader.Read()) { table.PK = sqlDataReader[0].ToString(); table.TableName = FixName.Fix(table.DatabaseName, table.TableName);//此处做替换 } } } } } return list; }
private bool SkipSomeTables(Table table) { if (table.TableName.StartsWith("MS")) { return true; } if (table.TableName.StartsWith("sys")) return true; if (table.TableName.Equals("sysarticles")) return true; return false; }