【发布时间】:2013-11-27 12:45:22
【问题描述】:
您好,我尝试将表单中的数据保存到 MS Access 数据库。有 13 个字段要保存,但是当我单击此行上显示的保存按钮时出现错误 top.ExecuteNonQuery(); Data type mismatch in criteria expression。
这是我的代码,尽可能帮助我
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();
在 Access 数据库中 13 列 InvoiceNumber 编号、条款文本、InvoiceDate 日期/时间、OurQuote 文本、SalesPerson 文本、CustomerName 文本、Orderno 文本、InvoiceAddress 备注、DeliveryAddress 备注、InclusiveStatus Yes/No、Price Decimal、Tax Decimal、GrandTotal Decimal .
我的代码出了什么问题?
【问题讨论】:
-
为什么在
single Quotes中传递十进制值?? -
嗨 kyle 1st 我试过这样
'"+ Price +"'osame 错误来了,所以我像这样改变了但仍然错误 -
@SriHari - 请让自己熟悉 parameters 和 why 在没有使用它们的情况下永远不要使用数据库。
-
#corak, 刚接触 C# 能看清楚吗?
-
@SriHari - 我认为您尝试学习 C# 很棒。更有理由不去养成坏习惯。将数据直接注入 sql 命令是处理数据库时最糟糕的习惯之一。我希望提供的链接能帮助您学习“正确”的方法,这样您就不会在路上遭受痛苦和折磨。