[英]Data type mismatch in criteria expression in C# with MS Access?
Hi I try to save data's from my form to MS Access Database. 嗨,我尝试将数据从表单保存到MS Access数据库。 There are 13 Fields to save but when i click save button error shown on this line
top.ExecuteNonQuery();
有13个字段要保存,但是当我单击此行
top.ExecuteNonQuery();
显示的保存按钮错误时top.ExecuteNonQuery();
Data type mismatch in criteria expression
. 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. 在Access数据库中的13列InvoiceNumber编号,条款文本,InvoiceDate日期/时间,OurQuote文本,SalesPerson文本,CustomerName文本,Orderno文本,InvoiceAddress备注,DeliveryAddress备注,InclusiveStatus是/否,价格小数,税收小数,GrandTotal小数。
What was wrong in my code ? 我的代码出了什么问题?
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();
Few point's which I think you should know while saving to Access Database
我认为在保存到
Access Database
应该知道的几点
1) String
should be within single Quotes
1)
String
应在single Quotes
2) Numeric
values should not be within Quotes 2)
Numeric
不应该在引号内
3) DateTime
values should be saved using # 3)
DateTime
值应使用#保存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.