简体   繁体   English

从C#运行SQL语句时,如何忽略MS Access“类型转换错误”?

[英]How to ignore MS Access “Type Conversion Error” when running SQL statement from C#?

I have an MS Access database with a table with hundreds of columns. 我有一个带有数百列表的MS Access数据库。 When running my SQL INSERT INTO query, one (or more) of the fields cause a "Type Conversion Error", which is fine because it works anyways. 运行我的SQL INSERT INTO查询时,一个(或多个)字段会引起“类型转换错误”,这很好,因为它仍然可以工作。 When I run this process of SQL INSERT INTO via the MS Access GUI I simply ignore the popup message. 当我通过MS Access GUI运行SQL INSERT INTO的过程时,我只是忽略了弹出消息。

eg: 例如:

INSERT INTO tbl_Log (ID, PID, Customer_ID...)
SELECT tbl_Data.ID, tbl_Data.PID, tbl_Data.Customer_ID...
FROM tbl_Data

However, when I run the query via C# and send the query string via Oledb, the query fails (I suspect due to the type conversion error). 但是,当我通过C#运行查询并通过Oledb发送查询字符串时,查询失败(我怀疑由于类型转换错误)。 How can I ignore this error and proceed with the query.. via C#? 如何通过C#忽略此错误并继续进行查询。

Is there an option in the GUI to "ignore all type conversion warnings" or a way to modify my SQL string? GUI中是否有“忽略所有类型转换警告”选项或一种修改SQL字符串的方法? Or do I have to go through all the hundreds of columns and make sure they are all the right type. 还是我必须遍历所有数百列,并确保它们是正确的类型。

When executing an "action query" (INSERT, UPDATE, or DELETE) in Access we are offered a choice of how to proceed if errors are encountered. 在Access中执行“动作查询”(INSERT,UPDATE或DELETE)时,如果遇到错误,我们可以选择如何进行操作。 For example, given an empty table named [tbl_Log] with fields 例如,给定一个名为[tbl_Log]的空表,其中包含字段

ID - Long Integer, Primary Key ID-长整数,主键
DateValue - Date/Time DateValue-日期/时间

and a table named [tbl_Data] with fields 和名为[tbl_Data]的表,其中包含字段

ID - Long Integer, Primary Key ID-长整数,主键
DateString - Text(10) DateString-文本(10)

and sample data 和样本数据

ID  DateString
--  ----------
 1  2016-01-01
 2  (unknown)
 3  2016-03-03

if we run the query 如果我们运行查询

INSERT INTO tbl_Log (ID, DateValue)
SELECT ID, DateString
FROM tbl_Data;

we see a dialog that says, in part 我们看到一个对话框,其中部分内容

Microsoft Access set 1 field(s) to Null due to a type conversion failure, ... 由于类型转换失败,Microsoft Access将1字段设置为Null。

To ignore the error(s) and run the query, click Yes. 要忽略错误并运行查询,请单击“是”。

If we click "No" then the entire INSERT is cancelled. 如果单击“否”,则整个INSERT将被取消。 If we click "Yes" then the valid data is inserted and the invalid value(s) are set to Null: 如果单击“是”,则插入有效数据,并将无效值设置为Null:

ID  DateValue
--  ----------
 1  2016-01-01
 2  
 3  2016-03-03

From a .NET application, System.Data.OleDb does not have the "ignore the error(s)" option but Access DAO does. 在.NET应用程序中, System.Data.OleDb没有“忽略错误”选项,而Access DAO则具有。 So, to achieve the same effect as clicking "Yes" in the Access dialog we can do 因此,要获得与在“访问”对话框中单击“是”相同的效果,我们可以

// This code requires the following COM reference in your project:
//
//     Microsoft Office 14.0 Access Database Engine Object Library
//
// and the declaration
//
//     using Microsoft.Office.Interop.Access.Dao;
//
// at the top of the class file            

var dbe = new DBEngine();
Database db = dbe.OpenDatabase(@"C:\Users\Public\Database1.accdb");
string sql =
        "INSERT INTO tbl_Log (ID, DateValue) " +
        "SELECT ID, DateString " +
        "FROM tbl_Data";
db.Execute(sql);

Note that the .Execute method does not use the RecordsetOptionEnum.dbFailOnError option, which would be the equivalent to clicking "No" in the Access dialog and cancelling the entire operation. 请注意, .Execute方法使用RecordsetOptionEnum.dbFailOnError选项,这将是相当于点击“否”,在Access对话框,取消整个操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM