[英]How to cast the correct data type when INSERTING into MS Access with a SQL string?
I have an Excel table with hundreds of columns and various data types (ints, strings, dates). 我有一个具有数百列和各种数据类型(整数,字符串,日期)的Excel表。 I am using C# to programatically insert these rows into an MS Access database table.
我正在使用C#以编程方式将这些行插入MS Access数据库表中。
When I build the query string, all the Excel cell contents become strings. 当我构建查询字符串时,所有Excel单元格内容都变为字符串。 The query string ends up looking like this:
查询字符串最终看起来像这样:
string query = "INSERT INTO tbl_Items VALUES ('toy duck', 'yellow', '122', '2015-01-16')"
Casting the type in my SQL string with INSERT INTO tbl_Items VALUES ('toy duck', 'yellow', CLng('122'), CDate('2015-01-16'))
is out of the question due to the quantity of columns be dealt with, and the need for flexible code. 使用
INSERT INTO tbl_Items VALUES ('toy duck', 'yellow', CLng('122'), CDate('2015-01-16'))
将类型转换为SQL字符串是INSERT INTO tbl_Items VALUES ('toy duck', 'yellow', CLng('122'), CDate('2015-01-16'))
因为列需要处理,并且需要灵活的代码。
So how can I achieve a flexible programatic insert of multiple datatypes into my Access DB? 那么,如何在Access DB中实现多种数据类型的灵活编程插入? I don't mind pre-defining the database table inside Access, but I am not sure if it will correctly auto-change the strings coming in. Any tips?
我不介意在Access中预定义数据库表,但是我不确定它是否会正确地自动更改输入的字符串。有什么提示吗?
Oh wow! 哇! Simply pre-defining the table column types will work!
只需预定义表列类型即可! MS Access automatically converts the strings into the necessary type.
MS Access自动将字符串转换为必要的类型。
For example: 例如:
***tbl_Items***
name: String,
color: String,
weight: Number,
delivery_date: Date
Will accept and auto-cast the SQL string: 将接受并自动广播SQL字符串:
string query = "INSERT INTO tbl_Items VALUES ('toy duck', 'yellow', '122', '2015-01-16')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.