问题
In SSDT project, using DacFx API, I am querying in memory model of a database. I have been trying to retrieve Type of a column, but cannot figure it out. Any idea how this could be done?
private void TestMethod()
{
using(TSqlModel model =
new TSqlModel(SqlServerVersion.Sql110, new TSqlModelOptions {}))
{
string[] scripts = new[]
{
"CREATE TABLE t1 (c1 NVARCHAR(30) NOT NULL)",
"CREATE TABLE t2 (c2 INT NOT NULL)"
};
foreach (string script in scripts)
{
model.AddObjects(script);
}
var tables = model.GetObjects(DacQueryScopes.All, Table.TypeClass);
var cols = tables.First().GetReferenced(Table.Columns);
foreach (TSqlObject col in cols)
{
//?? how can I get the data type of the column?
string dataType = col.??;
if (dataType == "int")
{
//my thing here
}
}
}
}
回答1:
Without using the strongly-typed model, it'd be:
var dataType = col.GetReferenced(Column.DataType).First();
var dataTypeName = dataType.Name.Parts[0];
The strongly-typed model can make this easier, though. Check out https://github.com/Microsoft/DACExtensions/tree/master/DacFxStronglyTypedModel
回答2:
The correct code should be:
string dataType = col.DataType;
来源:https://stackoverflow.com/questions/44058693/how-to-get-type-of-a-column-using-dacfx-api