C#中使用MS Access的标准表达式中的数据类型不匹配?
嗨,我尝试将数据从我的表单保存到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是/否,价格十进制,税十进制,GrandTotal十进制。
我的代码出了什么问题?
说真的,通过使用参数查询来节省头痛并同时编写更可靠的代码:
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();
在保存到Access Database
时,我认为您应该知道的几点
1) String
应在single Quotes
2) Numeric
值不应在引号内
3)应使用#保存DateTime
值
- Python for .NET:如何使用同一DLL的不同版本显式创建C#类的实例?
- 在分隔符处拆分字节数组
- 为什么if和#的范围以这种方式在c#中
- TimeZoneInfo.FindSystemTimeZoneById()在Unity应用程序中抛出exception
- 属性上的reflection和访问修饰符
- 无法处理文件resx,因为它位于Internet或Restricted区域中,或者在文件上具有Web标记
- 从ASP MVC中的区域控制器链接到根控制器
- CollectionEditor为System.Drawing.Point产生“对象与目标类型不匹配。”
- 为什么Monitor类保留2个队列:“就绪”和“等待”?