我想通过Windows窗体应用程序使用ID(整数)或名称(字符串)在oledb数据库中进行搜索。用户选择与组合框的搜索类型,作为可能的搜索类型是不同的我用一个开关创建两个不同的查询:OleDb在哪里像声明错误
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”(大写和小写)的所有内容,而不是。
第二个问题是当用户在组合框Id
中选择时,会导致错误Input String has not a correct format
(使用注释和非注释指令)。
任何想法如何更改代码来阅读我想要在数据库中搜索的项目?
编辑
感谢apportation。看到答案,我会尽力回答他们(太多的信息不能作为评论)。首先,我改变了代码为乔恩说,现在当选择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
的部分还没有以这种方式工作。
第二:*“如果ID是数字字段,则选择... WHERE ID不应该使用LIKE”。我认为这是必要的。我不需要搜索一个号码,我需要搜索一个号码,比如的插入。例如,如果用户选择Id
并插入1,则结果不仅仅是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);
}
在哪条线路到底如何?什么是'text'的价值,什么是你的'CurrentCulture?' – 2014-11-14 15:11:32
如果ID是一个数字字段,那么选择... WHERE ID不应该使用LIKE。 – Steve 2014-11-14 15:21:02
'Restro Fad'的问题,当你调试这个代码时,假设你已经通过了代码,Combo的价值是它的实际文本,也许你可以改变代码来确保它读取选择的值正确 'string Como = this.comboBox1.GetItemText(this.comboBox1。SelectedItem);' – MethodMan 2014-11-14 15:29:47