将 Excel 导入 ASP.NET 时的 OleDbDataAdapter 和列转换
我正在使用 C# 导入 Excel。但是 CustomerOrderNR 列包含这些类型值:
20283
20213
20625
50749-50
30687
31975
82253
但是当我执行这些代码时:
string QTam = @"SELECT Date, [Serial Number], [Status Code], Description, CustomerOrderNR FROM [Sheet1$]";
DataSet ds = new DataSet();
OleDbDataAdapter cmd = new OleDbDataAdapter(QTam, strConn);
cmd.Fill(ds, "orders");
它以 System.Double 数据类型返回 CustomerOrderNR 列。 我想更改数据表列类型,但随后它返回异常,例如:
Cannot change DataType of a column once it has data.
Can't I change DataType of Column from Double to String in SQL statements?
I'm importing an excel using c#. But CustomerOrderNR column contains these kind values:
20283
20213
20625
50749-50
30687
31975
82253
But when I execute these codes:
string QTam = @"SELECT Date, [Serial Number], [Status Code], Description, CustomerOrderNR FROM [Sheet1$]";
DataSet ds = new DataSet();
OleDbDataAdapter cmd = new OleDbDataAdapter(QTam, strConn);
cmd.Fill(ds, "orders");
It is returning CustomerOrderNR column in System.Double data type.
I want to change datatable column type but then it return exception like:
Cannot change DataType of a column once it has data.
Can't I change DataType of Column from Double to String in SQL statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定如何在 SQL 中强制使用数据类型,但您可以通过克隆数据集架构来更改数据集中的数据类型,并使用 ImportRow 复制数据。
但是,如果您想将 Excel 中的数据类型视为字符串,则可能应该将其更改为字符串。
I'm not sure how you can force the datatype in the SQL, but you could chage the datatype afterwards in the dataset by cloning the dataset schema, and copy the data with ImportRow.
But rather you should probably change the datatype in Excel to string if you want to treat it as string.
Excel 中的混合数据类型始终是有问题的。
这是一个可能有帮助的答案
OleDB 和 OleDB混合Excel数据类型:缺少数据
这是另一个很好的解释
http://munishbansal.wordpress.com/2009/12/15/importing-data-from-excel-having-mixed-data-types-in -a-列-ssis/
Mixed datatypes in excel are always problematic.
Heres an answer that might help
OleDB & mixed Excel datatypes : missing data
Heres another nice explanation
http://munishbansal.wordpress.com/2009/12/15/importing-data-from-excel-having-mixed-data-types-in-a-column-ssis/
查看您的连接字符串。您可能需要包含 ISAM=1,以便 Excel 知道将混合列作为文本处理。
Look at your connection String. You may need to include ISAM=1 so excel knows to handle mixed columns as text.
如果您有预先确定的字段列表及其预期数据类型,我相信,这应该是最可维护的方法:
创建一个 xml 架构,如下所示:
并阅读:
日期格式必须对系统设置有效 短日期设置为“dd/mm/yy” ,您可以根据需要设置格式。
设置类型后,可以将
if you have a predetermined list of fields and their expected datatypes, i believe, this should be the most maintainable approach:
create a xml schema as follows:
and read:
the catch being that date format has to be valid for the system settings is in short dates as 'dd/mm/yy'
once the type is set, you can format as required.