1.) Do not reuse a parameter
2.) VALUES(@str, @new) ParameterName="@str." ParameterName="@new."
As the error indicates, you are using the parameters @str and @new and adding two different parameters. Read the error message, it is there for a reason and told you exactly what is wrong!
As the error indicates, you are using the parameters @str and @new and adding two different parameters. Read the error message, it is there for a reason and told you exactly what is wrong!
3.) Here is how I would write it (in this context).
using (SQLiteConnection cnn = new SQLiteConnection("Data Source=C://EEk.db"))
using (SQLiteCommand cmd = Database.CreateCommand())
{
cmd.CommandText = "INSERT INTO test (Field1,fname) VALUES(@str, @new)";
cmd.Parameters.AddWithValue("@str", textBox1.Text);
cmd.Parameters.AddWithValue("@new", textBox2.Text);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
using (SQLiteCommand cmd = Database.CreateCommand())
{
cmd.CommandText = "INSERT INTO test (Field1,fname) VALUES(@str, @new)";
cmd.Parameters.AddWithValue("@str", textBox1.Text);
cmd.Parameters.AddWithValue("@new", textBox2.Text);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
While you can use DbConnection and DbCommand and make a factory
pattern sort of implementation (which I doubt is going here...) I
suggest you do not. Use the SqliteConnection, SqliteCommand, etc. The
provider specific objects generally have features that make coding
easier. In this case, the AddWithValue method.
本文针对SQLite数据库操作中常见的参数使用错误进行了详细解析,并提供了一段正确的插入数据示例代码。文章强调了不要重复使用参数的重要性,并指导如何正确地定义和使用参数。
459

被折叠的 条评论
为什么被折叠?



