[英]Invalid Column Name | C#
I am trying to figure out how to collect data from a database with c#.我想弄清楚如何使用 c# 从数据库中收集数据。 I am stuck on a SQL command, and I can't really figure out how to fix it.
我被困在 SQL 命令上,我真的不知道如何修复它。 My error is: Invalid Column Name
我的错误是:无效的列名
This is my database: MyDatabase这是我的数据库: MyDatabase
And this is my connection class:这是我的连接类:
namespace CarDAL
{
public class ConnectionClass
{
private string Connectionstring =
"MYCONNECTIONSTRING";
public List<string> SoortSelectList(string KarakterSoort)
{
KarakterSoort = "Defensive";
List<string> soortList = new List<string>();
using (SqlConnection connection = new SqlConnection(Connectionstring))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Karakter WHERE KarakterSoort = Defensive", connection))
{
using (IDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
soortList.Add(dr.GetValue(0).ToString());
soortList.Add(dr.GetValue(1).ToString());
}
}
}
}
return soortList;
}
I think after WHERE that the problem is, but I don't know (and can't find) the right solution for my problem.我认为问题出在哪里之后,但我不知道(也找不到)适合我的问题的解决方案。
Your WHERE
clause here:你的
WHERE
子句在这里:
WHERE KarakterSoort = Defensive
compares the value in the KarakterSoort
column to the value in the Defensive
column.将
KarakterSoort
列中的值与Defensive
列中的值进行比较。
Is that really what you want???这真的是你想要的吗???
Quite possibly, you want to compare to a string literal - then you need to put this into single quotes like this:很可能,您想与字符串文字进行比较 - 然后您需要将其放入单引号中,如下所示:
WHERE KarakterSoort = 'Defensive'
Now you're selecting all rows where the KarakterSoort
column contains the value Defensive
现在您正在选择
KarakterSoort
列包含值Defensive
所有行
Or if you might want compare to some other value in the future - use a parameter in your query string:或者,如果您将来可能想与其他一些值进行比较 - 在查询字符串中使用一个参数:
WHERE KarakterSoort = @DesiredValue
and declare it并声明它
cmd.Parameters.Add("@DesiredValue", SqlDbType.VarChar, 100);
and set its value before your run the command:并在运行命令之前设置其值:
cmd.Parameters["@DesiredValue"].Value = "Defensive";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.