C # replicating databases, transferring database data to another database

Source: Internet
Author: User

This article takes a table as an example, to go to multiple tables, you can associate a dataset with multiple tables, and the complete code, including references and the main and copy functions, is given below.

To illustrate, you must first replicate the table structure with SQL statements in order to successfully replicate the data using the following code.

Using system;using system.collections.generic;using system.linq;using system.text;using System.Data;using System.data.sqlclient;using System.data.common;namespace copydata{class Program {static void Main (string[]                        args) {//Table name to copy string table = "V_position";            Constructs a connection string sqlconnectionstringbuilder Builder1 = new SqlConnectionStringBuilder (); Builder1.      DataSource = ". \\CANFLY"; The instance name is Canfly builder1.    InitialCatalog = "Desdata"; Target database Builder1.     IntegratedSecurity = true;            Use Windows authentication SqlConnectionStringBuilder builder2 = new SqlConnectionStringBuilder (); Builder2.            DataSource = ". \\CANFLY"; Builder2.     InitialCatalog = "Bddata"; SOURCE database Builder2.            IntegratedSecurity = true; Call the Copy database function inserttable (builder1. ConnectionString, Builder2.        ConnectionString, table);        A connection string with a parameter of two databasesprivate static void Inserttable (String conString1, String conString2, String tabstr) {//Connection database            SqlConnection conn1 = new SqlConnection (); Conn1.            ConnectionString = conString1; Conn1.            Open ();            SqlConnection conn2 = new SqlConnection (); Conn2.            ConnectionString = conString2; Conn2.            Open ();            Fill DataSet1 SqlDataAdapter adapter1 = new SqlDataAdapter ("select * from" + tabstr, conn1);            DataSet DataSet1 = new DataSet (); if (DataSet1! = null) {Adapter1.            Fill (DataSet1, TABSTR);            } SqlDataAdapter adapter2 = new SqlDataAdapter ("select * from" + tabstr, CONN2);            DataSet DataSet2 = new DataSet ();            SqlCommand cmd2 = new SqlCommand ("SELECT count (*) from" + Tabstr, CONN2); Object res2 = cmd2.            ExecuteScalar (); if (res2! = null) {int ncount = Convert.ToInt32 (res2. TostriNg ()); if (ncount = = 0) {conn1.                    Close (); Conn2.                    Close ();                Return }}//Fills DataSet2 if (DataSet2! = null) {Adapter2.            Fill (DataSet2, TABSTR); }//Copy data for (int j = 0; J < Dataset2.tables[0]. Rows.Count; J + +) {Dataset1.tables[0]. Loaddatarow (Dataset2.tables[0]. ROWS[J].            ItemArray, false);            }//The dataset transform is displayed in its associated target database SqlCommandBuilder cb = new SqlCommandBuilder (adapter1); Adapter1.            Update (DataSet1, TABSTR); Cb.            RefreshSchema (); Console.WriteLine ("Table" + tabstr + "copy succeeded!")            "); Conn1.            Close (); Conn2.        Close (); }    }}


C # replicating databases, transferring database data to another database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.