问题
Hi I try to save data's from my form to MS Access Database. There are 13 Fields to save but when i click save button error shown on this line top.ExecuteNonQuery();
Data type mismatch in criteria expression
.
This is my code help me as possible
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Srihari/Invoice.accdb");
conn.Open();
// to save top column in invoive
int invoicenumber = Convert.ToInt32(TXE_Invoice_Number.Text);
string terms = CBL_Terms.Text;
DateTime date = CBL_Date.DateTime;
string ourquote = TXE_OurQuote.Text;
string salesperson = CBL_Sales_Person.Text;
string customername = CBL_Customer_Nmae.Text;
string oderno = CBL_Order_Number.Text;
string invoiceaddress = TXE_Invoice_Address.Text;
string deliveryaddress = TXE_Delivery_Address.Text;
bool inclusive = CBX_New.Checked;
decimal Price = Convert.ToDecimal(TXE_Price.Text);
decimal tax = Convert.ToDecimal(TXE_Tax.Text);
decimal grandtotal = Convert.ToDecimal(TXE_Total.Text);
OleDbCommand top = new OleDbCommand("INSERT INTO test_top(InvoiceNumber,Terms,[InvoiceDate],OurQuote,SalesPerson,CustomerName,OrderNumber,InvoiceAddress,DeliveryAddress,InclusiveStatus,Price,Tax,GrandTotal) VALUES (" + invoicenumber + ",'" + terms + "','" + date + "','" + ourquote + "','" + salesperson + "','" + customername + "','" + oderno + "','" + invoiceaddress + "','" + deliveryaddress + "','" + inclusive + "','" + Price + "','" + tax + "','" + grandtotal + "')", conn);
top.ExecuteNonQuery();
MessageBox.Show("Inserted Successful (top)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.close();
In Access Database 13 Columns InvoiceNumber Number, Terms Text, InvoiceDate Date/Time, OurQuote Text, SalesPerson Text, CustomerName Text, Orderno Text, InvoiceAddress Memo, DeliveryAddress Memo, InclusiveStatus Yes/No, Price Decimal, Tax Decimal, GrandTotal Decimal.
What was wrong in my code ?
回答1:
Seriously, save yourself headaches and write more reliable code at the same time by using a parameter query:
OleDbCommand top = new OleDbCommand(
"INSERT INTO test_top (" +
"InvoiceNumber,Terms,[InvoiceDate],OurQuote," +
"SalesPerson,CustomerName,OrderNumber," +
"InvoiceAddress,DeliveryAddress,InclusiveStatus," +
"Price,Tax,GrandTotal" +
") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", conn);
top.Parameters.AddWithValue("?", invoicenumber);
top.Parameters.AddWithValue("?", terms);
top.Parameters.AddWithValue("?", date);
top.Parameters.AddWithValue("?", ourquote);
top.Parameters.AddWithValue("?", salesperson);
top.Parameters.AddWithValue("?", customername);
top.Parameters.AddWithValue("?", oderno);
top.Parameters.AddWithValue("?", invoiceaddress);
top.Parameters.AddWithValue("?", deliveryaddress);
top.Parameters.AddWithValue("?", inclusive);
top.Parameters.AddWithValue("?", Price);
top.Parameters.AddWithValue("?", tax);
top.Parameters.AddWithValue("?", grandtotal);
top.ExecuteNonQuery();
回答2:
Few point's which I think you should know while saving to Access Database
1) String
should be within single Quotes
2) Numeric
values should not be within Quotes
3) DateTime
values should be saved using #
来源:https://stackoverflow.com/questions/20242896/data-type-mismatch-in-criteria-expression-in-c-sharp-with-ms-access