C # Simple Code Transfer Database data

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Data;
Using System.Data.SqlClient;
Using System.Data.Common;

Namespace WangwoSoft.ShejiWorld.WebSite.GeneralHandler
{
<summary>
Summary description of Memberhandler
</summary>
public class Memberhandler:ihttphandler
{

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
The name of the table to copy
string table = "Member_user";

Constructing the connection string
SqlConnectionStringBuilder SqlConnect1 = new SqlConnectionStringBuilder ();
Sqlconnect1.datasource = "211.149.***.***";
Sqlconnect1.initialcatalog = "* * * *";//target database
Sqlconnect1.integratedsecurity = false;
Sqlconnect1.userid = "* * * *";//Login ID
Sqlconnect1.password = "*******";//Database Password

SqlConnectionStringBuilder sqlConnect2 = new SqlConnectionStringBuilder ();
Sqlconnect2.datasource = "211.149.***.***";
Sqlconnect2.initialcatalog = "* * * * *";//Source database
Sqlconnect2.integratedsecurity = false;//When True for Windows authentication
Sqlconnect2.userid = "*******";//Login ID
Sqlconnect2.password = "*******";//Database Password

Call the Copy database function
string result = Inserttable (sqlconnect1.connectionstring, sqlconnect2.connectionstring, table);
Context. Response.Write (Result);
}

Connection string with a parameter of two databases
private string Inserttable (string constring1,string constring2,string tabstring)
{
Connecting to a 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" +TABSTRING,CONN1);
DataSet DataSet1 = new DataSet ();
if (dataset1!=null)
{
Adapter1. Fill (DataSet1, tabstring);
}

SqlDataAdapter adapter2 = new SqlDataAdapter ("select * from" + tabstring, CONN2);
DataSet DataSet2 = new DataSet ();

SqlCommand cmd2 = new SqlCommand ("Select COUNT (*) from" +tabstring,conn2);

Object res2 = cmd2. ExecuteScalar ();
if (res2!=null)
{
int ncount = Convert.ToInt32 (res2. ToString ());
if (ncount==0)
{
Conn1. Close ();
Conn2. Close ();
Return "no data";
}
}

Fill DataSet2
if (dataset2!=null)
{
Adapter2. Fill (DataSet2, tabstring);
}

Copying data
for (int i = 0; i < dataset2.tables[0]. Rows.Count; i++)
{
Dataset1.tables[0]. Loaddatarow (Dataset2.tables[0]. Rows[i]. ItemArray, false);
}

To display a dataset transformation in its associated target database
SqlCommandBuilder cb = new SqlCommandBuilder (adapter1);
Adapter1. Update (DataSet1, tabstring);
Cb. RefreshSchema ();

Return "TABLE" + tabstring + "Copy succeeded!" ";
Conn1. Close ();
Conn2. Close ();
}


public bool IsReusable
{
Get
{
return false;
}
}
}
}

C # Simple Code Transfer Database data

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.