【问题标题】:Auto complete the textbox from the oracle database从 oracle 数据库自动完成文本框
【发布时间】:2015-02-25 21:04:28
【问题描述】:
    void AutoCompleteText()
    {
        textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();

        string query = "select * from Book;";
        OracleConnection con = new OracleConnection(ConString);
        OracleCommand cmd = new OracleCommand(query, con);
        OracleDataReader myReader;

        try
        {
            con.Open();
            myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {
                string sName = myReader.GetString("Title");
                coll.Add(sName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        textBox1.AutoCompleteCustomSource = coll;
    }

在表格Book中我有一个名为Title的列,我想在textbox1中搜索时建议我,假设我在textbox1中写了A,那么它会自动弹出所有以A开头的标题从甲骨文数据库。

但是通过执行代码我得到了以下错误:

Error-1: 'System.Data.Common.DbDataReader.GetString(int)' 的最佳重载方法匹配有一些无效参数

错误 2:参数 1:无法从 'string' 转换为 'int'

【问题讨论】:

  • 它告诉你GetString() 方法需要一个整数作为参数。您需要提供Title 列的列索引,而不是列名。

标签: c# oracle visual-studio-2012


【解决方案1】:

GetStringint 作为参数。您正在尝试传递string。您可以使用GetOrdinal 获取命名列的列序号:

while (myReader.Read())
{
    string sName = myReader.GetString(myReader.GetOrdinal("Title"));
    coll.Add(sName);
}

或者,这也应该有效:

string sName = (string) myReader["Title"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2015-10-08
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多