1

如何从一个数据库表中获取 select 语句的结果并将其插入到另一个数据库表中 - 使用 C#?

问题是我需要在 C# 代码中使用 2 个不同的连接字符串。这是我到目前为止但没有工作的..

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

问题是我需要在 C# 代码中使用 2 个不同的连接字符串。这是我到目前为止但没有工作的..

    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();
4

4 回答 4

4

SELECT ... INTO ...与完全限定的表名一起使用database.schema.object_name,如下所示:

USE DatabaseName;

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

然后您可以使用 ADO.net 或其他 API 从 C# 执行此查询。

于 2013-02-04T08:52:39.177 回答
1
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];

将此表转移到新的数据表

DataTable NewTable = new DataTable();
NewTable = sqlTable;
于 2013-02-04T08:54:16.573 回答
0

如果您因为权限或性能而不能/不想使用 Linked Server,则在获得 DataTable 对象后,您可以调用以下函数将数据插入到位于不同服务器的第二个数据库中。这个对我有用。

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);
    }                    
}
于 2014-12-15T17:38:16.323 回答
0
INSERT INTO [INSERTDATABASE].[dbo].[INSERTTABLE] 
SELECT * FROM [CURRENTDATABASE].[dbo].[CURRENTTABLE] 
于 2013-02-04T08:54:32.573 回答