C# System.Data.SQLite 参数问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/751172/
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
System.Data.SQLite parameter issue
提问by CasperT
I have the following code:
我有以下代码:
try
{
//Create connection
SQLiteConnection conn = DBConnection.OpenDB();
//Verify user input, normally you give dbType a size, but Text is an exception
var uNavnParam = new SQLiteParameter("@uNavnParam", SqlDbType.Text) { Value = uNavn };
var bNavnParam = new SQLiteParameter("@bNavnParam", SqlDbType.Text) { Value = bNavn };
var passwdParam = new SQLiteParameter("@passwdParam", SqlDbType.Text) {Value = passwd};
var pc_idParam = new SQLiteParameter("@pc_idParam", SqlDbType.TinyInt) { Value = pc_id };
var noterParam = new SQLiteParameter("@noterParam", SqlDbType.Text) { Value = noter };
var licens_idParam = new SQLiteParameter("@licens_idParam", SqlDbType.TinyInt) { Value = licens_id };
var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
"VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam')", conn);
insertSQL.Parameters.Add(uNavnParam); //replace paramenter with verified userinput
insertSQL.Parameters.Add(bNavnParam);
insertSQL.Parameters.Add(passwdParam);
insertSQL.Parameters.Add(pc_idParam);
insertSQL.Parameters.Add(noterParam);
insertSQL.Parameters.Add(licens_idParam);
insertSQL.ExecuteNonQuery(); //Execute query
//Close connection
DBConnection.CloseDB(conn);
//Let the user know that it was changed succesfully
this.Text = "Succes! Changed!";
}
catch(SQLiteException e)
{
//Catch error
MessageBox.Show(e.ToString(), "ALARM");
}
It executes perfectly, but when I view my "brugere" table, it has inserted the values: '@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam' literally. Instead of replacing them.
它完美地执行,但是当我查看我的“brugere”表时,它插入了值:'@uNameParam'、'@bNavnParam'、'@passwdParam'、'@pc_idParam'、'@noterParam'、'@licens_idParam' . 而不是取代它们。
I have tried making a breakpoint and checked the parameters, they do have the correct assigned values. So that is not the issue either.
我尝试创建断点并检查参数,它们确实具有正确的分配值。所以这也不是问题。
I have been tinkering with this a lot now, with no luck, can anyone help?
我现在一直在修补这个,没有运气,有人可以帮忙吗?
Oh and for reference, here is the OpenDB method from the DBConnection class:
哦,作为参考,这里是 DBConnection 类中的 OpenDB 方法:
public static SQLiteConnection OpenDB()
{
try
{
//Gets connectionstring from app.config
const string myConnectString = "data source=data;";
var conn = new SQLiteConnection(myConnectString);
conn.Open();
return conn;
}
catch (SQLiteException e)
{
MessageBox.Show(e.ToString(), "ALARM");
return null;
}
}
采纳答案by Ronald Wildenberg
You should remove the quotes around your parameter names in the INSERT statement.
您应该删除 INSERT 语句中参数名称周围的引号。
So instead of
所以代替
VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam',
'@noterParam', '@licens_idParam')
use
用
VALUES (@uNameParam, @bNavnParam, @passwdParam, @pc_idParam,
@noterParam, @licens_idParam)
回答by CasperT
Thanks to rwwilden and Jorge Villuendas, the answer is:
感谢 rwwilden 和 Jorge Villuendas,答案是:
var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
" VALUES (@uNavnParam, @bNavnParam, @passwdParam, @pc_idParam, @noterParam, @licens_idParam)", conn);
insertSQL.Parameters.AddWithValue("@uNavnParam", uNavn);
insertSQL.Parameters.AddWithValue("@bNavnParam", bNavn);
insertSQL.Parameters.AddWithValue("@passwdParam", passwd);
insertSQL.Parameters.AddWithValue("@pc_idParam", pc_id);
insertSQL.Parameters.AddWithValue("@noterParam", noter);
insertSQL.Parameters.AddWithValue("@licens_idParam", licens_id);
insertSQL.ExecuteNonQuery(); //Execute query
回答by Stark
replace
代替
VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam')
值('@uNameParam'、'@bNavnParam'、'@passwdParam'、'@pc_idParam'、'@noterParam'、'@licens_idParam')
with
和
VALUES (?, ?, ?, ?, ?, ?)
值 (?, ?, ?, ?, ?, ?)
回答by bro
When you use System.Data.SqlClient
then you provide parameter types from System.Data.SqlDbType
enumeration.
当您使用System.Data.SqlClient
then 时,您可以从System.Data.SqlDbType
枚举中提供参数类型。
Butif you use System.Data.SQLite
then you have to use **System.Data.DbType**
enumeration.
但是如果你使用System.Data.SQLite
那么你必须使用**System.Data.DbType**
枚举。