在C#的列表框中显示除PRIMARY KEY之外的所有列 [英] Display all columns except the PRIMARY KEY in a listbox in C#
问题描述
我希望在C#的列表框中显示除PRIMARY KEY之外的所有列列名。我该怎么做?
我的代码给出了以下错误:
无效的列名'column_type'。
请帮助。我正在使用MS-SQL。
我尝试过:
I want to display all the columns column names except the PRIMARY KEY in a listbox in C#. How do I do that?
My code gives me the following error:
Invalid column name 'column_type'.
Please Help. I am using MS-SQL for the same.
What I have tried:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string cmdstr = @"select * from information_schema.columns where table_name = '" + comboBox1.SelectedItem + "' and column_type <> 'PRI'";
string conStr = @"Data Source=INPDDBA027\NGEP;Initial Catalog=Dev_Server;Integrated Security=True";
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmdstr, conStr);
sda.Fill(dt);
listBox2.DataSource = dt;
listBox2.DisplayMember = "Column_Name";
}
推荐答案
试试这个
try this
select COLUMN_NAME from information_schema.columns where table_name = 'YourTableName'
except
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name
WHERE tc.CONSTRAINT_TYPE = 'Primary Key' and tc.table_name = 'YourTableName'
注意:您的代码是< span style =color:red>易受攻击到 SQL注入 [ ^ ]攻击
始终使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]
Note: Your code is Vulnerable to SQL injection [^] attacks
Always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
Column_Type不是您在SQL中访问的表中的有效字段
尝试
Column_Type is not a valid field in the table you are accessing in SQL
Try
SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name
WHERE tc.CONSTRAINT_TYPE = 'Primary Key'
获取哪些列是主键并连接到您所指的表。
to get which columns that are primary keys and join to the table you are referring to.
这篇关于在C#的列表框中显示除PRIMARY KEY之外的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!