Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用SqlConnection.GetSchema()获取列主键约束_C#_Stored Procedures_Ado.net_Sql Server 2000_Database Schema - Fatal编程技术网

C# 如何使用SqlConnection.GetSchema()获取列主键约束

C# 如何使用SqlConnection.GetSchema()获取列主键约束,c#,stored-procedures,ado.net,sql-server-2000,database-schema,C#,Stored Procedures,Ado.net,Sql Server 2000,Database Schema,我有一些ADO.NET代码来动态检测数据库模式,我需要的是如何使用SqlConnection上的GetSchema方法来获取唯一的列约束和主键约束。这是我的代码: conn.Open(); SqlCommand mSqlCommand = new SqlCommand("sp_pkeys", conn); mSqlCommand.CommandType = CommandType.StoredProcedure; mSqlCommand.Parameters.Add( "@ta

我有一些ADO.NET代码来动态检测数据库模式,我需要的是如何使用
SqlConnection
上的
GetSchema
方法来获取唯一的列约束和主键约束。这是我的代码:

conn.Open();     
SqlCommand mSqlCommand = new SqlCommand("sp_pkeys", conn);
mSqlCommand.CommandType = CommandType.StoredProcedure;
mSqlCommand.Parameters.Add(
    "@table_name", SqlDbType.NVarChar).Value = tableName;
SqlDataReader mReader = mSqlCommand.ExecuteReader(
    (CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly));
//ExecuteReader();
DataTable schema = mReader.GetSchemaTable();
mReader.Close();
conn.Close();

呼叫中没有任何内容可以让您了解这一点

您似乎可以使用
IsKey
列值,对于有助于唯一标识表中记录的任何内容,该列值都应返回true。但是,从
IsKey
列的文档中(重点):

true:该列是一组 行集中的列,以 一起,唯一地标识该行。 IsKey设置为的列集 true必须唯一标识中的行 划船组。没有要求 这组列是最小值 一组列。这组列 可以从基表生成 主键、唯一约束或 唯一索引

因此,您不能保证它本身就是主键

现在,如果您只需要一些东西来唯一地标识行,那么
IsKey
就可以了,因为主键并不总是唯一标识行的方式(例如,您可以使用具有唯一索引的自然标识符)。即使您有一个主键和与其他列的唯一索引,组合中所有这些列的值也将始终是唯一的


但是,如果您特别需要查看组成主键的列,那么
GetSchemaTable
将不会提供您需要的信息。相反,您可以只调用系统存储过程来查找有助于生成主键的列的名称。

如果仍然有人需要解决方案,我已经为此创建了一个函数,Sqlcommand包含希望获取架构信息的语句

Public Shared Function TableFromCommand(ByVal Command As SqlCommand) As DataTable

    Dim Cn As SqlConnection = Nothing
    Dim Dt As DataTable
    Dim Dr As SqlDataReader
    Dim Column As DataColumn
    Dim Answer As New DataTable

    Try
        Answer.TableName = "SearchTable"
        Cn = New SqlConnection("Your connection string")
        Cn.Open()

        Command.Connection = Cn
        For Each Prm As SqlParameter In Command.Parameters
            If Prm.Direction = ParameterDirection.Input _
            OrElse Prm.Direction = ParameterDirection.InputOutput Then
                Prm.Value = DBNull.Value
            End If
        Next

        Dr = Command.ExecuteReader(CommandBehavior.SchemaOnly Or CommandBehavior.KeyInfo)
        Dt = Dr.GetSchemaTable
        Dim Keys As New List(Of DataColumn)
        Dim ColumnsDic As New SortedDictionary(Of Integer, DataColumn)

        For Each Row As DataRow In Dt.Rows
            Column = New DataColumn
            With Column
                .ColumnName = Row("ColumnName").ToString
                .DataType = Type.GetType(Row("DataType").ToString)
                .AllowDBNull = CBool(Row("AllowDBNull"))
                .Unique = CBool(Row("IsUnique"))
                .ReadOnly = CBool(Row("IsReadOnly"))

                If Type.GetType(Row("DataType").ToString) Is GetType(String) Then
                    .MaxLength = CInt(Row("ColumnSize"))
                End If

                If CBool(Row("IsIdentity")) = True Then
                    .AutoIncrement = True
                    .AutoIncrementSeed = -1
                    .AutoIncrementStep = -1
                End If

                If CBool(Row("IsKey")) = True Then
                    Keys.Add(Column)
                End If
            End With

            ColumnsDic.Add(CInt(Row("ColumnOrdinal")), Column)

            Answer.Columns.Add(Column)
        Next

        If Keys.Count > 0 Then
            Answer.Constraints.Add("PrimaryKey", Keys.ToArray, True)
        End If
    Catch ex As Exception
        MyError.Show(ex)
    Finally
        If Cn IsNot Nothing AndAlso Not Cn.State = ConnectionState.Closed Then
            Cn.Close()
        End If
    End Try

    Return Answer

End Function

在SqlConnection上调用GetSchema()怎么样?使用
collectionName=“IndexColumns”
和架构限制列表,您可以使用GetSchema()请求所需的信息

见:

一旦我使用数据库名称建立了SqlConnection,以下内容对我起到了作用:

var connectionString = 
    string.Format("Server=.\\SQLEXPRESS;Database={0};Trusted_Connection=true", dbName);
using (var sqlConnection = new SqlConnection(connectionString))
{
    sqlConnection.Open();
    DataTable tables = sqlConnection.GetSchema("Tables");
    foreach (DataRow tablesRow in tables.Rows)
    {
        string tableName = tablesRow["table_name"].ToString();
        Console.WriteLine(tableName);
        var indexCols = sqlConnection.GetSchema("IndexColumns",
            new string[] {dbName, null, tableName, "PK_" + tableName, null});
        foreach (DataRow indexColsRow in indexCols.Rows)
            Console.WriteLine("  PK: {0}", indexColsRow["column_name"]);
    }
}

您可以获取
primaryKeys
UniqueKeys
ForeignKeys
以及此命令返回的数据表中列出的任何其他架构:
“connection.GetSchema(“MetaDataCollections”)”

下面是返回PrimaryKey和UniqueKey(键名和列名)的代码

查看所有文档

public void Dotransfer()
{
var sourceSchema=new TableSchema(SourceConnectionString);
}
公共类表模式
{
公共表模式(字符串连接字符串)
{
this.TableList=新列表();
this.ColumnList=新列表();
this.PrimaryKeyList=新列表();
this.ForeignKeyList=新列表();
this.UniqueKeyList=新列表();
GetDataBaseSchema(connectionString);
}
公共列表TableList{get;set;}
公共列表列列表{get;set;}
公共列表PrimaryKeyList{get;set;}
公共列表UniqueKeyList{get;set;}
公共列表ForeignKeyList{get;set;}
受保护的void GetDataBaseSchema(字符串连接字符串)
{
使用(SqlConnection连接=新的SqlConnection(ConnectionString))
{
System.Data.SqlClient.SqlConnectionStringBuilder builder=new System.Data.SqlClient.SqlConnectionStringBuilder();
builder.ConnectionString=连接字符串;
字符串服务器=builder.DataSource;
字符串数据库=builder.InitialCatalog;
connection.Open();
DataTable schemaTables=connection.GetSchema(“表”);
foreach(schemaTables.Rows中的System.Data.DataRow行表)
{
String tableName=rowTable.ItemArray[2].ToString();
this.TableList.Add(tableName);
string[]restrictionsColumns=新字符串[4];
restrictionsColumns[2]=表名;
DataTable schemaColumns=connection.GetSchema(“Columns”,restrictionsColumns);
foreach(schemaColumns.Rows中的System.Data.DataRow rowColumn)
{
string ColumnName=rowColumn[3]。ToString();
this.ColumnList.Add(新列(){TableName=TableName,FieldName=ColumnName});
}
string[]restrictionsPrimaryKey=新字符串[4];
restrictionsPrimaryKey[2]=表名;
DataTable schemaPrimaryKey=connection.GetSchema(“IndexColumns”,restrictionsColumns);
foreach(schemaPrimaryKey.Rows中的System.Data.DataRow rowPrimaryKey)
{
string indexName=rowPrimaryKey[2].ToString();
if(indexName.IndexOf(“PK”)!=-1)
{
this.PrimaryKeyList.Add(newprimarykey())
{
TableName=TableName,
FieldName=rowPrimaryKey[6]。ToString(),
PrimaryKeyName=indexName
});
}
if(indexName.IndexOf(“UQ”)!=-1)
{
this.UniqueKeyList.Add(newuniquekey()
{
TableName=TableName,
FieldName=rowPrimaryKey[6]。ToString(),
单曲
    public void Dotransfer()
    {
        var sourceSchema = new TableSchema(SourceConnectionString);

    }



  public class TableSchema
    {
        public TableSchema(string connectionString)
        {
            this.TableList = new List<string>();
            this.ColumnList = new List<Columns>();
            this.PrimaryKeyList = new List<PrimaryKey>();
            this.ForeignKeyList = new List<ForeignKey>();
            this.UniqueKeyList = new List<UniqueKey>();

            GetDataBaseSchema(connectionString);

        }

        public List<string> TableList { get; set; }
        public List<Columns> ColumnList { get; set; }
        public List<PrimaryKey> PrimaryKeyList { get; set; }
        public List<UniqueKey> UniqueKeyList { get; set; }
        public List<ForeignKey> ForeignKeyList { get; set; }


        protected void GetDataBaseSchema(string ConnectionString)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {

                System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
                builder.ConnectionString = ConnectionString;
                string server = builder.DataSource;
                string database = builder.InitialCatalog;

                connection.Open();


                DataTable schemaTables = connection.GetSchema("Tables");

                foreach (System.Data.DataRow rowTable in schemaTables.Rows)
                {
                    String tableName = rowTable.ItemArray[2].ToString();
                    this.TableList.Add(tableName);

                    string[] restrictionsColumns = new string[4];
                    restrictionsColumns[2] = tableName;
                    DataTable schemaColumns = connection.GetSchema("Columns", restrictionsColumns);

                    foreach (System.Data.DataRow rowColumn in schemaColumns.Rows)
                    {
                        string ColumnName = rowColumn[3].ToString();
                        this.ColumnList.Add(new Columns(){TableName= tableName, FieldName = ColumnName});
                    }

                    string[] restrictionsPrimaryKey = new string[4];
                    restrictionsPrimaryKey[2] = tableName;
                    DataTable schemaPrimaryKey = connection.GetSchema("IndexColumns", restrictionsColumns);


                    foreach (System.Data.DataRow rowPrimaryKey in schemaPrimaryKey.Rows)
                    {
                        string indexName = rowPrimaryKey[2].ToString();

                        if (indexName.IndexOf("PK_") != -1)
                        {
                            this.PrimaryKeyList.Add(new PrimaryKey()
                            {
                                TableName = tableName,
                                FieldName = rowPrimaryKey[6].ToString(),
                                PrimaryKeyName = indexName
                            });
                        }

                        if (indexName.IndexOf("UQ_") != -1)
                        {
                            this.UniqueKeyList.Add(new UniqueKey()
                            {
                                TableName = tableName,
                                FieldName = rowPrimaryKey[6].ToString(),
                                UniqueKeyName = indexName
                            });
                        }

                    }


                    string[] restrictionsForeignKeys = new string[4];
                    restrictionsForeignKeys[2] = tableName;
                    DataTable schemaForeignKeys = connection.GetSchema("ForeignKeys", restrictionsColumns);


                    foreach (System.Data.DataRow rowFK in schemaForeignKeys.Rows)
                    {

                        this.ForeignKeyList.Add(new ForeignKey()
                        {
                            ForeignName = rowFK[2].ToString(),
                            TableName = tableName,
                            // FieldName = rowFK[6].ToString() //There is no information
                        });                
                    }


                }


            }

        }

    }    

    public class Columns
    {
        public string TableName { get; set; }
        public string FieldName { get; set; }
    }

    public class PrimaryKey
    {
        public string TableName { get; set; }
        public string PrimaryKeyName { get; set; }
        public string FieldName { get; set; }
    }


    public class UniqueKey
    {
        public string TableName { get; set; }
        public string UniqueKeyName { get; set; }
        public string FieldName { get; set; }
    }

    public class ForeignKey
    {
        public string TableName { get; set; }
        public string ForeignName { get; set; }
       // public string FieldName { get; set; } //There is no information
    }