Sql Parametrized syntax error with VFP OleDb

2019-02-25 04:23发布

问题:

I'm trying to make an SQL parametrized update command for a DBF File ( Visual Fox Pro ) I don't know why, but i have a "syntax error" on the "DbCommand.ExecuteNonQuery();".

The exception error message is "Syntax Error.". I don't have any additionnal informations.

string maRequete = "UPDATE "  + strNomTable + " set " 
+ "evetype = @evetype ,"
+ "evedes = @evedes ,"
+ "evecli = @evecli ,"
+ "eveusermo = @eveusermo ,"
+ "eveinterv = @eveinterv where eveNum = '"  + strEvtNumeroString.ToString() + "'";

OleDbCommand DbCommand = new OleDbCommand(maRequete);

DbCommand.Parameters.Add("@evetype", OleDbType.VarChar);
DbCommand.Parameters.Add("@evedes", OleDbType.VarChar);
DbCommand.Parameters.Add("@evecli", OleDbType.VarChar);
DbCommand.Parameters.Add("@eveusermo", OleDbType.VarChar);
DbCommand.Parameters.Add("@eveinterv", OleDbType.VarChar);


DbCommand.Parameters["@evetype"].Value = m_strEvtType.ToString().Trim();
DbCommand.Parameters["@evedes"].Value = m_strDesignation.ToString().Trim();
DbCommand.Parameters["@evecli"].Value = m_strCodeClient.ToString().Trim();
DbCommand.Parameters["@eveusermo"].Value = m_strUserModification;
DbCommand.Parameters["@eveinterv"].Value = m_strCodeIntervenant.ToString().Trim();


try
{
    string strStringConnect = @"Provider=vfpoledb.1;Data Source=" + m_strDirectoryDBF + @"\" + strDbfFile + ".dbf;Collating Sequence=general";
    OleDbConnection DbConnection = new OleDbConnection(strStringConnect);

    DbCommand.CommandType = System.Data.CommandType.Text;

    DbConnection.Open();
    DbCommand.Connection = DbConnection;

    DbCommand.ExecuteNonQuery();
    return "O";
}
catch (Exception Ex)
{
    return Ex.Message;
}

I tried to use a parameter for the "where" statement, and there is no changes :(

Anyone have ideas please?

Thanks a lot :)

回答1:

VFP does not support "named" parameters with the @ symbol. Instead, just change to "?" which acts as a place-holder for parameters. Then, make sure your parameters are added in the exact same sequence as they appear in the sql command. You are very close.

You can even use a parameter for the eveNum value in the WHERE clause.

string maRequete = "UPDATE "  + strNomTable + " set " 
+ "evetype = ?,"
+ "evedes = ?,"
+ "evecli = ?,"
+ "eveusermo = ?,"
+ "eveinterv = ? where eveNum = ?";

OleDbCommand DbCommand = new OleDbCommand(maRequete);

DbCommand.Parameters.Add("Varevetype", OleDbType.VarChar);
DbCommand.Parameters.Add("Varevedes", OleDbType.VarChar);
DbCommand.Parameters.Add("Varevecli", OleDbType.VarChar);
DbCommand.Parameters.Add("Vareveusermo", OleDbType.VarChar);
DbCommand.Parameters.Add("Vareveinterv", OleDbType.VarChar);
DbCommand.Parameters.Add("VarWhere", OleDbType.VarChar);


DbCommand.Parameters["Varevetype"].Value = m_strEvtType.ToString().Trim();
DbCommand.Parameters["Varevedes"].Value = m_strDesignation.ToString().Trim();
DbCommand.Parameters["Varevecli"].Value = m_strCodeClient.ToString().Trim();
DbCommand.Parameters["Vareveusermo"].Value = m_strUserModification;
DbCommand.Parameters["Vareveinterv"].Value = m_strCodeIntervenant.ToString().Trim();
DbCommand.Parameters["VarWhere"].Value = strEvtNumeroString.ToString().Trim();