首页 >  应用与开发  >  VisualStudio

远程数据库中的记录更新到本地数据库中C#源码

发布日期:2010-04-20 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace VoltageSystem.PL
{
    public partial class frmBackUpDB : Form
    {
        public frmBackUpDB()
        {
            InitializeComponent();
        }
       
        SqlConnection conn = null;
        SqlConnection conn1 = new SqlConnection("server="localhost\\SQLEXPRESS;database=VoltageMessage;uid=sa;pwd=666666"");

        private void button1_Click(object sender, EventArgs e)
        {
           
           
                try
                {
                    string sqlconn = "server=" + textBox1.Text.Trim() + "\\SQLEXPRESS;database=VoltageMessage;uid=sa;pwd=666666";

                    conn = new SqlConnection(sqlconn);
                    this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                    this.BackMessage();
                    this.BackTrueMonth();
                    this.BackFalseMonth();
                    this.Cursor = System.Windows.Forms.Cursors.Default;
                    MessageBox.Show("备份成功!");

                }
                catch (Exception ex)
                {
                    MessageBox.Show("备份失败,错误代码为:" + ex.Message);
                 
                }
                finally
                {
                    this.Close();
                }
                       
        }   

        private void BackMessage()
        {
            string sqlstr = "select * from MessageScout";
                string sqlstr1 = "select * from MessageScout";
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
                da.Fill(ds, "MessageScout");
                DataSet ds1 = new DataSet();
                SqlDataAdapter da1 = new SqlDataAdapter(sqlstr1, conn1);
                da1.Fill(ds1, "MessageScout");


               
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        ///更新
                        if (ds1.Tables[0].Rows[1][0].Equals(ds.Tables[0].Rows[j][0]))
                        {
                            string sql = "";

                            for (int n = 1; n < ds1.Tables[0].Columns.Count; n++)
                            {
                                sql = sql + "," + ds1.Tables[0].Columns[n] + "="'"" + ds.Tables[0].Rows[j][n] + "' ";

                            }
                            string sql1 = "update MessageScout set " + sql + "where ScoutID=" + ds1.Tables[0].Rows[1][0];
                            //加入去掉","语句!
       &