0

Windows フォーム アプリケーションで ID (整数) または名前 (文字列) を使用して oledb データベースを検索しようとしています。可能な検索タイプが異なるため、ユーザーはコンボボックスで検索のタイプ​​を選択します。スイッチを使用して2つの異なるクエリを作成します。

        string Combo = this.comboBox1.SelectedItem.ToString();
        string text = this.textBox1.Text;
        connection.Open();
        OleDbCommand command = null;
        switch (Combo)
        {
            case "Nombre":
                command = new OleDbCommand("SELECT Id, Nombre  FROM ARTICULOS WHERE ? LIKE ? OR ? LIKE ?", connection);
                command.Parameters.AddWithValue("?", Combo);
                command.Parameters.AddWithValue("?", "%" + LowercaseFirst(text) + "%");
                command.Parameters.AddWithValue("?", Combo);
                command.Parameters.AddWithValue("?", "%"+UppercaseFirst(text)+"%");
                break;
            case "Id":
                command = new OleDbCommand("SELECT Id, Nombre  FROM ARTICULOS WHERE ? LIKE ?", connection);
                command.Parameters.AddWithValue("?", Combo);
                command.Parameters.AddWithValue("?", Convert.ToInt32(text));
                //command.Parameters.AddWithValue("?", "%" + Convert.ToInt32(text) + "%");
                break;
        }
        try
        {
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                string list = "";
                list += reader.GetValue(0).ToString() + "\t";
                list += reader.GetValue(1).ToString();
                this.listBox1.Items.Add(list);
            }
        }
        catch (Exception ex)
        {
            DialogResult result = MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            this.Close();
        }
        connection.Close();

コンボボックスはデフォルトでNombre(スペイン語の名前)で始まり、データベース内のすべてのアイテムが表示されます(私が望んでいたように)。

最初の問題は、ユーザーがテキストボックスにテキストを挿入したときに始まります。たとえば、テキストボックスに「p」を挿入すると、データベース内に「p」(大文字と小文字) を含むすべてのものを表示する必要がありますが、表示されません。

2 番目の問題は、ユーザーがコンボボックスで選択するIdと、エラーが発生することですInput String has not a correct format(コメント付きおよびコメントなしの命令を使用)。

データベースで検索したいアイテムを読み取るようにコードを変更する方法はありますか?


編集

分譲ありがとう。答えを見て、私はそれらに答えようとします(コメントとして入れるには情報が多すぎます)。最初にジョンが言うようにコードを変更しましたが、今ではNombreが選択されているときに正しく動作しています:

        this.listBox1.Items.Clear();
        string Combo = this.comboBox1.SelectedItem.ToString();
        string text = this.textBox1.Text;
        connection.Open();
        OleDbCommand command = null;
        switch (Combo)
        {
            case "Nombre":
                command = new OleDbCommand("SELECT Id, Nombre  FROM ARTICULOS WHERE Nombre LIKE ? OR Nombre LIKE ?", connection);
                command.Parameters.AddWithValue("?", "%" + LowercaseFirst(text) + "%");
                command.Parameters.AddWithValue("?", "%" + UppercaseFirst(text) + "%");
                break;
            case "Id":
                command = new OleDbCommand("SELECT Id, Nombre  FROM ARTICULOS WHERE ID LIKE ?", connection);
                command.Parameters.AddWithValue("?", "%" + Convert.ToInt32(text) + "%");
                break;
        }
        try
        {
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                string list = "";
                list += reader.GetValue(0).ToString() + "\t";
                list += reader.GetValue(1).ToString();
                this.listBox1.Items.Add(list);
            }
        }
        catch (Exception ex)
        {
            DialogResult result = MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            this.Close();
        }
        connection.Close();

の部分Idはまだこのように機能していません。

2 番目: *"If ID is a numeric field then select ... WHERE ID should not use LIKE". これは必要だと思います。番号を検索する必要はありません。挿入されたような番号を検索する必要があります。たとえば、ユーザーが 1 を選択Idして挿入した場合、結果は 1 だけである必要はなく、11、111、231 など、1 を含む任意の数になります ( の場合Nombre)。

最後に: 大文字と小文字の関数が必要です。これらの関数を使用した場合と使用しない場合のコードをテストしたところ、これらの関数を使用するとより完全な結果が得られました。コードは次のとおりです。

static string LowercaseFirst(string s)
        {
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToLower(s[0]) + s.Substring(1);
}
static string UppercaseFirst(string s)
{
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToUpper(s[0]) + s.Substring(1);
}
4

1 に答える 1

3

問題 (または少なくとも 1 つの問題) は、列名にパラメーターを使用しようとしていることです。それはできません -パラメータ化できるのは値だけです。列とテーブルの名前は、SQL 自体の一部である必要があります。

たとえば、次のようになります。

case "Nombre":
    command = new OleDbCommand("SELECT Id, Nombre  FROM ARTICULOS WHERE ? LIKE ? OR ? LIKE ?", connection);
    command.Parameters.AddWithValue("?", Combo);
    command.Parameters.AddWithValue("?", "%" + LowercaseFirst(text) + "%");
    command.Parameters.AddWithValue("?", Combo);
    command.Parameters.AddWithValue("?", "%"+UppercaseFirst(text)+"%");

次のようにする必要があります。

case "Nombre":
    command = new OleDbCommand(
        "SELECT Id, Nombre  FROM ARTICULOS WHERE Nombre LIKE ? OR Nombre LIKE ?", 
        connection);
    command.Parameters.AddWithValue("?", "%" + LowercaseFirst(text) + "%");
    command.Parameters.AddWithValue("?", "%"+UppercaseFirst(text)+"%");
于 2014-11-14T15:12:05.450 に答える