繁体   English   中英

{“值不能为空。\\ r \\ nParameter name:s”}

[英]{“Value cannot be null.\r\nParameter name: s”}

我试图使用sqlite数据库创建一个独立的程序,但它每次插入时都会显示以下错误。 我检查了我的参数,但没有一个是空的。 这是我的插入代码。

public bool insertToTable(SQLiteConnection sql,string TableName, string Date, string Receipt, string Particulars, string Description, string Category, double amount)
{
    SQLiteCommand command = new SQLiteCommand(sql);
    if (sql == null)
        return false;
    string statement = 
        "INSERT INTO @table (Date, Receipt,Particulars,Description,Category,Amount) " +
        "VALUES (@date, @rec,@part,@desc,@cat,@amt)";

    command.Parameters.AddWithValue("@date",Date);
    command.Parameters.AddWithValue("@rec", Receipt);
    command.Parameters.AddWithValue("@part", Particulars);
    command.Parameters.AddWithValue("@desc", Description);
    command.Parameters.AddWithValue("@cat", Category);
    command.Parameters.AddWithValue("@amt", amount);
    command.Prepare();
    int x = command.ExecuteNonQuery();
    return true;
}

并且堆栈跟踪是

 at System.Text.UTF8Encoding.GetByteCount(String chars)
   at System.Data.SQLite.SQLiteConvert.ToUTF8(String sourceText)
   at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
   at ConnectToDb.sqlitecommand.insertToTable(SQLiteConnection sql, String TableName, String Date, String Receipt, String Particulars, String Description, String Category, Double amount) in C:\Users\Jon Stephen\documents\visual studio 2015\Projects\ConnectToDb\ConnectToDb\sqlitecommand.cs:line 45
   at ConnectToDb.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\Jon Stephen\documents\visual studio 2015\Projects\ConnectToDb\ConnectToDb\Form1.cs:line 36
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

第一眼看法:SQL语句的表名应该从方法参数而不是SQL参数给出。 您的代码应如下所示:

public boolean insertToTable (SQLiteConnection sql, string TableName, string Date, string Receipt, string Particulars, string Description, string Category, double amount)
{
    SQLiteCommand command = new SQLiteCommand(sql);
    if (sql == null)
        return false;
    String statement = 
        "INSERT INTO " + TableName + " (Date, Receipt,Particulars,Description,Category,Amount) " +
        "VALUES (@date, @rec,@part,@desc,@cat,@amt)";

    command.Parameters.AddWithValue("@date",Date);
    command.Parameters.AddWithValue("@rec", Receipt);
    command.Parameters.AddWithValue("@part", Particulars);
    command.Parameters.AddWithValue("@desc", Description);
    command.Parameters.AddWithValue("@cat", Category);
    command.Parameters.AddWithValue("@amt", amount);
    command.Prepare();
    int x = command.ExecuteNonQuery(); // change to command.ExecuteNonQuery() depending what you need
    return true;
}

并执行时:

sqlitecommander.insertToTable(dbConnection, "tester", "4/1", "rec", "part", "desc", "cat", 3.3);

将生成SQL语句,如下所示:

INSERT INTO tester (Date,Receipt,Particulars,Description,Category,Amount) VALUES ('4/1', 'rec', 'part', 'desc', 'cat', 3.3)

发生异常,因为您没有提供任何表名来插入DB(证明@table为null或为空),CMIIW。

这是我的sqlite版本的兼容性问题,我下载了32位sqlite而不是64位。我通过Nuget下载了64位然后解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM