我正在構建一個簡單的銷售點程序並處理允許最多3個搜索條件(InvoiceID,ClientName和ClientID)的「搜索發票」按鈕。這些是名爲「發票」的表中3列的名稱。在選擇查詢中的條件表達式中的數據類型不匹配c#訪問數據庫
InvoiceID
是Int32
類型的鍵列,ClientName
是String
類型的,ClientID
是Int32
類型。 ClientName
和ClientID
搜索工作完美。
我的問題:如果在選擇查詢中包含InvoiceID
,我會收到以下錯誤消息。我花了幾天的時間試圖找出答案。
錯誤:Database Error: Datatype mismatch in criteria expression.
你更有經驗的程序員可以幫助我嗎?謝謝!
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());}
需要更多信息?如果我沒有提供足夠的信息,我會發布更多信息。 數據庫信息或其他。
非常感謝所有程序員。
也感謝您編輯格式丹麥語(我知道如何做到這一點)。 – burfor7