如何使用 DacFx api 获取列的类型? [英] How to get Type of a column using DacFx api?

查看:20
本文介绍了如何使用 DacFx api 获取列的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SSDT 项目中,使用 DacFx API,我在数据库的内存模型中进行查询.我一直在尝试检索列的类型,但无法弄清楚.知道如何做到这一点吗?

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
                    }
                }
            }
        }

推荐答案

如果不使用强类型模型,那就是:

Without using the strongly-typed model, it'd be:

var dataType = col.GetReferenced(Column.DataType).First();
var dataTypeName = dataType.Name.Parts[0];

不过,强类型模型可以使这更容易.查看 https://github.com/Microsoft/DACExtensions/tree/master/DacFxStronglyTypedModel

The strongly-typed model can make this easier, though. Check out https://github.com/Microsoft/DACExtensions/tree/master/DacFxStronglyTypedModel

这篇关于如何使用 DacFx api 获取列的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆