ado.net CommandBehavior.KeyInfo / getSchema 方法

发布于 2024-11-09 17:51:31 字数 515 浏览 0 评论 0原文

在 C# asp.net vs2008 上工作。使用 Reader = Command.ExecuteReader(CommandBehavior.KeyInfo) 我尝试获取外键,有没有办法获取它? 如果我写

     string sql = string.Format("Select * from {0}", tableName);
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.CommandType = CommandType.Text;
    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

    DataTable schema = reader.GetSchemaTable();

然后在 isKey=true 设置主键。我想从这个命令获取外键。有什么方法可以获取它。 如果有任何疑问请询问。提前致谢。

Work on C# asp.net vs2008. with Reader = Command.ExecuteReader(CommandBehavior.KeyInfo) i try to get foreign keys , is there a way to get it ?
if i write

     string sql = string.Format("Select * from {0}", tableName);
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.CommandType = CommandType.Text;
    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

    DataTable schema = reader.GetSchemaTable();

Then on isKey=true set for primary key.I want to get foreign key from this command.Is there any way to get it.
If have any query plz ask.Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-11-16 17:51:31

如果您有管理权限。您可以从以下查询中获取结果。

SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
Where FK.TABLE_NAME = YourtableName

您可以在后面的代码中使用以下代码

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Connection String");
            con.Open();

            string str = "SELECT ";
            str+= " K_Table = FK.TABLE_NAME,";
            str+= " FK_Column = CU.COLUMN_NAME,";
            str+= " PK_Table = PK.TABLE_NAME,";
            str+= " PK_Column = PT.COLUMN_NAME,";
            str+= " Constraint_Name = C.CONSTRAINT_NAME";
            str+= " FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C";
            str+= " INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME";
            str+= " INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME";

            str+= " INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME";
            str+= " INNER JOIN (";
            str+= " SELECT i1.TABLE_NAME, i2.COLUMN_NAME";
            str+= " FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1";
            str+= " INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME";
            str+= " WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'";
            str+= " ) PT ON PT.TABLE_NAME = PK.TABLE_NAME";
            str+= " Where FK.TABLE_NAME = 'f'";

            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, con);
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

            }

If you have the administrative Priviliges. You can get the result from the below query.

SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
Where FK.TABLE_NAME = YourtableName

You can use the below code in your code behind

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Connection String");
            con.Open();

            string str = "SELECT ";
            str+= " K_Table = FK.TABLE_NAME,";
            str+= " FK_Column = CU.COLUMN_NAME,";
            str+= " PK_Table = PK.TABLE_NAME,";
            str+= " PK_Column = PT.COLUMN_NAME,";
            str+= " Constraint_Name = C.CONSTRAINT_NAME";
            str+= " FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C";
            str+= " INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME";
            str+= " INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME";

            str+= " INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME";
            str+= " INNER JOIN (";
            str+= " SELECT i1.TABLE_NAME, i2.COLUMN_NAME";
            str+= " FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1";
            str+= " INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME";
            str+= " WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'";
            str+= " ) PT ON PT.TABLE_NAME = PK.TABLE_NAME";
            str+= " Where FK.TABLE_NAME = 'f'";

            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, con);
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

            }
我最亲爱的 2024-11-16 17:51:31

ADO.NET 当前不从数据源返回外键信息。 CommandBehavior.KeyInfo 仅指主键信息。

http://support.microsoft.com/kb/310107

使用此::

**选择

fk。 CONSTRAINT_NAME、fk.UNIQUE_CONSTRAINT_NAME、cn.TABLE_NAME、cn.COLUMN_NAME

INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS中的

作为 fk

join INFORMATION_SCHEMA.TABLE_CONSTRAINTS 作为 pk

上的

pk.CONSTRAINT_NAME=fk.UNIQUE_CONSTRAINT_NAME

将 INFORMATION_SCHEMA.KEY_COLUMN_USAGE 作为 cn 加入到

cn.CONSTRAINT_NAME=fk.CONSTRAINT_NAME 上

,其中 cn.TABLE_NAME='tablename'**

ADO.NET does not currently return foreign key information from a data source. CommandBehavior.KeyInfo refers to primary key information only.

http://support.microsoft.com/kb/310107

Use this::

**select

fk.CONSTRAINT_NAME,fk.UNIQUE_CONSTRAINT_NAME, cn.TABLE_NAME,cn.COLUMN_NAME

from

INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as fk

join INFORMATION_SCHEMA.TABLE_CONSTRAINTS as pk

on

pk.CONSTRAINT_NAME=fk.UNIQUE_CONSTRAINT_NAME

join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as cn

on cn.CONSTRAINT_NAME=fk.CONSTRAINT_NAME

where cn.TABLE_NAME='tablename'**

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文