将 Excel 导入 ASP.NET 时的 OleDbDataAdapter 和列转换

发布于 2024-10-22 20:25:09 字数 645 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葵雨 2024-10-29 20:25:09

我不确定如何在 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.

べ映画 2024-10-29 20:25:09

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/

源来凯始玺欢你 2024-10-29 20:25:09

查看您的连接字符串。您可能需要包含 ISAM=1,以便 Excel 知道将混合列作为文本处理。

Look at your connection String. You may need to include ISAM=1 so excel knows to handle mixed columns as text.

若能看破又如何 2024-10-29 20:25:09

如果您有预先确定的字段列表及其预期数据类型,我相信,这应该是最可维护的方法:

创建一个 xml 架构,如下所示:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Table">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Field1" type="xs:int" minOccurs="0" />
              <xs:element name="Field2" type="xs:string" minOccurs="0" />
              <xs:element name="Field3" type="xs:date" minOccurs="0" />
              <xs:element name="Field4" type="xs:double" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

并阅读:

string query = "SELECT Field1, Field2, Field3, Field4 FROM [" + row["TABLE_NAME"].ToString() + "]";

DataSet ds = new DataSet();
ds.ReadXmlSchema(@".\MySchema.xsd");
OleDbDataAdapter data = new OleDbDataAdapter(query, Connection);
data.Fill(ds);
ds.Tables[0].TableName = row["TABLE_NAME"].ToString().Replace("$", string.Empty);`

日期格式必须对系统设置有效 短日期设置为“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:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Table">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Field1" type="xs:int" minOccurs="0" />
              <xs:element name="Field2" type="xs:string" minOccurs="0" />
              <xs:element name="Field3" type="xs:date" minOccurs="0" />
              <xs:element name="Field4" type="xs:double" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

and read:

string query = "SELECT Field1, Field2, Field3, Field4 FROM [" + row["TABLE_NAME"].ToString() + "]";

DataSet ds = new DataSet();
ds.ReadXmlSchema(@".\MySchema.xsd");
OleDbDataAdapter data = new OleDbDataAdapter(query, Connection);
data.Fill(ds);
ds.Tables[0].TableName = row["TABLE_NAME"].ToString().Replace("$", string.Empty);`

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文