【问题标题】:SQLite: Insufficient parameters suppliedSQLite:提供的参数不足
【发布时间】:2020-06-07 10:06:39
【问题描述】:

我正在尝试存储用户输入的信息(如注册),但我收到一条错误消息:

SQLiteException 未处理
System.Data.SQLite.dll 中出现“System.Data.SQLite.SQLiteException”类型的未处理异常
附加信息:未知错误
提供给命令的参数不足

代码:

var command = "insert into Reports(sReportNo, sReportDate) values(@ReportNo, @ReportDate)";
using (IDbConnection = new SQLiteConnection connection =
    new SQLiteConnection(@"Data Source=.\T.Server.db;Initial Catalog=Reports;Version=3;"))
{
    try
    {
        connection.Execute(command, new
    {
        sReportNo = ReportNo,
        sReportDate = ReportDate
    });
    MessageBox.Show(successful);
}
catch
{
    MessageBox(unsuccessful);
}

Reset();

【问题讨论】:

  • 表中还有其他字段吗?哪些是强制性的?
  • @viveknuna - 我正在使用 SQLite DB 浏览器,那里的代码是:CREATE TABLE "Reports" ("sReportNo" TEXT, "sReportDate" TEXT);

标签: c# sqlite


【解决方案1】:

更新
使用 Dapper,您可以将参数作为匿名对象发送到数据库,如下所示:

var sql = "Insert into Reports (sReportNo, sReportData) values (@ReportNo, @ReportDate)";

using (var connection = new SQLiteConnection(@"Data Source=.\T.Server.db;Initial Catalog=Reports;Version=3;"))
{
    connection.Execute(sql, new {ReportNo = reportNo, ReportDate = reportDate} );
}

注意事项:
1. 我还在猜测保存值的变量名。
2. 如果连接尚未打开,Dapper 将为您打开连接。


原答案
您正在执行该语句,但您没有提供参数...在 new SQLiteCommand(...)command.ExecuteNonQuery() 之间,您应该将参数添加到命令中:

using (var connection = new SQLiteConnection(@"Data Source=.\T.Server.db;Initial Catalog=Reports;Version=3;"))
{
    using(var command = new SQLiteCommand("Insert into Reports (sReportNo, sReportData) values (@ReportNo, @ReportDate)", connection)
    {
        command.Parameters.Add("@ReportNo", SQLiteType.Int32).Value = reportNumber;
        command.Parameters.Add("@ReportDate", SQLiteType.DateTime).Value = reportDate;
        connection.Open();
        command.ExecuteNonQuery();
    }
}

注意事项:

  1. 我猜测参数类型和保存这些值的变量名称。
  2. 只需要在执行命令前打开连接即可。
  3. 您无需显式关闭连接,它会在 using 块的末尾被释放(并关闭)。

【讨论】:

  • 它给了我一些错误: 1. 无法使用集合初始化程序初始化类型 'System.Data.SQLite.SQLiteCommand',因为它没有实现 'System.Collections.IEumerable' 2. 名称 'command ' 在当前上下文中不存在。 3.当前上下文中不存在名称'SQLiteType'。
  • 我的代码没有使用集合初始化器。你复制错了。
  • 嗯,这根本没有帮助。如果你这么含糊,我帮不了你。我需要知道这里的最低限度。如果您可以将整个方法粘贴到问题中,那可能会有所帮助。
  • 嘿,我正在使用 SQLite 和 Dapper,所以我应该使用正确的语法
  • 看,只需使用您现在使用的代码编辑您的问题,也许我(或其他人)可以帮助您解决问题。其他任何事情都只是时间的腰部。
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2013-04-22
相关资源
最近更新 更多