使用 C# 在 SQLite 中添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/809246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding parameters in SQLite with C#
提问by Brian Sweeney
Im just learning SQLite and I can't get my parameters to compile into the command properly. When I execute the following code:
我只是在学习 SQLite,我无法让我的参数正确编译到命令中。当我执行以下代码时:
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 (?,?)
我得到一个 SQLite 异常。检查 CommandText 后,我发现无论我在做什么,都没有正确添加参数:INSERT INTO [StringData] VALUE (?,?)
Any ideas what I'm missing?
任何想法我错过了什么?
Thanks
谢谢
采纳答案by Brian Sweeney
Try VALUES
instead of VALUE
.
尝试VALUES
代替VALUE
.
回答by Bj?rn
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));
...