• 数据数据库跨数据库批量导入数据


    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!

        要求做一个序程,将数据库newmsa中的表CptCapital中的数据选取部份字段值,导入到数据库hangyun中表ShipInfo_T中,并证保字段的一致性。

        大致路思:

        1、与newmsa立建连接,查询所需数据,保存到一张DataTable中

        2、与hangyun立建连接,将DataTable中的数据导入到应相的字段中

        每日一道理
    最为值得珍惜的是今天,因为最容易流逝的就是今天,把握今天就是把握希望,分分秒秒只是瞬间,而所乘载的分分秒秒就叫做一天,时间的流逝往往是在不经意之间,人生几回,青春更珍贵,对于我们这个年龄的青少年来说,青春已不足二十载,在学习的生活中我们必须靠自己的力量,驾驭着自己的小船驶向希望的彼岸。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace 批量数据操纵_北京到长江_
    {
        class Program
        {
    		//置配连接字符串
           public static string ConnString = @"Data Source=Q-PC\SQLEXPRESS;Integrated Security=SSPI;uid=sa;pwd=;Initial Catalog=hangyun";
           public static string ConnString1 = @"Data Source=Q-PC\SQLEXPRESS;Integrated Security=SSPI;uid=sa;pwd=;Initial Catalog=newmsa";
            static void Main(string[] args)
            {
                Program p = new Program();
                p.batchInsertData_ShipInfo();//导入数据
                Console.WriteLine("导入胜利!");
                Console.ReadLine();
            }
    		//批量处置数据
            public Boolean batchInsertData_ShipInfo()
            {
                DataTable dt = new DataTable();
                //取获
                string sql = "select Id,Textfield5,Name,capitaltypeid,Customerid,Numberfield1,Numberfield2,depreyear,deprerate,Isinner,manufacturer,Unitid,Textfield3,departmentid from CptCapital";
                sql += " where departmentid='10' or departmentid='140' or departmentid='141' or departmentid='142' or departmentid='143' or departmentid='144'";
                sql += " or departmentid='145' or departmentid='146' or departmentid='147' or departmentid='148' or departmentid='23' or departmentid='53' or departmentid='74'";
                dt =GetTable(sql);
    
                //导入
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string strsql;
                    int year, month;
                    year = Convert.ToInt32(dt.Rows[i]["depreyear"]);
                    month = Convert.ToInt32(dt.Rows[i]["deprerate"]);
    
                    DateTime date = new DateTime(0);
                    date = date.AddYears(year);
                    date = date.AddMonths(month);
                    
    				strsql = "insert into ShipInfo_T(ShipID,Shipnum,ShipName,ShipType,companyNum,HostPower,loadquota,Builddate,isexist,RegistryPort,SailArea,ShipOwner,SMCInstitution)values('";
                    strsql = strsql + dt.Rows[i]["Id"].ToString() + "','" + dt.Rows[i]["Textfield5"].ToString() + "','" + dt.Rows[i]["Name"].ToString() + "','";
                    strsql += dt.Rows[i]["capitaltypeid"].ToString() + "','" + dt.Rows[i]["Customerid"].ToString() + "','" + dt.Rows[i]["Numberfield1"].ToString() + "','";
                    strsql += Convert.ToInt32(dt.Rows[i]["Numberfield2"]) + "','" + date.ToString("yyyy-MM-dd") + "','" + dt.Rows[i]["Isinner"].ToString() + "','";
                    strsql += dt.Rows[i]["manufacturer"].ToString() + "','" + dt.Rows[i]["Unitid"].ToString() + "','" + dt.Rows[i]["Textfield3"].ToString() + "','" + dt.Rows[i]["departmentid"].ToString() + "')";
    
                    ExecuteInsertOrUpdateSql(strsql);
                }
                return true;
            }
    		//从数据库中取得所需数据,并返回DataTable
            public DataTable GetTable(string strSql)
            {
                SqlConnection conn = new SqlConnection(ConnString1);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
                DataTable dt = new System.Data.DataTable();
                try
                {
                    da.Fill(dt);
                    return dt;
                }
                catch (Exception e)
                {
                    throw new Exception("行执任务失败:" + e.Message + "   " + strSql);
                }
                finally
                {
                    conn.Close();
                }
            }
    
            #region 行执添加,改修,删除的时候停止证验的方法
            /// <summary>
            /// 行执添加的时候停止证验的方法,返回结果的sql更新句语
            /// </summary>
            /// <param name="strSql">要需行执的sql句语</param>
            /// 返回一个boolean型的值
             public Boolean ExecuteInsertOrUpdateSql(string strSql)
            {
                SqlConnection conn = new SqlConnection(ConnString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSql, conn);
                try
                {
                    int count = cmd.ExecuteNonQuery();
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("行执任务失败:" + e.Message + "   " + strSql);
    
                }
                finally
                {
                    conn.Close();
                    cmd.Dispose();
                }
            }
            #endregion
        }
    }

    文章结束给大家分享下程序员的一些笑话语录: 神灯新篇
    一个程序员在海滩上发现了一盏神灯。他在灯上擦了几下,一个妖怪就从灯里跳出来说:“我是世界上法术最强的妖怪。我可以实现你的任何梦想,但现在,我只能满足你一个愿望。”程序员摊开了一幅中东地图说:“我想让中东得到永久的和平。”妖怪答道:“哦,我没办法。自打创世纪以来,那里的战火就没有停息过。这世上几乎没有我办不到的事,但这件事除外。”程序员于是说:“好吧,我是一个程序员,为许多用户编写过程序。你能让他们把需求表述得更清楚些,并且让我们的软件项目有那么一两次按进度按成本完成吗?”妖怪说:“唔,我们还是来看中东地图吧。”

  • 相关阅读:
    使用一行Python代码从图像读取文本
    FastAI 简介
    OpenCV-Python 轮廓:更多属性 | 二十四
    Numpy和OpenCV中的图像几何变换
    从云计算到边缘计算
    OpenCV-Python 轮廓属性 | 二十三
    c# GetType()和typeof()的区别
    C# 子类父类方法同名,三种处理方式
    C# string 与 String的区别
    decimal
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3050689.html
Copyright © 2020-2023  润新知