简体   繁体   中英

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#?

The problem is I need to use 2 different connection strings in the C# code. 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. 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:

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#.

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. 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);
    }                    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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