简体   繁体   中英

Message: System.Data.OleDb.OleDbException : Syntax error (missing operator) in query expression

public static void WriteData(string colName,string data)
{
    using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Constants.FileName + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=3;READONLY=FALSE\""))
    {
        // string query = String.Format("INSERT INTO  [DataSet$]({0}) VALUES ({1})",colName, data);

        string query = String.Format("INSERT INTO  [DataSet$]({0}) VALUES ({1})", colName, data);
        cn.Open();
        OleDbCommand cmd = new OleDbCommand(query, cn);
        cmd.Parameters.AddWithValue("@colName", colName);
        cmd.Parameters.AddWithValue("@data", data);

        cmd.ExecuteNonQuery();
        cn.Close();
        cn.Dispose();
    }
}

What you are trying to do is mixing dynamic query generation and queryparameters. You can not use command parameters to populate column names in the query. Parameters are used only for passing values to the query.

So what you need to do is, to use string.Format only for placing column name in the query and use command parameter only to pass data to the query.

public void WriteData(string colName, string data)
{
    using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Constants.FileName + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=3;READONLY=FALSE\""))
    {
        string query = string.Format("INSERT INTO [DataSet$] ({0}) VALUES (@data)", colName);
        // The query variable will have following value assuming colName=token
        //INSERT INTO [DataSet$] (token) VALUES (@data)
        cn.Open();
        //Now use OleDbCommand to pass value of @data to the query.
        OleDbCommand cmd = new OleDbCommand(query, cn);
        cmd.Parameters.AddWithValue("@data", data);

        cmd.ExecuteNonQuery();
        cn.Close();
    }
}

This should help you resolve your issue.

public static void UpdateData(string keyName,string colName,string data)
    {
        using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Constants.FileName + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=3;READONLY=FALSE\""))
        {
            // string query = String.Format("INSERT INTO  [DataSet$]({0}) VALUES ({1})",colName, data);

                connection.Open();
                string commandString = String.Format("UPDATE [DataSet$] SET colName ='{0}' WHERE keyName = '{1}'",@data,@keyName);
                OleDbCommand cmd = new OleDbCommand(commandString, connection);
            cmd.Parameters.AddWithValue("@data", data);
            cmd.Parameters.AddWithValue("@keyName", keyName);
            connection.Close();
                connection.Dispose();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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