SqlBulkCopy 可以从 Sql 选择创建表吗

发布于 2024-09-28 02:29:30 字数 43 浏览 0 评论 0原文

SqlBulkCopy 可以创建类似于 SELECT INTO 的表吗?

Can SqlBulkCopy create a table, kind of like a SELECT INTO?

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

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

发布评论

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

评论(2

感情旳空白 2024-10-05 02:29:30

我认为上面的答案不太清楚。

您必须使用 SQL 创建表。没有其他办法。如果您只需要创建列结构,那么如果您的源位于同一服务器中,则非常简单,这样做就足够了:

Select * from source_table into destination_table where 1=2

如果您的源不在同一服务器中(例如,它是 excel 或 dbf 文件或其他文件) ,最简单的事情是用 ODBC(或 SQL,如果可能的话)连接到它,并向他发送:

    Select * from source_table where 1=2

,然后将结果收集到 DataTable 中。然后在第二步中,您应该在目标服务器上创建存储过程,该存储过程将该表作为参数,然后将其插入到新表中。

更准确地说,尝试使用 SQL 过程: http://www.builderau.com.au/program/sqlserver/soa/Passing-table-valued-parameters-in-SQL-Server-2008/ 0,339028455,339282577,00.htm

并在c#中创建SqlCommnand对象并将其添加到其Parameters集合SqlParameter即SqlDbType.Structured

我没有详细介绍每一个细节,但希望它能有所帮助。

I think answer above wasn't quite clear.

You must create table with SQL. There is no other way. And if you need just to create column structure, then it is quite simple if your source is in the same server, it is enough to do this:

Select * from source_table into destination_table where 1=2

If your source is not in same server (e.g. it is excel or dbf file or whatever), the easiest thing to do is to connect to it with ODBC (or SQL if possible), and send him:

    Select * from source_table where 1=2

and then collect result into DataTable. Then in second step you should create stored procedure on your destination server that will take that table as argument and then insert it into new table.

A bit more precisely, try this for SQL procedure: http://www.builderau.com.au/program/sqlserver/soa/Passing-table-valued-parameters-in-SQL-Server-2008/0,339028455,339282577,00.htm

And create SqlCommnand object in c# and add to its Parameters collection SqlParameter that is SqlDbType.Structured

I didn't go into every single detail, but hope that it can help.

梦幻的味道 2024-10-05 02:29:30

看来SqlBulkCopy不能自己创建表。必须预定义目标表。如果目标端有自动增量标识(int),只需在 select 语句中使用 1,即

SELECT 
    1, 
    [ColumnName],
    [ColumnName]...
FROM TABLENAME

SQL Server 将自行处理自动增量。

It seems that SqlBulkCopy can not create tables by itself. The destination table has to be predefined. In the case where the destination has got an auto incremental identity (int), just use a 1 in the select statement i.e.

SELECT 
    1, 
    [ColumnName],
    [ColumnName]...
FROM TABLENAME

SQL Server will handle the auto increment by itself.

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