简体   繁体   English

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

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

I am trying to create a stand alone program using a sqlite database but it keeps showing the following error every time I insert. 我试图使用sqlite数据库创建一个独立的程序,但它每次插入时都会显示以下错误。 I checked my parameters and none of them are null. 我检查了我的参数,但没有一个是空的。 Here is my insert code. 这是我的插入代码。

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;
}

and the stack trace is 并且堆栈跟踪是

 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)

First sight: The table name for SQL statement should be given from method parameter instead of SQL parameter. 第一眼看法:SQL语句的表名应该从方法参数而不是SQL参数给出。 Your code should looks like this: 您的代码应如下所示:

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;
}

and while executed: 并执行时:

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

will produce SQL statement as this: 将生成SQL语句,如下所示:

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

The exception occurred since you didn't provide any table name to insert into DB (proving that @table is null or empty), CMIIW. 发生异常,因为您没有提供任何表名来插入DB(证明@table为null或为空),CMIIW。

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

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

相关问题 值不能为空。\\ r \\ n参数名称:输入 - Value cannot be null.\r\nParameter name: input “值不能为空。\\r\\n参数名称:文本” - “Value cannot be null.\r\nParameter name: text” Image.Save() 抛出异常“值不能为空。/r/n 参数名称:编码器” - Image.Save() throws exception "Value cannot be null./r/nParameter name: encoder" dc.BeginDialogAsync:“值不能为空。\\ nParameter name:options” - dc.BeginDialogAsync: “Value cannot be null.\nParameter name: options” Image.Save崩溃:{“值不能为空。\\ r \\ nParameter name:encoder”} - Image.Save crashing: {“Value cannot be null.\r\nParameter name: encoder”} C#Powershell-Exchange管理{“值不能为空。\\ r \\ n参数名称:serverSettings”} - C# Powershell - Exchange management {“Value cannot be null.\r\nParameter name: serverSettings”} Dynamics Crm 2015:值不能为空。\\ r \\ n参数名称:详细信息 - Dynamics Crm 2015: Value cannot be null.\r\nParameter name: detail “值不能为空。\\ r \\ n参数名称:实体(ASP.NET Web API) - "Value cannot be null.\r\nParameter name: entity (ASP.NET Web API) 根据xml错误{“值不能为空。\\ r \\ n参数名称:元素”}创建对象 - Creating objects from xml error {“Value cannot be null.\r\nParameter name: element”} 值不能为空,参数名称来源 - Value cannot be null, r nparameter name source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM