数据从oracle导入sql server

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data.OracleClient;
    using System.Text;
    public partial class Default2 : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
        //string connstr = "Server=*******;Database=*******;uid=*******;pwd=*******";//中间库数据链接字符串
    string connstr = "User ID=*******;Password=*******;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = *******)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = SWAP)))";
    string connstrn = "Server=*******;Database=*******;uid=*******;pwd=*******";//内网数据链接字符串
    string connstrw = "Server=*******;Database=*******;uid=*******;pwd=*******";//外网库数据链接字符串
    OracleConnection conn = new OracleConnection(connstr);
    SqlConnection connw = new SqlConnection(connstrw);
    SqlConnection connn = new SqlConnection(connstrn);
    try
    {
        conn.Open();

     
        string maxid = System.IO.File.ReadAllText(@"D:\web\swdx\maxid.txt");

        string sql = " select *  from sycomk.D_NEWSARTICLE_ALL where info_id > "+maxid+" order by info_id asc ";
        OracleCommand sc = new OracleCommand(sql, conn);
        OracleDataReader sr = sc.ExecuteReader();
        string strn = "";
        string strw = "";
        int i = 0;
        int j = 0;
        
        while (sr.Read())
        {
            if (i == 0) {
                System.IO.File.WriteAllText(@"D:\web\swdx\maxid.txt", sr["info_id"].ToString(), Encoding.UTF8);
            }

            string Title = sr["BT"].ToString().Replace("'", "'");
            string AddTime = sr["CTDATE"].ToString();
            string content = sr["NRCONTENT"].ToString().Replace("'", "'");
            string suggest = sr["SFTJ"].ToString();
            string subtitle = sr["SDTITLE"].ToString().Replace("'", "'");
            string author = sr["AUTHOR"].ToString().Replace("'", "'");
            if (!String.IsNullOrEmpty(sr["COLUMNTYPE2"].ToString()))//外网数据
            {
                
                strw += " insert into catalog_001_Item (Title,Content,AddTime,NodeID,CategoryID,AddUserID,NotDisplay) values ('" + Title + "','" + content + "','" + AddTime + "','" + sr["COLUMNTYPE2"].ToString() + "',0,'1','0') ";
                strw += " update catalog_001_Item set OrderNum = (select max(OrderNum)+1 from catalog_001_Item where NodeID = " + sr["COLUMNTYPE2"].ToString() + " ) where ItemID = @@IDENTITY  ";
                i++;
            }


            if (!String.IsNullOrEmpty(sr["COLUMNTYPE"].ToString()))
            { //内网数据
                strn += " insert into catalog_001_Item (Title,Content,AddTime,NodeID,CategoryID,AddUserID,NotDisplay) values ('" + Title + "','" + content + "','" + AddTime + "','" + sr["COLUMNTYPE"].ToString() + "',0,'1','0') ";
                strn += " update catalog_001_Item set OrderNum = (select max(OrderNum)+1 from catalog_001_Item where NodeID = " + sr["COLUMNTYPE"].ToString() + " ) where ItemID = @@IDENTITY  ";
                j++;
            }
        }

        if (strw != "")
        {
            connw.Open();
            SqlCommand scw = new SqlCommand(strw, connw);
            int lenghw = scw.ExecuteNonQuery();
            Response.Write("导入外网数据" + i + "条       ");
            connw.Close();
        }
        else {
            Response.Write("导入外网数据0条       ");
        }

        if (strn != "")
        {
            connn.Open();
            SqlCommand scn = new SqlCommand(strn, connn);
            int lenghn = scn.ExecuteNonQuery();
            Response.Write("导入内网数据" + j + "条.");
            connn.Close();
        }
        else {
            Response.Write("导入内网数据0条.");
        }

       
        conn.Close();
        Response.Write("
更新完成"); } catch (Exception ex) { conn.Close(); connw.Close(); connn.Close(); Response.Write(ex.Message.ToString()); } }

}

结果:
QQ截图20171221112910.jpg

txt文件:
QQ截图20171221113028.jpg

你可能感兴趣的:(数据从oracle导入sql server)