17

I have a simple insert statement to a table in an SQLite database on MonoDroid.

When inserting to the database, it says

SQLite error Insufficient parameters supplied to the command at Mono.Data.Sqlite.SqliteStatement.BindParameter

I think there is either a bug, or the error message is misleading. Because I only have 5 parameters and I am providing 5 parameters, so I cannot see how this be right.

My code is below, and any help would be greatly appreciated.

try
{
    using (var connection = new SqliteConnection(ConnectionString))
    {
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandTimeout = 0;
            command.CommandText = "INSERT INTO [User] (UserPK ,Name ,Password ,Category ,ContactFK) VALUES ( @UserPK , @Name , @Password , @Category , @ContactFK)";
            command.Parameters.Add(new SqliteParameter("@Name", "Has"));
            command.Parameters.Add(new SqliteParameter("@Password", "Has"));
            command.Parameters.Add(new SqliteParameter("@Cateogry", ""));
            command.Parameters.Add(new SqliteParameter("@ContactFK", DBNull.Value));
            command.Parameters.Add(new SqliteParameter("@UserPK", DbType.Guid) {Value = Guid.NewGuid()});
            var result = command.ExecuteNonQuery();
            return = result > 0 ;
        }
    }
}
catch (Exception exception)
{
    LogError(exception);
}
4

2 回答 2

23

您在 INSERT 语句中的拼写@Category与添加的参数不同。你有:

command.Parameters.Add(new SqliteParameter("@Cateogry", ""));
                                           ^^^^^^^^^^^
                                           //@Category

它应该在哪里:

将您的声明修改为:

command.Parameters.Add(new SqliteParameter("@Category", ""));
于 2013-04-29T04:22:23.420 回答
4

这已经得到了正确的回答,但我想补充一下,以获取更多信息:

如果任何 @Parameters 在 INSERT 语句与 AddWithValue 或 .Add(new SqliteParameter... 语句中的拼写不同,则也可以生成此特定的 SQLite 错误字符串。

于 2016-02-01T00:24:44.453 回答