使用 oledb 数据类型将数据写入 Excel 工作表时出现问题

发布于 2024-09-15 22:07:05 字数 386 浏览 0 评论 0原文

我正在尝试使用从 MYSQL 数据库获取的 oledb 数据适配器将一些数据插入到 Excel 工作表中。从 mysql 数据库获取的数据包含非常长的文本,其数据类型在 MYSQL 中已定义为(Varchar(1023)、文本、长文本等)。当我尝试将这些传递给 oledb Dataadapter 我尝试使用大小为 5000 的 oledb.VarWChar、oledb.LongVarWChar 等。但是当我尝试运行 da.update(...) 命令时,出现以下异常。

该字段太小,无法接受您尝试添加的数据量。尝试插入或粘贴更少的数据

我无法理解应该在 oledb 中使用哪些数据类型和哪些大小来映射到这些长文本值。

有人可以帮我解决这个问题吗?

谢谢。

I am trying insert some data into excel sheet using oledb dataadapter which is obtained from MYSQL Db.This data obtained from mysql db contains very long texts whose datatypes in MYSQL have been defined as(Varchar(1023),Text,Longtext etc).When I try to pass these to the oledb Dataadapter I tried to use oledb.VarWChar,oledb.LongVarWChar with size 5000 and so on.But I am getting the following exception when I try to run da.update(...) command.

The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data

I am having trouble understanding what datatypes with what sizes should I use in oledb to map to these long text values.

Could someone please help me with this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

扬花落满肩 2024-09-22 22:07:05

我正在做类似的事情,并且对来自 SQL Server 的 varchar(max) 数据类型遇到了相同的错误。不过,数据来自哪里并不重要。当您从数据库获取数据时,您需要定义列数据类型和大小的架构。我通过在我正在使用的数据适配器上调用 FillSchema 来完成此操作 -

 DataTable dt = new DataTable();
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 da.Fill(dt);
 da.FillSchema(dt, SchemaType.Source);
 return dt;

如果需要,您还可以单独设置列属性。

然后,我循环遍历 DataTable 中的每一列,并使用 ADOX.NET 设置要通过 oleDB 导出的列。您不必使用 ADOX.NET,这里的主要概念是使用来自原始数据库的大小。

    foreach (DataColumn col in dt.Columns)
    {
       columnList.Append(dt.TableName + "." + col.ColumnName + ", ");
       ADOX.Column adoxCol = new Column();
       adoxCol.ParentCatalog = cat;
       adoxCol.Name = col.ColumnName;
       adoxCol.Type = TranslateType(col.DataType, col.MaxLength);
       int size = col.MaxLength > 0 ? col.MaxLength : 0;
       if (col.AllowDBNull)
       {
          adoxCol.Attributes = ColumnAttributesEnum.adColNullable;
       }
       adoxTable.Columns.Append(adoxCol, adoxCol.Type, size);
    }

以下是我的 TranslateType 方法的一个片段,用于确定是否使用 LongVarWChar 或 VarWChar。这些数据类型是 oleDB 数据类型的 ADOX.NET 版本。我相信任何超过 4000 个字符的内容都应该使用 LongVarWChar 类型,但我对此不确定。您没有提到哪个版本的 Excel 是您的目标,但我在 Excel 2003 和 Excel 2007 上都可以使用此功能。LongVarWChar

    case "System.String":
       if (maxLength > 4000)
       {
          return DataTypeEnum.adLongVarWChar;
       }
       return DataTypeEnum.adVarWChar;

可以采用可容纳 2 GB 的大尺寸。所以不用担心尺寸太大。

I am doing something similar and ran into the same error with varchar(max) data types that come from SQL Server. It doesn't matter where the data is coming from though. When you get the data from your database, you need to define the schema for the column data types and sizes. I do this by calling FillSchema on the data adapter that I am using -

 DataTable dt = new DataTable();
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 da.Fill(dt);
 da.FillSchema(dt, SchemaType.Source);
 return dt;

You could also set the column properties individually, if you wanted.

Then I loop through each column in my DataTable and set up my columns for export with oleDB using ADOX.NET. You don't have to use ADOX.NET, the main concept here is to use the sizes that came from the original database.

    foreach (DataColumn col in dt.Columns)
    {
       columnList.Append(dt.TableName + "." + col.ColumnName + ", ");
       ADOX.Column adoxCol = new Column();
       adoxCol.ParentCatalog = cat;
       adoxCol.Name = col.ColumnName;
       adoxCol.Type = TranslateType(col.DataType, col.MaxLength);
       int size = col.MaxLength > 0 ? col.MaxLength : 0;
       if (col.AllowDBNull)
       {
          adoxCol.Attributes = ColumnAttributesEnum.adColNullable;
       }
       adoxTable.Columns.Append(adoxCol, adoxCol.Type, size);
    }

Here is a snippet from my TranslateType method that determines whether or not to use the LongVarWChar or VarWChar. These data types are the ADOX.NET version of the oleDB data types. I believe that anything over 4000 characters should use the LongVarWChar type but I'm not sure about that. You didn't mention which version of Excel is your target, but I have this working with both Excel 2003 and Excel 2007.

    case "System.String":
       if (maxLength > 4000)
       {
          return DataTypeEnum.adLongVarWChar;
       }
       return DataTypeEnum.adVarWChar;

The LongVarWChar can take large sizes that can accomadate 2 GB. So don't worry about making the size too big.

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