• C#将数据库导出成Excel,再从Excel导入到数据库中。


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.IO;
    namespace CindyDatabaseProcess
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Data.DataTable dt1 = null;
                System.Data.DataTable dt2 = null;
    
    
                SqlConnection conn = null;
                SqlCommand comm = null;
                SqlDataAdapter Adap = null;
                System.Data.DataSet ds = new System.Data.DataSet();
    
                string sqltext = "select * from product";
    
                conn = new SqlConnection("server=localhost;database=Cindy;Trusted_Connection=SSPI");
                conn.Open();
                comm = new SqlCommand(sqltext, conn);
                Adap = new SqlDataAdapter(comm);
                //ds = new System.Data.DataSet();
                Adap.Fill(ds, "product1");
                dt1 = ds.Tables["product1"];
    
                conn = new SqlConnection("server=localhost;database=db_AjaxWall;Trusted_Connection=SSPI");
                conn.Open();
                comm = new SqlCommand(sqltext, conn);
                Adap = new SqlDataAdapter(comm);
                //ds = new System.Data.DataSet();
                Adap.Fill(ds, "product2");
                dt2 = ds.Tables["product2"];
    
    
                foreach (System.Data.DataRow dr in dt1.Rows)
                {
                    string ziduan1 = dr[0].ToString();
                    string ziduan2 = dr[1].ToString();
                    string ziduan3 = dr[2].ToString();
                    string ziduan4 = dr[3].ToString();
    
                    System.IO.File.AppendAllText(@"C:UserslenovoDesktopxxx111.csv", ziduan1 + "," + ziduan2 + "," + ziduan3 + "," + ziduan4 + "
    ", Encoding.UTF8);
                }
                foreach (System.Data.DataRow dr in dt2.Rows)
                {
                    string ziduan1 = dr[0].ToString();
                    string ziduan2 = dr[1].ToString();
                    string ziduan3 = dr[2].ToString();
                    string ziduan4 = dr[3].ToString();
    
                    System.IO.File.AppendAllText(@"C:UserslenovoDesktopxxx111.csv", ziduan1 + "," + ziduan2 + "," + ziduan3 + "," + ziduan4 + "
    ", Encoding.UTF8);
                }
    
                string[] aa = File.ReadAllLines(@"C:UserslenovoDesktopxxx111.csv", Encoding.UTF8);
    
                StringBuilder sb=new StringBuilder();
                foreach (var item in aa)
                {
                    string[] ss = item.Split(',');
                    string sql=String.Format("insert into product values({0},'{1}',{2},'{3}');", ss[0], ss[1], ss[2], ss[3]);
                    sb.Append(sql);
                }
    
                comm = new SqlCommand(sb.ToString(), conn);
                int kk = comm.ExecuteNonQuery();
                Console.WriteLine(kk);
                Console.ReadKey();
    
            }
        }
    }
  • 相关阅读:
    intellij IDEA启动springboot项目报无效的源发行版错误解决方法
    JDBC调用oracle 存储过程
    Java自定义注解学习
    [Python3网络爬虫开发实战] 1.7.1-Charles的安装
    [Python3网络爬虫开发实战] 1.6.1-Flask的安装
    [Python3网络爬虫开发实战] 1.6.2-Tornado的安装
    [Python3网络爬虫开发实战] 1.5.4-RedisDump的安装
    [Python3网络爬虫开发实战] 1.5.3-redis-py的安装
    [Python3网络爬虫开发实战] 1.5.2-PyMongo的安装
    [Python3网络爬虫开发实战] 1.4.3-Redis的安装
  • 原文地址:https://www.cnblogs.com/cindy-2014/p/3709017.html
Copyright © 2020-2023  润新知