简体   繁体   English

在C#中将SQL查询的结果从一个数据库插入另一个数据库

[英]Inserting result of a SQL query from one database to another in C#

How can I take the result of a select statement from one database table and insert it into another database table - using C#? 如何使用C#从一个数据库表中获取select语句的结果并将其插入到另一个数据库表中?

The problem is I need to use 2 different connection strings in the C# code. 问题是我需要在C#代码中使用2个不同的连接字符串。 This is what I have so far but isnt working.. 到目前为止,这是我目前无法使用的..

        string sCMD_All = "SELECT * FROM table";
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();

        using (SqlConnection myConn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand myCommand = new SqlCommand(sCMD_All, myConn))
            {
                myConn.Open();
                SqlDataReader reader = myCommand.ExecuteReader();
                da.Fill(ds);
                myConn.Close();
            }
        }
        DataTable sqTable = ds.Tables[0];

        //insert into server database
        DataTable newTable = new DataTable();
        newTable = sqTable;
        using (SqlConnection myConn = new SqlConnection(ConnectionString_M))
        {
            string sCMD_I = "INSERT INTO tableNew @newTable";

            using (SqlCommand myCommand = new SqlCommand(sCMD_I, myConn))
            {
                myConn.Open();
                SqlDataReader reader = myCommand.ExecuteReader();
                myConn.Close();
            }
        }

The problem is I need to use 2 different connection strings in the C# code. 问题是我需要在C#代码中使用2个不同的连接字符串。 This is what I have so far but isnt working.. 到目前为止,这是我目前无法使用的..

    string sCMD_All = "SELECT * FROM table";
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();

    using (SqlConnection myConn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand(sCMD_All, myConn))
        {
            myConn.Open();
            SqlDataReader reader = myCommand.ExecuteReader();
            da.Fill(ds);
            myConn.Close();
        }
    }
    DataTable sqTable = ds.Tables[0];

    //insert into server database
    DataTable newTable = new DataTable();
    newTable = sqTable;
    using (SqlConnection myConn = new SqlConnection(ConnectionString_M))
    {
        string sCMD_I = "INSERT INTO tableNew @newTable";

        using (SqlCommand myCommand = new SqlCommand(sCMD_I, myConn))
        {
            myConn.Open();
            SqlDataReader reader = myCommand.ExecuteReader();
            myConn.Close();

Use SELECT ... INTO ... with a fully qualified table names database.schema.object_name , like this: SELECT ... INTO ...与完全限定的表名database.schema.object_name ,如下所示:

USE DatabaseName;

SELECT *
FROM DatabaseName.schemaname.Tablename
INSERT INTO AnotherDatabase.schemaname.AnotherTablename;

Then you can use ADO.net or other API to execute this query from C#. 然后,您可以使用ADO.net或其他API从C#执行此查询。

SqlCommand Data = new SqlCommand("Select * FROM " + DBTableName + " WHERE " + DBColName + " = " + ColumnId + ";", this.con);
SqlDataAdapter SqAdptr = new SqlDataAdapter(Data);
DataSet SqDataset = new DataSet();
SqAdptr.Fill(SqDataset);
DataTable sqTable = SqDataset.Tables[0];

Transfer this table to new Datatable 将此表转移到新的数据表

DataTable NewTable = new DataTable();
NewTable = sqlTable;
INSERT INTO [INSERTDATABASE].[dbo].[INSERTTABLE] 
SELECT * FROM [CURRENTDATABASE].[dbo].[CURRENTTABLE] 

If you can't/don't want to use Linked Server because permission or performance, after you got the DataTable object, you can call the following function to insert the data to the 2nd database which is located in different server. 如果由于权限或性能而不能/不想使用Linked Server,则在获得DataTable对象后,可以调用以下函数将数据插入位于不同服务器的第二数据库中。 It works for me. 这个对我有用。

CopyDataFromOneToOther(DB2, myDataTable, "tableName");


private static void CopyDataFromOneToOther(String sConnStr, DataTable dt, String sTableName)
{
    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sConnStr, SqlBulkCopyOptions.TableLock)) {
    bulkCopy.DestinationTableName = sTableName;
    bulkCopy.WriteToServer(dt);
    }                    
}

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

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