OleDbException(0x80040E07):条件表达式中的数据类型不匹配

时间:2016-10-22 04:06:27

标签: c# ms-access ado.net windows-services

OleDbException(0x80040E07):条件表达式中的数据类型不匹配。

我可以使用一些帮助来解决此错误。我已经阅读了所有常见问题,但似乎都没有适用。事实上相同的代码在通过单元测试执行时成功执行但是从我的Windows服务运行时失败。

我的sql:

SQL_SALES_INSERT = "INSERT INTO [Sales](Key_Sets, Trans_Code, Key_No, Group_ID, NLU_No, PLU_No, Description, Price, Tax1, Tax2, Tax3, FoodStamp, POS, Trans_No, Qty, Amount, Shift, Amount_Action, Date_Time, Adjustment, Dept, Class, SubClass, TaxID_1, TaxID_2, TaxID_3, SKU, UPC, PromoNo, PromoListNo, UnitRetailAdj, Discount, SaleAmt, RSU, OrigPrice, ReasonCode, Emp_ID, PromoType, RetailPrice, RetailAmt, NACSCode)"
+ " VALUES(?, ?, ?, ? ,? ,? ,? ,? ,? ,? ,? ,?,?,? ,?,? ,? ,? ,?,? ,? ,? ,? ,? ,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ,?,?)";

参数设置:

_saleInsertCommand = _database.GetCommand(SQL_SALES_INSERT, _database.Connection);
_database.AddParameterWithValue(_saleInsertCommand, "Key_Sets", 0, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "Trans_Code", data.TransCode, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "Key_No", 0, DbType.Int16);
_database.AddParameterWithValue(_saleInsertCommand, "Group_ID", data.GroupID, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "NLU_No", data.NluNo, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "PLU_No", data.PluNo, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "Description", data.Description, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "Price", data.Price, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "Tax1", false, DbType.Boolean);
_database.AddParameterWithValue(_saleInsertCommand, "Tax2", false, DbType.Boolean);
_database.AddParameterWithValue(_saleInsertCommand, "Tax3", false, DbType.Boolean);
_database.AddParameterWithValue(_saleInsertCommand, "FoodStamp", false, DbType.Boolean);
_database.AddParameterWithValue(_saleInsertCommand, "POS", 8, DbType.Int16);
_database.AddParameterWithValue(_saleInsertCommand, "Trans_No", data.TransNo, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "Qty", data.Qty, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "Amount", data.Amount, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "Shift", data.Shift, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "Amount_Action", 1, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "Date_Time", data.DateTime.Value, DbType.DateTime);
_database.AddParameterWithValue(_saleInsertCommand, "Adjustment", 0.0M, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "Dept", data.Dept, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "Class", 0, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "SubClass", 0, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "TaxID_1", data.TaxID, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "TaxID_2", data.TaxID, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "TaxID_3", data.TaxID, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "SKU", data.SKU, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "UPC", data.UPC, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "PromoNo", data.PromoNo, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "PromoListNo", data.PromoListNo, DbType.Int32);
_database.AddParameterWithValue(_saleInsertCommand, "UnitRetailAdj", 0.0M, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "Discount", 0.0M, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "SaleAmt", data.SaleAmt, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "RSU", data.RSU, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "OrigPrice", data.OrigPrice, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "ReasonCode", string.Empty, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "Emp_ID", string.Empty, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "PromoType", data.PromoType, DbType.String);
_database.AddParameterWithValue(_saleInsertCommand, "RetailPrice", data.RetailPrice, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "RetailAmt", data.RetailAmt, DbType.Decimal);
_database.AddParameterWithValue(_saleInsertCommand, "NACSCode", data.NacsCode, DbType.String);

执行ExecuteNonQuery前命令的详细信息:

 COMMAND TEXT: INSERT INTO [Sales] (Key_Sets,Trans_Code,Key_No,Group_ID,NLU_No,PLU_No,Description,Price,Tax1,Tax2,Tax3,FoodStamp,POS,Trans_No,Qty,Amount,Shift,Amount_Action ,Date_Time,Adjustment,Dept,Class,SubClass,TaxID_1,TaxID_2,TaxID_3,SKU,UPC,PromoNo,PromoListNo,UnitRetailAdj,Discount,SaleAmt,RSU,OrigPrice,ReasonCode,Emp_ID,PromoType,RetailPrice,RetailAmt,NACSCode) 
    VALUES(?,?,?,? ,? ,? ,? ,? ,? ,? ,? ,?,?,? ,?,? ,? ,? ,?,? ,? ,? ,? ,? ,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ,?,?) \r\n
    PARAMETERS: Key_Sets(Int32/0)=0 Trans_Code(String/50)=SOPEN Key_No(Int16/0)=0 Group_ID(Int32/0)=0 NLU_No(Int32/0)=0 PLU_No(String/15)= Description(String/50)=Shift Open Price(Decimal/0)=0.0 Tax1(Boolean/0)=False Tax2(Boolean/0)=False Tax3(Boolean/0)=False FoodStamp(Boolean/0)=False POS(Int16/0)=8 Trans_No(Int32/0)=1 Qty(Decimal/0)=0.0 Amount(Decimal/0)=0.0 Shift(Int32/0)=1 Amount_Action(Int32/0)=1 Date_Time(DateTime/0)=10/21/2016 3:07:28 PM Adjustment(Decimal/0)=0.0 Dept(Int32/0)=0 Class(Int32/0)=0 SubClass(Int32/0)=0 TaxID_1(Int32/0)=0 TaxID_2(Int32/0)=0 TaxID_3(Int32/0)=0 SKU(Int32/0)=0 UPC(String/20)= PromoNo(Int32/0)=0 PromoListNo(Int32/0)=0 UnitRetailAdj(Decimal/0)=0.0 Discount(Decimal/0)=0.0 SaleAmt(Decimal/0)=0.0 RSU(String/10)= OrigPrice(Decimal/0)=0.0 ReasonCode(String/10)= Emp_ID(String/10)= PromoType(String/6)= RetailPrice(Decimal/0)=0.0 RetailAmt(Decimal/0)=0.0 NACSCode(String/10)= 

访问架构:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Sales" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="generated" type="xsd:dateTime"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Sales">
<xsd:annotation>
<xsd:appinfo/>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Key_Sets" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="Trans_Code" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Key_No" minOccurs="0" od:jetType="integer" od:sqlSType="smallint" type="xsd:short"/>
<xsd:element name="Group_ID" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="NLU_No" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="PLU_No" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="15"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Description" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Price" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="Tax1" minOccurs="1" od:jetType="yesno" od:sqlSType="bit" od:nonNullable="yes" type="xsd:boolean"/>
<xsd:element name="Tax2" minOccurs="1" od:jetType="yesno" od:sqlSType="bit" od:nonNullable="yes" type="xsd:boolean"/>
<xsd:element name="Tax3" minOccurs="1" od:jetType="yesno" od:sqlSType="bit" od:nonNullable="yes" type="xsd:boolean"/>
<xsd:element name="FoodStamp" minOccurs="1" od:jetType="yesno" od:sqlSType="bit" od:nonNullable="yes" type="xsd:boolean"/>
<xsd:element name="POS" minOccurs="0" od:jetType="integer" od:sqlSType="smallint" type="xsd:short"/>
<xsd:element name="Trans_No" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="Qty" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="Amount" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="Shift" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="Amount_Action" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="Keys_ID" minOccurs="1" od:jetType="autonumber" od:sqlSType="int" od:autoUnique="yes" od:nonNullable="yes" type="xsd:int"/>
<xsd:element name="Date_Time" minOccurs="0" od:jetType="datetime" od:sqlSType="datetime" type="xsd:dateTime"/>
<xsd:element name="Adjustment" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="Dept" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="Class" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="SubClass" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="TaxID_1" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="TaxID_2" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="TaxID_3" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="SKU" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="UPC" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="PromoNo" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="PromoListNo" minOccurs="0" od:jetType="longinteger" od:sqlSType="int" type="xsd:int"/>
<xsd:element name="UnitRetailAdj" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="Discount" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="SaleAmt" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="RSU" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="OrigPrice" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="ReasonCode" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Emp_ID" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="PromoType" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="6"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="RetailPrice" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="RetailAmt" minOccurs="0" od:jetType="currency" od:sqlSType="money" type="xsd:double"/>
<xsd:element name="NACSCode" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

1 个答案:

答案 0 :(得分:0)

导致问题的是DateTime字段。解决方案是将参数值转换为特定格式:

_database.AddParameterWithValue(_saleInsertCommand, "Date_Time", data.DateTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), DbType.DateTime);