gpt4 book ai didi

c# - 如何获取表的PRIMARY KEY列名

转载 作者:太空狗 更新时间:2023-10-29 22:14:00 25 4
gpt4 key购买 nike

我需要获取主键列名称。我在一个名为 _lstview_item

的变量中有我的表的名称

直到现在我尝试像这样获取列名

 string sql = "SELECT ColumnName = col.column_name" +
"FROM information_schema.table_constraints tc" +
"INNER JOIN information_schema.key_column_usage col" +
"ON col.Constraint_Name = tc.Constraint_Name" +
"AND col.Constraint_schema = tc.Constraint_schema" +
"WHERE tc.Constraint_Type = 'Primary Key'" +
"AND col.Table_name = " +_lstview_item+ "";

SqlConnection conn2 = new SqlConnection(cc.connectionString(cmb_dblist.Text));
SqlCommand cmd_server2 = new SqlCommand(sql);
cmd_server2.CommandType = CommandType.Text;
cmd_server2.Connection = conn2;
conn2.Open();
string ColumnName = (string)cmd_server2.ExecuteScalar();
conn2.Close();

没有任何成功。帮助?

最佳答案

这应该是您的查询。您的表名中缺少单引号。已经过测试并且工作正常。

string sql = "SELECT ColumnName = col.column_name 
FROM information_schema.table_constraints tc
INNER JOIN information_schema.key_column_usage col
ON col.Constraint_Name = tc.Constraint_Name
AND col.Constraint_schema = tc.Constraint_schema
WHERE tc.Constraint_Type = 'Primary Key' AND col.Table_name = '" + _lstview_item + "'";

关于c# - 如何获取表的PRIMARY KEY列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com