using DDTek.Oracle;
using System.Data.Common;
using System.Data.SqlClient;
private static bool DownLoadEmail()
{
OracleConnection conMiddle = null;
SqlConnection connSQL = null;
SqlCommand commSQL = null;
try
{
conMiddle = new OracleConnection("Host=192.168.**.**;Port=1521;User ID=****;Password=****;SID=orcl;Pooling=False");
OracleCommand cmdMiddle = conMiddle.CreateCommand();
cmdMiddle.CommandType = CommandType.Text;
if (conMiddle.State == ConnectionState.Closed) //打开oracle
{
conMiddle.Open();
}
//查询资料是否存在
cmdMiddle.CommandText = @"select ID,COMPANY,EMAIL,NAME,OPUSER,OPDATE from email";
OracleDataAdapter sda = new OracleDataAdapter(cmdMiddle);
DataTable dtEmail = new DataTable();
sda.Fill(dtEmail);
if (dtEmail != null && dtEmail.Rows.Count > 0)
{
connSQL = new SqlConnection("Data Source=192.168.**.***;Database=****;User=**;Password=****;Application Name=ws");
if (connSQL.State == ConnectionState.Closed)//打开sqlserver
connSQL.Open();
commSQL = new SqlCommand();
commSQL.Connection = connSQL;
commSQL.CommandText = "delete from email";//刪除表,如果不刪除,会报错“主键重复”
commSQL.ExecuteNonQuery();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connSQL))
{
//插入数据的表名
bulkCopy.DestinationTableName = "email";
//插入的资料
bulkCopy.WriteToServer(dtEmail);
}
}
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
if (conMiddle.State == ConnectionState.Open)
{
conMiddle.Close();
conMiddle.Dispose();
}
if (connSQL.State == ConnectionState.Open)
{
connSQL.Close();
connSQL.Dispose();
}
}
}
SqlBulkCopy 使用案例
最新推荐文章于 2023-08-02 14:57:55 发布