0

我有下面的代码,我收到了这个错误:

Troubleshooting Exceptions: System.Data.OleDb.OleDbException "syntax error"

我不知道我做错了什么。它假设从表中提取信息。我在整个项目上都使用了相同的方法,只是在这方面,它让我很麻烦......

    class Codons
{
    private bool start, end;
    private string codon1, codon3, triplet1, triplet2, triplet3;
    private string triplet4, triplet5, triplet6, fullName;
    private OleDbConnection dataconnection;


    public Codons(string letter)
    {
        this.start = false;
        this.end = false;
        this.dataconnection = new OleDbConnection();
            this.dataconnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
            this.dataconnection.Open();


            string sql = "SELECT  tblCodons.codonsCodon3, " +
            "tblCodons.codonsTriplet1, tblCodons.codonsTriplet2, tblCodons.codonsTriplet3, " +
            "tblCodons.codonsTriplet4, tblCodons.codonsTriplet5, tblCodons.codonsTriplet6, " +
            "tblCodons.codonsFullName, tblCodons.codonsStart, tblCodons.codonsEnd"
            + " FROM tblCodons"
            + " WHERE tblCodons.codonsCodon1="+letter;

            OleDbCommand mycomm = new OleDbCommand(sql, dataconnection);
            OleDbDataReader dataReader = mycomm.ExecuteReader();
            dataReader.Read();
            this.codon1 = letter;
            this.codon3 = dataReader.GetString(0);
            this.triplet1 = dataReader.GetString(1);

            if (dataReader.IsDBNull(2))
                this.triplet2 = "     ";
            else
                this.triplet2 = dataReader.GetString(2);

            if (dataReader.IsDBNull(3))
                this.triplet3 = "     ";
            else
                this.triplet3 = dataReader.GetString(3);

            if (dataReader.IsDBNull(4))
                this.triplet4 = "     ";
            else
                this.triplet4 = dataReader.GetString(4);

            if (dataReader.IsDBNull(5))
                this.triplet5 = "     ";
            else
                this.triplet5 = dataReader.GetString(5);

            if (dataReader.IsDBNull(6))
                this.triplet6 = "     ";
            else
                this.triplet6 = dataReader.GetString(6);

            this.fullName = dataReader.GetString(7);
            this.start = dataReader.GetBoolean(8);
            this.end = dataReader.GetBoolean(9);
            dataReader.Close();

    }
4

3 回答 3

3

这里:

+ " WHERE tblCodons.codonsCodon1="+letter

您正在创建一个如下所示的 SQL 子句。

...WHERE tblCodons.codonsCodon1=A

这是无效的 SQL。SQL 中的字符串文字必须被引用属性,即

...WHERE tblCodons.codonsCodon1='A'

为此,您可以手动添加单引号确保正确转义字符串字母中的所有单引号(除非您想处理SQL 注入)...

...或者(更好)您使用参数化查询

+ " WHERE tblCodons.codonsCodon1 = ?"; 

OleDbCommand mycomm = new OleDbCommand(sql, dataconnection); 
mycomm.Parameters.AddWithValue("codonsCodon1", letter);

此外,您应该检查返回值dataReader.Read()而不是仅仅调用它:

if (!dataReader.Read()) {
     // replace this with more useful error reporting, maybe throwing an exception
     MessageBox.Show("No codon for this letter found.");
     return;
}
于 2012-04-25T14:51:18.330 回答
1

由于您将 SQL 查询构建为字符串,我猜您在letter. 一旦将该值附加到查询中,就会导致您看到的语法错误。

我会看看这篇关于Parameterized Queries (OleDb)的帖子。这是将参数传递给查询的正确方法,可以在您的应用程序中避免这种情况(以及其他明显的 SQL 注入攻击)。

于 2012-04-25T14:45:11.340 回答
0

可能是您输入了letter不带引号的值。然后它将 的内容letter视为一个函数。由于找不到该函数,因此会引发语法错误异常。

尝试letter用引号(“字母”)关闭

于 2012-04-25T14:51:44.777 回答