简体   繁体   English

SqlException:'='附近的语法不正确

[英]SqlException: Incorrect syntax near '='

I'm taking some basic lessons and I'm trying the simplest example possible but I'm getting a SQL exception during the reader execution. 我正在学习一些基本的课程,并且正在尝试最简单的示例,但是在读取器执行期间遇到了SQL异常。

An unhandled exception of type 'System.Data.SqlClient.SqlException ... Incorrect syntax near '='. 类型为'System.Data.SqlClient.SqlException的未处理异常...'='附近的语法不正确。

Here's the core of the code: 这是代码的核心:

    static void Main(string[] args)
    {
        string connectionString = "Server=(localdb)\\v11.0;Integrated Security=true;AttachDBFileName=C:\\Users\\james\\Documents\\Visual Studio 2013\\Projects\\Day8_6\\Database1.mdf;";
        string queryString = "SELECT * FROM [Customers]";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(connectionString, connection);

                connection.Open();

                // *** ERROR ON FOLLOWING LINE
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("\t{1}\t{2}", reader[1], reader[2]);
                }

                connection.Close();
        }

        Console.ReadLine();

    }

Suggetions? 建议? Thank you! 谢谢!

This: 这个:

SqlCommand command = new SqlCommand(connectionString, connection);

needs to be: 需要是:

SqlCommand command = new SqlCommand(queryString, connection);

您在SqlCommand构造函数而不是sql查询中传递连接字符串:

SqlCommand command = new SqlCommand(queryString, connection);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM