SQLite错误提供给命令的参数不足 [英] SQLite error Insufficient parameters supplied to the command

查看:188
本文介绍了SQLite错误提供给命令的参数不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在注释/ ********** /的行上收到错误提供给命令的参数不足。我不知道它有什么问题?请帮帮我?



I got the error "Insufficient parameters supplied to the command" on the line I comment /**********/. I don't know what's wrong with it? Please help me?

string _search;
private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
    _search = txtSearchBox.Text;
}

SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source = music.db");
SQLiteCommand sqlite_cmd;

private void btn_search_Click(object sender, RoutedEventArgs e)
{
    sqlite_conn.Open();
    sqlite_cmd = sqlite_conn.CreateCommand();
    sqlite_cmd.CommandText = "select Title, ArtistName, AlbumName from Song,Artist,Album where (Song.Title=@title) and (Song.AlbumID=Album.AlbumID) and (Song.ArtistID=Artist.ArtistID)";
    
    sqlite_cmd.Parameters.Add("@title", DbType.String, -1);
    sqlite_cmd.Parameters["@title"].Value = _search;
    
    DataSet dataSet = new DataSet();
    SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlite_cmd.CommandText, sqlite_conn);
    
    dataAdapter.Fill(dataSet); /*****************/
    datagrid.ItemsSource = dataSet.Tables[0].DefaultView;
    datagrid.CanUserAddRows = false;
    datagrid.CanUserDeleteRows = false;
    sqlite_conn.Close();
}

推荐答案

由于您正在使用CommandText而不是SQLiteCommand对象构造SQLiteDataAdaptor,因此发生错误。 CommandText不包含SQLiteParameter对象 - 只是它在字符串中的占位符。



更改

The error is occurring because you are constructing the SQLiteDataAdaptor with the CommandText, rather than the SQLiteCommand object. The CommandText does not contain the SQLiteParameter object - just its placeholder in the string.

Changing
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlite_cmd.CommandText, sqlite_conn);



to


to

SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlite_cmd, sqlite_conn);



应该修复问题。


should fix the issue.


这篇关于SQLite错误提供给命令的参数不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆