c# - SQL 异常错误 C# ASP.Net

标签 c# asp.net sql-server

自从我搞砸了任何 SQL 以来已经有一段时间了,我正在尝试构建一个小的 Todo 应用程序以使用 C# 学习一些 ASP.Net。我在本地使用 Visual Studio 2013 及其随附的任何版本的 SQL Express。

我有下表 todo_list,通过 Visual Studio 使用以下脚本制作:

CREATE TABLE [dbo].[todo_list] (
    [id]       INT  NOT NULL,
    [task]     TEXT NOT NULL,
    [complete] BIT  NOT NULL,
    [date]     DATE NOT NULL,
    PRIMARY KEY CLUSTERED ([id] ASC)
);

当 Web 应用程序启动时,我试图获取所有 completefalse 的记录。我假设我可以将 complete 列读/写为 true/false 因为它是 bit 类型。

当执行以下代码时抛出异常...

    private void Get_Tasks()
    {
        //Get the connection string
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = System.Configuration.ConfigurationManager.
            ConnectionStrings["Database1ConnectionString"].ConnectionString;

        //Build SQL query
        string query = "SELECT * FROM todo_list WHERE complete=False";
        System.Diagnostics.Debug.WriteLine(query);

        //Build SQL Command Object
        SqlCommand command = new SqlCommand(query, connection);

        //Grab all uncompleted tasks from database
        SqlDataReader cursor;

        try
        {
            using(connection)
            {
                //Open and execute SQL stuffz...
                connection.Open();
                cursor = command.ExecuteReader();

                //Get all the tasks
                while (cursor.Read())
                {
                    //Build the task from record set
                    Todo task = new Todo(
                        (int)cursor["id"], (string)cursor["task"], 
                        (bool)cursor["complete"], (DateTime)cursor["date"]);

                    //Append to DOM
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert" + UniqueID, "alert('About to append to DOM!');", true);
                    tasklist.InnerHtml = task.toHtml();
                }

                //Close connection
                connection.Close();
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.ToString());
            connection.Close();
        }

        //TODO - Grab all completed tasks from database

    }

cursor = command.ExecuteReader(); 执行时抛出的异常 -

System.Data.dll 中发生类型为“System.Data.SqlClient.SqlException”的第一次机会异常

System.Data.SqlClient.SqlException (0x80131904): 列名称“False”无效。

我不知道为什么它使用 False 作为列名?

在此先感谢您的帮助!

最佳答案

您可以将 False 更改为 0 或 'False'

Example

关于c# - SQL 异常错误 C# ASP.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484154/

相关文章:

sql-server - 将外键添加到复合键时出错

c# - 通过大型数据表优化循环

C# 如何按空格将字符串拆分为几个数组

c# - ASP.NET 5 Identity 3 用户在应用程序重启后注销

asp.net - 通过源代码预编译 - 如何对其进行逆向工程?

sql-server - 空间索引

c# - 继续打开 OpenFileDialog 直到选择有效文件

c# - C# 窗体的大小和位置

asp.net - 如何使用iTextSharp打印 GridView

sql-server - 使用 ID 和日期进行透视