SQLException与数据库中的字符

时间:2013-12-31 18:00:42

标签: c# sql sql-server

我在表格中有数据:

CodeID    ColOne          ColTwo
---------------------------------------
1         DATAONE         DATAONE
2         DATATWO-Test    DATA-TEST1

这是我的代码:

public static Codes [] Fetch(IDataProxy proxy, string whereClause, params FetchParameter [] parameters)
{
    List<Codes> list = new List<Codes>();
    using(IDbCommand cmd = proxy.Connection.CreateCommand())
    {
        cmd.CommandText = "SELECT [CodeID],[ColOne],[ColTwo] FROM [Codes]";
        using(IDataReader reader = cmd.ExecuteReader()) {
            while(reader.Read()) {
                list.Add(new Codes(reader));
            }
        }
    }
    return list.ToArray();
}

当我检索第二个结果时,我在第7行的“cmd.ExecuteReader()”上收到一条错误消息。错误消息如下:

Unclosed quotation mark after the character string 'DATATWO'. Incorrect syntax near 'DATATWO'.

我做错了什么?

感谢您的帮助!

修改

这是代码:

internal Codes(IDataReader reader)
{
   this.CodeID = reader.GetInt32(0);
   this.ColOne = reader.GetString(1);
   this.ColTwo = reader.GetString(2);
   this.DataState = System.Data.DataRowState.Unchanged;
}

1 个答案:

答案 0 :(得分:1)

我忽略了一些东西,在我的例子中,我没有包含我的“where”子句,而select语句是:where ColOne = 'DATATWO-Test。在价值之后,我没有收盘。