设为首页 加入收藏

TOP

C#同步SQLServer数据库中的数据--数据库同步工具[同步新数据](一)
2014-11-24 02:54:41 来源: 作者: 【 】 浏览:12
Tags:同步 SQLServer 数据库 数据 工具

C#同步SQL Server数据库中的数据

1. 先写个sql处理类:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace PinkDatabaseSync
{
    class DBUtility : IDisposable
    {
        private string Server;
        private string Database;
        private string Uid;
        private string Password;
        private string connectionStr;
        private SqlConnection mySqlConn;

        public void EnsureConnectionIsOpen()
        {
            if (mySqlConn == null)
            {
                mySqlConn = new SqlConnection(this.connectionStr);
                mySqlConn.Open();
            }
            else if (mySqlConn.State == ConnectionState.Closed)
            {
                mySqlConn.Open();
            }
        }

        public DBUtility(string server, string database, string uid, string password)
        {
            this.Server = server;
            this.Database = database;
            this.Uid = uid;
            this.Password = password;
            this.connectionStr = "Server=" + this.Server + ";Database=" + this.Database + ";User Id=" + this.Uid + ";Password=" + this.Password;
        }

        public int ExecuteNonQueryForMultipleScripts(string sqlStr)
        {
            this.EnsureConnectionIsOpen();
            SqlCommand cmd = mySqlConn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sqlStr;
            return cmd.ExecuteNonQuery();
        }
        public int ExecuteNonQuery(string sqlStr)
        {
            this.EnsureConnectionIsOpen();
            SqlCommand cmd = new SqlCommand(sqlStr, mySqlConn);
            cmd.CommandType = CommandType.Text;
            return cmd.ExecuteNonQuery();
        }


        public object ExecuteScalar(string sqlStr)
        {
            this.EnsureConnectionIsOpen();
            SqlCommand cmd = new SqlCommand(sqlStr, mySqlConn);
            cmd.CommandType = CommandType.Text;
            return cmd.ExecuteScalar();
        }

        public DataSet ExecuteDS(string sqlStr)
        {
            DataSet ds = new DataSet();
            this.EnsureConnectionIsOpen();
            SqlDataAdapter sda= new SqlDataAdapter(sqlStr,mySqlConn);
            sda.Fill(ds);
            return ds;
        }

        public void BulkCopyTo(string server, string database, string uid, string password, string tableName, string primaryKeyName)
        {
            string connectionString = "Server=" + server + ";Database=" + database + ";User Id=" + uid + ";Password=" + password;
            // Create destination connection
            SqlConnection destinationConnector = new SqlConnection(connectionString);

            SqlCommand cmd = new SqlCommand("SELECT * FROM " + tableName, destinationConnector);
            // Open source and destination connections.
            this.EnsureConnectionIsOpen();
            destinationConnector.Open();

            SqlDataReader readerSource = cmd.ExecuteReader();
            bool isSourceContainsData = false;
            string whereClause = " where ";
            while (readerSource.Read())
            {
                isSourceContainsData = true;
                whereClause += " " + primaryKeyName + "=" + readerSource[primaryKeyName].ToString() + " or ";
            }
            whereClause = whereClause.Remove(whereClause.Length - " or ".Length, " or ".Length);
            readerSource.Close();

            whereClause = isSourceContainsData   whereClause : string.Empty;

            // Select data from Products table
            cmd = new SqlCommand("SELECT * FROM " + tableName + whereClause, mySqlConn);
            // Execute reader
            SqlDataReader reader = cmd.ExecuteReader();
            // Create SqlBulkCopy
            SqlBulkCopy bulkData = new SqlBulkCopy(destinationConnector);
            // Set destination table name
            bulkData.DestinationTableName = tableName;
            // Write data
            bulkData.WriteToServer(reader);
            // Close objects
            bulkData.Close();
            destinationConnector.Close();
            my
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SQL-Oracle08sql序列和同义词 下一篇锋利的SQL-SQLServer的表旋转(行..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java多线程基础 - 华 (2025-12-24 13:20:10)
·深入解读Java多线程 (2025-12-24 13:20:08)
·Spring Boot 中文文档 (2025-12-24 13:20:04)
·如何理解智能指针? (2025-12-24 12:48:26)
·c++是否应避免使用普 (2025-12-24 12:48:23)