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

时间:2016-06-16 03:58:56

标签: c# sqlite

我正在尝试使用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)

2 个答案:

答案 0 :(得分:0)

第一眼看到:SQL语句的表名应该从method参数而不是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为空或空),CMIIW,因此发生了异常。

答案 1 :(得分:0)

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