Import SQL server data to Oracle Data

Source: Internet
Author: User
Tags oracleconnection

Some code is provided below: C #

Need to reference

Using system. Data. oracleclient;
Using system. Data. sqlclient;

 

Private void button3_click (Object sender, eventargs e) // import data from sqlserver to Oracle
{
Button3.enabled = false; // prevent multiple clicks
Int temp = 0; // used to determine whether the insert is successful
String sqlstr; // receives the insert statement
Datatable dt = new datatable ();
String SQL = "select * From Dictionary"; // an existing data table in Oracle
Sqldataload sqldata = new sqldataload (); // custom class
Dt = sqldata. orselect (SQL );
String [] col = new string [3]; // used to receive data in no row
If (Dt. Rows. Count> 0)
{
For (INT I = 0; I <DT. Rows. Count; I ++) // loop each row
{
For (Int J = 0; j <DT. Columns. Count; j ++) // loop each column
{
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); // insert data into ORACLE data
}
}
Button3.enabled = true;
If (temp! =-1) MessageBox. Show ("conversion successful !! ");
Else
MessageBox. Show ("Conversion failed !! ");
}

Public String strto (string Str)
{
STR = "'" + STR + "'";
Return STR;
}

 

Custom class:

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"; // write a connection string
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 (); // close the connection
}
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 (); // return the number of Operation rows
}
Catch (oracleexception ee)
{
Return-1; // operation failed
}
Finally
{
Conn. Close (); // close the connection
}

}
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 ("Connection Failed !!! ");
}
Finally
{
Conn. Close (); // close the connection
}
Return Ds;
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.