我们正在将dbproj升级到sqlproj,以便我们将其指向一个新的sql 2012数据库.我们目前有一个程序,读取.dbschema xml文件以查找所有表和列,并从中检索信息.我们使用这些数据来构建我们自己的自定义类.
新的sqlproj文件现在可以生成一个dacpac,我们想要进行调整,以便获取我们需要的数据.我写了以下内容来尝试遍历dacpac并获取我需要的信息:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.sqlServer.Dac; using Microsoft.sqlServer.Dac.Extensions; using Microsoft.sqlServer.Dac.Model; namespace DacPacReader { class Program { static void Main(string[] args) { using (System.IO.TextWriter writter = new System.IO.StreamWriter(@"c:\temp\output.txt")) { using (TsqlModel model = new TsqlModel(@"C:\temp\Data.dacpac")) { var allTables = model.GetObjects(DacQueryScopes.All,ModelSchema.Table); foreach (var table in allTables) { writter.WriteLine(table.Name); foreach (var column in table.GetChildren().Where(child => child.ObjectType.Name == "Column")) { writter.WriteLine("\t" + column.Name); writter.WriteLine("\tProperties:"); foreach (var property in column.ObjectType.Properties) { writter.WriteLine("\t\t" + property.Name + "\t\t" + property.DataType.FullName); } writter.WriteLine("\tMetadata:"); foreach (var MetaData in column.ObjectType.Metadata) { writter.WriteLine("\t\t" + MetaData.Name + "\t\t" + MetaData.DataType.FullName); } } } } } } } }
我不知道我是做正确的方法,还是有一个更好/更简单的方法.我不确定在Google / S.E上搜索什么.并没有找到任何例子.
我可以看到变量列有一个名为ContextObject的非公共成员,它是一个Microsoft.Data.Tools.Schema.sql.SchemaModel.sqlSimpleColumn.如果我可以访问这个对象,那么我可以把我需要的所有信息拉出来.表也有一个类似的ContextObject,它可以帮助我.
无论如何,目前,这将打开dacpac并检索所有的表和列名称.我得到的数据的一个例子是:
[dbo].[User] [dbo].[User].[UserID] Properties: Collation System.String IsIdentityNotForReplication System.Boolean Nullable System.Boolean IsRowGuidCol System.Boolean Sparse System.Boolean Expression Microsoft.Data.Tools.Schema.sql.SchemaModel.sqlScriptProperty Persisted System.Boolean PersistedNullable System.Nullable`1[[System.Boolean,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]] Scale System.Int32 Precision System.Int32 Length System.Int32 IsMax System.Boolean XmlStyle Microsoft.sqlServer.Dac.Model.XmlStyle IdentityIncrement System.String IdentitySeed System.String IsFileStream System.Boolean IsIdentity System.Boolean Metadata: ColumnType Microsoft.sqlServer.Dac.Model.ColumnType
基本上,我想做以下之一:
>访问ContextObject以获取Microsoft.Data.Tools.Schema.sql.SchemaModel.*对象
要么
>从ObjectType属性获取属性和元数据的值
要么
>从头开始,用一种更简单的方法来获取这些信息
我们需要获取信息,例如列类型,如何可空,以及列的比例和精度
解决方法@H_301_26@
因此,在查询模型时可以使用一组完全定义的元数据类.这比依靠Linq简单,需要测试每个属性的字符串名称.以
Table和
Column类为例.我更新了您的示例,显示如何使用它们:
// Query for UserDefined objects to just filter to your own objects. All will
// include system objects (references to objects in master.dacpac if you reference that
// and BuiltIn objects such as the data types. You probably don't care about those
var allTables = model.GetObjects(DacQueryScopes.UserDefined,Table.TypeClass);
foreach (var table in allTables)
{
writter.WriteLine(table.Name);
// Columns are referenced by tables,so GetReferenced can be used. The GetChildren can also be used
// but filtering by comparing "child.ObjectType == Column.TypeClass" would simplify your example
foreach (var column in table.GetReferenced(Table.Columns))
{
// Now you can use the Column Metadata class's properties to query your Column object
bool isNullable = column.GetProperty<bool>(Column.Nullable);
sqlDataType sdt = column.GetReferenced(Column.DataType).First().GetProperty<sqlDataType>(DataType.sqlDataType);
}
// Query for UserDefined objects to just filter to your own objects. All will // include system objects (references to objects in master.dacpac if you reference that // and BuiltIn objects such as the data types. You probably don't care about those var allTables = model.GetObjects(DacQueryScopes.UserDefined,Table.TypeClass); foreach (var table in allTables) { writter.WriteLine(table.Name); // Columns are referenced by tables,so GetReferenced can be used. The GetChildren can also be used // but filtering by comparing "child.ObjectType == Column.TypeClass" would simplify your example foreach (var column in table.GetReferenced(Table.Columns)) { // Now you can use the Column Metadata class's properties to query your Column object bool isNullable = column.GetProperty<bool>(Column.Nullable); sqlDataType sdt = column.GetReferenced(Column.DataType).First().GetProperty<sqlDataType>(DataType.sqlDataType); }