在MS访问在C#标准表达式中数据类型不匹配? [英] Data type mismatch in criteria expression in C# with MS Access?

查看:198
本文介绍了在MS访问在C#标准表达式中数据类型不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图从我的窗体的MS Access数据库保存数据的。有13个字段来保存,但是当我点击保存在这一行 top.ExecuteNonQuery()显示按钮错误; 条件表达式中数据类型不匹配



这是我的代码帮我尽可能

  OleDbConnection的康恩=新的OleDbConnection(供应商= Microsoft.ACE.OLEDB.12.0;数据源= D:/Srihari/Srihari/Invoice.accdb); 
conn.Open();

//保存在invoive
INT invoicenumber = Convert.ToInt32(TXE_Invoice_Number.Text)顶柱;
串条款= CBL_Terms.Text;
日期时间日期= CBL_Date.DateTime;
串ourquote = TXE_OurQuote.Text;
线的销售人员= CBL_Sales_Person.Text;
字符串客户名称= CBL_Customer_Nmae.Text;
串oderno = CBL_Order_Number.Text;
串invoiceaddress = TXE_Invoice_Address.Text;
串deliveryaddress = TXE_Delivery_Address.Text;

布尔包容= CBX_New.Checked;

十进制价格= Convert.ToDecimal(TXE_Price.Text);
十进制税= Convert.ToDecimal(TXE_Tax.Text);
十进制grandtotal = Convert.ToDecimal(TXE_Total.Text);

OleDbCommand的顶部=新的OleDbCommand(INSERT INTO test_top(InvoiceNumber,条款,[InvoiceDate],OurQuote,销售人员,客户名称,订单编号,InvoiceAddress,DeliveryAddress,InclusiveStatus,价格,税务,GrandTotal)VALUES( + invoicenumber +,'+术语+','+日期+,+ ourquote +,+销售人员+,+客户名称+','+ oderno + ','+ invoiceaddress +,+ deliveryaddress +,+包容性+,+价格+,+税收+,+ grandtotal +' ),conn);在
top.ExecuteNonQuery();
MessageBox.Show(插入成功(上),信息,MessageBoxButtons.OK,MessageBoxIcon.Information);



conn.close();



在Access数据库13列InvoiceNumber数,条款文字,InvoiceDate日期/时间,OurQuote文本,销售员文本,客户名称文字,Orderno文本,InvoiceAddress备忘录,DeliveryAddress备忘录,InclusiveStatus是/不,价格小数,税务小数,GrandTotal小数。



什么是错在我的代码?


解决方案

严重的,可以救自己头痛,并通过使用参数查询写的同时更可靠的代码:



<预类=郎CSHARP prettyprint-覆盖> OleDbCommand的顶部=新的OleDbCommand(
INSERT INTO test_top(+
InvoiceNumber,条款,[InvoiceDate],OurQuote,+
的营业员,客户名称,订单编号,+
InvoiceAddress,DeliveryAddress,InclusiveStatus,+
的价格,税收,GrandTotal+
)VALUES(?,?,?,?,?,? ???????,,,,,,,),conn);在
top.Parameters.AddWithValue(,invoicenumber?);
top.Parameters.AddWithValue(,术语?);
top.Parameters.AddWithValue(,日期?);
top.Parameters.AddWithValue(,ourquote?);
top.Parameters.AddWithValue(,营业员?);
top.Parameters.AddWithValue(,客户名称?);
top.Parameters.AddWithValue(,oderno?);
top.Parameters.AddWithValue(,invoiceaddress?);
top.Parameters.AddWithValue(,deliveryaddress?);
top.Parameters.AddWithValue(,包容?);
top.Parameters.AddWithValue(,价钱?);
top.Parameters.AddWithValue(,税?);
top.Parameters.AddWithValue(,grandtotal?);
top.ExecuteNonQuery();


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 ?

解决方案

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();

这篇关于在MS访问在C#标准表达式中数据类型不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆