在C#SQL查询中不能使用WHERE? [英] Cannot use WHERE in C# SQL query?

查看:282
本文介绍了在C#SQL查询中不能使用WHERE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,每当我尝试在C#中的SQL命令中使用简单的WHERE子句时,我都会收到错误:列名无效。



这是我的代码,当检查单选按钮时触发...



For some reason, any time I try to use a simple WHERE clause in a SQL command in C#, I'm getting an error: invalid column name.

Here is my code, which is triggered when a radiobutton is checked...

private void radioShowActive_CheckedChanged(object sender, EventArgs e)
        {
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM namesdates WHERE IsActive='Yes'", connection))
            {
                DataTable namedatesTable = new DataTable();
                adapter.Fill(namedatesTable);

                listEmployees.DisplayMember = "Name";
                listEmployees.ValueMember = "Id";
                listEmployees.DataSource = namedatesTable;

                DataTable birthdaysTable = new DataTable();
                adapter.Fill(birthdaysTable);

                listBirthdays.DisplayMember = "BirthDate";
                listBirthdays.ValueMember = "Id";
                listBirthdays.DataSource = birthdaysTable;

            }





数据库名称正确,列名是IsActive,与数据库名称相对应是名字。如果我使用带有ID号的WHERE,它可以正常工作。除了其他任何东西,它都会因此错误而失败。任何帮助表示赞赏。



我的尝试:



试过使用ID键而不是列引用,这是有效的。



The database name is correct, the name of the column is IsActive, adn the database name is namesdates. If I use WHERE with ID number, it works fine. With anything else, it fails with this error. Any help is appreciated.

What I have tried:

Tried using the ID key instead of a column reference, and that works.

推荐答案

啊......好像我引用了一个旧的数据库实例,而不是新的一个。旧的有一个不同的列名,所以显然WHERE子句没问题。
Ahh... It seems like I was referencing an old instance of the database instead of the new one. Old one had a different column name, so apparently the WHERE clause was fine.


好吧,错误告诉你IsActive不是正确的列名。您的查询非常简单,问题不可能是其他任何问题。



您是否碰巧将列命名为Is Active(请注意空格)?如果是这样,您必须在查询中将列名称指定为[Is Active],方括号。

Well, the error is telling you that "IsActive" is not the correct column Name. Your query is so simple, the problem cannot be anything else.

Did you happen to name the column "Is Active" (note the space)? If so, you have to specify the column name in the query as [Is Active], in square brackets.
SELECT * FROM namesdates WHERE [Is Active]='Yes'


这篇关于在C#SQL查询中不能使用WHERE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆