【问题标题】:Determining a SQL Server Identity Column Name in .NET在 .NET 中确定 SQL Server 标识列名称
【发布时间】:2009-07-22 05:14:03
【问题描述】:

我正在尝试在 VB.NET 中编写一些通用代码,以确定 SQL Server 数据库表是否包含标识列,如果包含,则返回该列的名称。

我正在使用 Visual Basic 2008 Express 并创建了一个 SQL 数据库“MyDatabase”,其中包含一个名为“MyTable”的表。在该表中,我有 3 列,“ID”、“Column1”和“Column2”。我知道,我知道……有创意的名字。在 Database Explorer 的列属性下,我已将“ID”列“Identity Specification”设置为“yes”,并将“Is Identity”值设置为“yes”。

我需要 .NET 代码返回“ID”作为标识列。这可以通过 LINQ 或其他方式完成吗?

提前非常感谢!

运气

【问题讨论】:

    标签: .net sql-server vb.net linq linq-to-sql


    【解决方案1】:

    使用 SQLDataReader 打开 MyTable (SELECT ID from myTable WHERE 1 = 2) 并使用 GetSchemaTable 方法,这将为您提供数据表。

    根据文档,检查名为 IsAutoIncrement 的列的内容。如果它返回 true,它应该是一个 Identity 列。

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable.aspx

    注意:这纯粹是基于我的记忆和 MSDN 文档的帮助。

    【讨论】:

      【解决方案2】:

      如果你有Linq to SQL DataContext,可以通过查询数据上下文MappingSource获取表元数据:

      var ctx = new YourDataContext();
      var identityAndKey = ctx.Mapping.MappingSource
                                      .GetModel(typeof(YourDataContext)) 
                                      .GetMetaType(typeof(YourTable))
                                      .DataMembers
                                      .SingleOrDefault(i => i.IsDbGenerated &&
                                                            i.IsPrimaryKey);
      

      您将获得一个 MetaDataMember 实例,它是您的数据类的主键和 DbGenerated (Identity) 属性。列名在 Name 属性中可用。

      稍加思考,您就可以将这段代码重新用于任何表/数据上下文。

      【讨论】:

        【解决方案3】:

        您可以调用 sp_columns 存储过程来获取此信息以及更多信息:

        SqlConnection sqlConx = new SqlConnection(p_conectionStrWithDB);
        sqlConx.Open();
        SqlCommand cmd = new SqlCommand("sp_columns", sqlConx);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@table_name", TableName));
        DataTable tblColumns = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(tblColumns);
        

        编辑:您还可以像这样获取有关主键的信息:

        SqlConnection sqlConx = new SqlConnection(p_conectionStrWithDB);
        sqlConx.Open();
        SqlCommand cmd = new SqlCommand("sp_pkeys", sqlConx);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@table_name", tableName));
        DataTable tblConstraints = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(tblConstraints);
        return tblConstraints;
        

        【讨论】:

        • 谢谢大家的回答。乔纳森 - 我已经运行了你的代码,可以看到很多列信息,包括 DATA_TYPE、RADIX、NULLABLE、SQL_DATA_TYPE 和 SS_DATA_TYPE,但我仍然没有看到有关主键的任何信息。请原谅这个看起来非常简单的问题。我才刚刚开始使用 SQL。再次感谢,幸运
        • 这段代码让你知道什么列是Identity,你可以读取数据表的“TYPE_NAME”列。如果列类型是 INT 并且是标识,则该列将包含“int Identity”。我将编辑我的答案以添加有关获取主键信息的代码。
        • 非常感谢。这正是我所需要的。干杯,好运
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多