使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:04:50  来源:igfitidea点击:

Adding parameters in SQLite with C#

c#sqlitesystem.data.sqlite

提问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 VALUESinstead 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));
...