簡體   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