将sqlserver中数据导入到oracle数据中

 以下贴出部分代码:C#

需要引用

using System.Data.OracleClient;
using System.Data.SqlClient;

 

private void button3_Click(object sender, EventArgs e)       //将sqlserver中数据导入到oracle数据中
        {
            button3.Enabled = false;                               //防止多次点击
            int temp=0;                                            //用于判断插入是否成功
            string sqlstr;                                         //接收插入语句
            DataTable dt = new DataTable();
            string sql = "select * from dictionary";             // oracle中已存在的数据表
            Sqldataload sqldata = new Sqldataload();       //自定义类
            dt = sqldata.ORselect(sql);
            string[] col = new string[3];                     //用于接收没一行中的数据
            if (dt.Rows.Count > 0)                
            {
                for (int i = 0; i < dt.Rows.Count; i++)         //循环每一行
                {
                    for (int j = 0; j < dt.Columns.Count; j++)     //循环每一列
                    {
                        col[j] = dt.Rows[i][j].ToString();       
                    }
                    sqlstr = "Insert into Tdictionary(EnField,ChField,Method) values(" + StrTo(col[0]) + "," + StrTo(col[1]) + "," + StrTo(col[2]) + ")";
                    Dataconn ORconn = new Dataconn();
                    temp = ORconn.ORUpdate(sqlstr);            //将数据插入到oracle数据中
                }
            }
            button3.Enabled = true;
            if (temp!=-1) MessageBox.Show("转换成功!!");   
            else
                MessageBox.Show("转换失败!!");
        }

  public string StrTo(string str)
        {
            str = "'" + str + "'";
            return str;
        }

 

自定义类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;


namespace ExceltoOracleTable
{
    class Dataconn
    {
        public Dataconn()
        {
        }
        public   string ConnectionString = "Data Source=telecom;Persist Security Info=True;User ID=qiao;Password=qiao;Unicode=True";   //写连接串
         public DataTable  ORselect(string str)
        {
            DataTable dt = new DataTable();
            OracleConnection conn = new OracleConnection();
           
             conn.ConnectionString = ConnectionString;
            OracleDataAdapter adapter = new OracleDataAdapter(str, conn);
            try
            {
                conn.Open();
                adapter.Fill(dt);
            }
            catch (OracleException ee)
            {

            }
            finally
            {
                conn.Close(); //关闭连接
            }
            return dt;
        }
         public int ORUpdate(string SQLstr)
         {
             OracleConnection conn = new OracleConnection();
             conn.ConnectionString = ConnectionString;
             OracleCommand command = new OracleCommand(SQLstr, conn);
             try
             {
                 conn.Open();
                 return command.ExecuteNonQuery();         //返回操作行数
             }
             catch (OracleException ee)
             {
                 return -1;                //操作失败  
             }
             finally
             {
                 conn.Close(); //关闭连接
             }

         }
         public DataSet ORSelect2(string str)
         {
             OracleConnection conn = new OracleConnection();
             conn.ConnectionString = ConnectionString;
             OracleDataAdapter adapter = new OracleDataAdapter(str, conn);
             DataSet DS = new DataSet();
             try
             {
                 conn.Open();
                 adapter.Fill(DS );
             }
             catch (OracleException ee)
             {
                
                // messagebox.show("连接失败!!!");
             }
             finally
             {
                 conn.Close(); //关闭连接
             }
             return DS;
         }
      
    }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值