Adding parameters in SQLite with C#

85,150

Try a different approach, naming your fields in the query and naming the parameters in the query:

this.command.CommandText = "INSERT INTO StringData (field1, field2) VALUES(@param1, @param2)";
this.command.CommandType = CommandType.Text;
this.command.Parameters.Add(new SQLiteParameter("@param1", data.Data));
this.command.Parameters.Add(new SQLiteParameter("@param2", data.ByteIndex));
...
Share:
85,150
Brian Sweeney
Author by

Brian Sweeney

I like computers, I like the outdoors and I like old metal.

Updated on September 15, 2020

Comments

  • Brian Sweeney
    Brian Sweeney over 3 years

    Im just learning SQLite and I can't get my parameters to compile into the command properly. When I execute the following code:

    this.command.CommandText = "INSERT INTO [StringData] VALUE (?,?)";
    
    this.data = new SQLiteParameter();
    this.byteIndex = new SQLiteParameter();
    
    this.command.Parameters.Add(this.data);
    this.command.Parameters.Add(this.byteIndex);
    
    this.data.Value = data.Data;
    this.byteIndex.Value = data.ByteIndex;
    
    this.command.ExecuteNonQuery();
    

    I get a SQLite Exception. Upon inspecting the CommandText I discover that whatever I'm doing is not correctly adding the parameters: INSERT INTO [StringData] VALUE (?,?)

    Any ideas what I'm missing?

    Thanks