How to get Type of a column using DacFx api?

独自空忆成欢 提交于 2019-12-08 12:39:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!