是否可以从 C# 创建 SQL Server 本机文件(如 BCP 本机格式)

发布于 2024-08-12 10:50:01 字数 142 浏览 2 评论 0原文

我们正在升级一个已有 15 年历史的代码库,需要创建一些本机 BCP 格式的数据文件。

在新系统中,我们理想地希望利用 C# DataTable 对象中的数据来创建本机 BCP 格式的数据文件。

这可以做到吗?如果可以,最好的方法是什么?

We are upgrading a 15 year old code-base, there is a requirement to create some native BCP formatted data files.

In the new system, we would ideally like to utilize data in a C# DataTable object to create the data file in native BCP format.

Can this be done and if so, what would be the best approach?

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

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

发布评论

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

评论(5

简美 2024-08-19 10:50:02

有一个类似的方法可以做到这一点,但是您必须引用 SQLDMO,它会创建一个与 BCP 相同的格式。通过使用 SQLDMO 的 BulkCopy 对象,您可以完成您想要的操作。下面是在 vbscript 中完成的例程的链接,该例程使用 SQLDMO 库此处

希望这有帮助,
此致,
汤姆.

There is a similar way to do this, but you have to reference SQLDMO and it will create an equivalent format just like BCP. By using the BulkCopy object of the SQLDMO, you can do what you are looking for. Here's a link to the routine done in vbscript which uses the SQLDMO library here.

Hope this helps,
Best regards,
Tom.

七堇年 2024-08-19 10:50:02

您可以将数据表中的数据放入 SQL Server 上的暂存区吗?如果是这样,您可以生成 BCP 进程。

BCP: http://msdn.microsoft.com/ en-us/library/aa174646%28SQL.80%29.aspx

例如,我使用:

BCP Database.Schema.TableName OUT FileName.Ext -S ServerName -T -n

开关:

  • -S 用于服务器
  • -T 用于可信连接
  • -n 用于本机格式

EDIT + New想法:

如果您有权访问填充数据表的查询,则可以使用 BCP 或 SQLDMO 来导出本机格式文件。以下查询使用 QUERYOUT 开关而不是 OUT 开关,因为它们包含内联查询

从视图导出特定列:

BCP "SELECT Column1, Column2 FROM MyViewName" QUERYOUT FileName.Ext -S ServerName -T -n

从 JOIN 导出特定列:

BCP "SELECT Table1.Column1, Table2.Column2 FROM Table1 INNER JOIN Table2 on Table1.Column33 = Table2.Column33" QUERYOUT FileName.Ext -S ServerName -T -n

Are you able to put the data in the datatable into a Staging area on a SQL Server? If so, you can spawn off a BCP process.

BCP: http://msdn.microsoft.com/en-us/library/aa174646%28SQL.80%29.aspx

For example, I use:

BCP Database.Schema.TableName OUT FileName.Ext -S ServerName -T -n

Switches:

  • -S is for server
  • -T is for trusted connection
  • -n is for Native format

EDIT + New Idea:

If you have access to the queries that fill up the datatable, you could use those with BCP or SQLDMO to export out a native format file. The following queries use the QUERYOUT switch instead of the OUT switch because they contain inline queries

Export specific columns from a view:

BCP "SELECT Column1, Column2 FROM MyViewName" QUERYOUT FileName.Ext -S ServerName -T -n

Export specific columns from a JOIN:

BCP "SELECT Table1.Column1, Table2.Column2 FROM Table1 INNER JOIN Table2 on Table1.Column33 = Table2.Column33" QUERYOUT FileName.Ext -S ServerName -T -n
策马西风 2024-08-19 10:50:02

假设您只修复了目标表中的列类型,没有可为空的内容,没有 unicode 字符串,并且愿意处理字节序,那么本机文件格式只是类型的字节。

我最近通过逐字节编写暂存文件并使用 BCP

bcp destTable in model.raw -T -S _serverName -n

model.raw 逐字节创建,从 ac# 脚本批量导入数据:

fileBytes = new byte[theLength * 4]; // * 4 bytes per element for int and float
var offset =0;

    foreach (var element in outputDimensions)
    {
        // fastCopy is a faster and "Unsafe" equivelent of BlockCopy , faster because it doesn't create an intermediate byte array.
        //Buffer.BlockCopy(BitConverter.GetBytes(profileid), 0, fileBytes, offset, 4);
        Utilities.fastCopy(profileid, fileBytes, offset);
        offset += 4;
        Utilities.fastCopy(element.index, fileBytes, offset);
        offset += 4;
        for (var i = 0; i < TimeSlices; i++, offset += 4)
        {
            float target = GetDataForTime(i,...);
            Utilities.fastCopy(target, fileBytes, offset); 
        }
    }

FileStream dataWriter.Write(fileBytes , 0, byteArray.Length);

assuming you only have fixed with column types in the destination table, nothing nullable, no unicode strings, and are willing to handle endian-ness, then the native file format is just the bytes of the types.

i recently bulk imported data from a c# script by writing a staging file byte-by-byte and using BCP

bcp destTable in model.raw -T -S _serverName -n

model.raw iscreated byte-wise by:

fileBytes = new byte[theLength * 4]; // * 4 bytes per element for int and float
var offset =0;

    foreach (var element in outputDimensions)
    {
        // fastCopy is a faster and "Unsafe" equivelent of BlockCopy , faster because it doesn't create an intermediate byte array.
        //Buffer.BlockCopy(BitConverter.GetBytes(profileid), 0, fileBytes, offset, 4);
        Utilities.fastCopy(profileid, fileBytes, offset);
        offset += 4;
        Utilities.fastCopy(element.index, fileBytes, offset);
        offset += 4;
        for (var i = 0; i < TimeSlices; i++, offset += 4)
        {
            float target = GetDataForTime(i,...);
            Utilities.fastCopy(target, fileBytes, offset); 
        }
    }

FileStream dataWriter.Write(fileBytes , 0, byteArray.Length);
ゃ人海孤独症 2024-08-19 10:50:01

不,这是不可能的,我们发现创建本机 BCP 文件的唯一方法是使用 SQL Server。如果有人开发或找到其他方法,请在此发布!

No, it can't be done, the only way we've discovered to create native BCP files is by using SQL Server. If anyone develops or finds another way please post it here!

如果没结果 2024-08-19 10:50:01

如果它那么旧,那么 BCP 块可能看起来非常像 Sybase BCP 块。在 Sybase 中,我开始查看客户端库并提供使用 BCP API 的代码示例。 CTLib 和/或相应 API 的 Java jar。对于 Microsoft 来说,可能存在涉及 BCP 的本机 C 或基本 API 的类似部分。您可能不需要 API 的通信部分,只需要逐条记录准备和读取/写入文件。

如果没有这样的东西,那么我会考虑使用手工制作的 FMT 文件和类似文本的数据文件(由原始 BCP 程序生成/使用)的非本机格式。

If it is that old, then BCP piece may look very much like a Sybase BCP piece. In Sybase I'd start looking at client libaries and shipped examples of code using BCP API. CTLib and/or Java jars for corresponding API. For Microsoft there might be a similar portions of native C or Basic API involving BCP. You may not need the communication part of API, just record-by-record preparing and reads/writes to file.

If there is no such thing, then I'd consider non-native format with hand made FMT files and text-like data files, produced/consumed by original BCP program.

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