我正在构建一个简单的销售点程序,并正在开发一个“搜索发票”按钮,该按钮最多允许 3 个搜索条件(InvoiceID、ClientName 和 ClientID)。这些是名为“Invoicing”的表中 3 个列的名称。
InvoiceID
是 Int32
类型的关键列,ClientName
是 String
类型,ClientID
的类型为 Int32
。 ClientName
和 ClientID
搜索工作完美。
我的问题:如果我在选择查询中包含 InvoiceID
,则会收到以下错误。我花了几天时间试图弄清楚。
错误:数据库错误:条件表达式中的数据类型不匹配。
有经验的程序员可以帮帮我吗?谢谢!
String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data" + @" Source=TESTDB.accdb";
String tableName = "Invoicing";
String query = String.Format("select * from [{0}] where", tableName);
//ADD IN SEARCH CRITERIA
int filled = 0;
if (invoiceBox.Text != "") { query += " InvoiceID='" + invoiceBox.Text+"'"; filled += 1; }
/*if (DateCheckBox.Checked == true)
{
if (filled>=1) { query += " and DateNTime='" + monthCalendar1.SelectionStart.ToString() + "'"; filled += 1; }
else { query += " DateNTime='" + monthCalendar1.SelectionStart.ToString()+ "'"; filled += 1; }
}
* */
if (ClientNameBox.Text != "") //Doesnot work
{
if (filled >= 1) { query += " and Client='" + ClientNameBox.Text + "'"; filled += 1; }
else { query += " Client='" + ClientNameBox.Text + "'"; filled += 1; }
}
if (ClientIDBox.Text != "") //THIS search criteria works!!!!
{
if (filled >= 1) { query += " and ClientID='" + ClientIDBox.Text + "'"; filled += 1; }
else { query += " ClientID='" + ClientIDBox.Text + "'"; filled += 1; }
}
//CHECK IF SEARCH CRITERIA ARE PRESENT
if (filled < 1) { MessageBox.Show("At least One Search criteria above is required"); return; }
DataSet dsInvoicing = new DataSet();
OleDbConnection conn = new OleDbConnection(connectionString);
try
{
//Open Database Connection
conn.Open();
OleDbDataAdapter daInvoicing = new OleDbDataAdapter(query, conn);
//Fill the DataSet
daInvoicing.Fill(dsInvoicing, tableName);
//MessageBox.Show("dsInventory has "+dsInventory.Tables[0].Rows.Count+" search results");
conn.Close();
this.dataGridView1.DataSource = dsInvoicing.Tables[0];
}
catch (OleDbException exp){ MessageBox.Show("Database Error: " + exp.Message.ToString());}
需要更多信息?如果我没有提供足够的内容,我会发布更多内容。 数据库信息或其他。
非常感谢所有程序员。
最佳答案
看起来数据库中 InvoiceID 的数据类型是某种数字类型。在查询时,您将其视为字符串。尽量不要将 InvoiceID 值括在单引号中。
关于c# - 标准表达式中的数据类型不匹配。在选择查询中 C# Access 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161926/