Adding parameters in SQLite with C#

2019-01-05 03:26发布

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

2条回答
Emotional °昔
2楼-- · 2019-01-05 03:55

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));
...
查看更多
We Are One
3楼-- · 2019-01-05 04:12

Try VALUES instead of VALUE.

查看更多
登录 后发表回答