繁体   English   中英

将整个表从SQL Server CE数据库复制到另一个表

[英]Copy a whole table from a SQL Server CE database to another

我正在开发C#应用程序,我想以编程方式将整个表从SQL Server CE数据库复制到另一个数据库。 我知道我可以在SQL Server中使用此命令,但是我不确定如何在C#中的一个查询中使用两个数据库连接。

Select * into DestinationDB.dbo.tableName from SourceDB.dbo.SourceTable

提前致谢。

您不会像在SQL Server中那样进行此操作,因为没有单个服务器可以同时管理两个数据库。 您的应用程序是链接两个数据库的唯一内容,因此数据必须通过您的应用程序。 您正在尝试以错误的方式进行操作,这就是为什么找不到解决方案的原因:您正在寻找错误的内容。

这里有一些例子。 我知道,因为我自己写的不止一个。 你只需要使用数据适配器来填充DataTable从第一数据库,然后数据适配器到的内容保存DataTable到第二个数据库。 如果数据源是同一类型,则您甚至可以只使用一个数据适配器,因为SelectCommandInsertCommand可以具有不同的连接。

using (var adapter = new SqlDataAdapter("SELECT statement here", "source connection string here"))
using (var destinationConnection = new SqlConnection("destination connection string here"))
using (var insertCommand = new SqlCommand("INSERT statement here", destinationConnection))
{
    // Add parameters to insertCommand here.

    adapter.InsertCommand = insertCommand;

    // Leave the RowState of all DataRows as Added so they are ready to be inserted.
    adapter.AcceptChangesDuringFill = false;

    var table = new DataTable();

    // Retrieve data from source and save to destination.
    adapter.Fill(table);
    adapter.Update(table);
}

该示例使用SqlClient,但对包括SqlServerCe在内的任何提供程序都起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM