C#SQL的奇怪行为

时间:2013-09-12 07:02:30

标签: c# sql

string sql = "SELECT column1 FROM table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID where column1 = \"testData\"";
using (var command = new SqlCommand(sql, connection))
{
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            Console.WriteLine(reader["column1"].ToString());
        }
        Console.ReadLine();
    }
}

当没有where子句并且我能写入“testData”时,上面的SQL查询工作正常。但是当我限制条件时,它给我sqlException说“testData”是一个无效的列?

3 个答案:

答案 0 :(得分:7)

您需要使用单引号在SQL中指定字符串。

所以

string sql = "SELECT column1 FROM table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID where column1 = 'testData'";

答案 1 :(得分:2)

答案 2 :(得分:1)

\'无需使用'

string sql = "SELECT column1 FROM table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID where column1 = 'testData'";