如何使用Windows窗体将所有记录从oracle保存到sql [英] how to save all records from oracle to sql using windows form

查看:66
本文介绍了如何使用Windows窗体将所有记录从oracle保存到sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将记录从oracle数据库保存到sql数据库,。应该没有重复的身份证号码。

数据非常庞大缺乏



i只是尝试按代码插入但是需要我需要 PAT_NAME,PAT_ID,PATIENT_NO 这些来自oracle的三列,我只能查看该表,。,。







  protected   void  Button1_Click( object  sender,EventArgs e)
{
string oradb = 数据源=(DESCRIPTION = + (ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = 100.43.100.4)(PORT = 1521)))
+ (CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME) = HISP1))); + 用户ID = samir;密码= kodak;;
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
string sql = 选择PAT_NAME,PAT_ID ,来自PAT_REG的PATIENT_NO;
// 其中PATIENT_NO ='+ PatNo.Text +';
OracleCommand cmd = new OracleCommand(sql,conn);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader( );
while (dr.Read())
{
TextBox1.Text = dr [ PAT_ID]。ToString();
TextBox2.Text = dr [ PAT_NAME]。ToString();
TextBox3.Text = dr [ PATIENT_NO]。ToString();
string sq = Data Source = TRAINING-ROOM-8; Initial Catalog = ksmc; Integrated Security = True;
SqlConnection con = new SqlConnection(sq);
con.Open();
string s =( 插入测试(PatID,Patname,Pat_ID_NO)值(' + TextBox3.Text + ',' + TextBox2.Text + ',' + TextBox1.Text + '));


SqlCommand cmnd = new SqlCommand(s,con);
cmnd.ExecuteNonQuery();
TextBox1.Text = ;
TextBox2.Text = ;
TextBox3.Text = ;

}

conn.Close();
}

解决方案

如果只添加新记录,您应首先进行更新,以确保只选择新行。我可能会在oracle table bit / boolean中添加新列已处理,如果已将记录保存到sql,则会说明。

然后任何已保存到sql中的记录都应将新属性处理设置为是的。



为什么你要将datareader中的数据存储到文本框中?如果是临时存储,你应该使用一些简单的类。



如果你期望有很多数据(至少成千上万)你应该考虑使用批量插入的每个周期。



好​​的批量插入我会使用这里的代码

.NET应用程序中的插入内容:第1部分 [ ^ ]



因为你只有表的视图,你不能添加标签,说它已经下载到sql server。

如果你使用SQL Server 2008或稍后我总是将数据添加到tmp表(它将与要保存数据的tabe相同)然后在存储过程中使用TSQL MERGE,可以从代码中调用aso或由SQL Agent定期调用。

i want to save records from oracle database to sql database ,. there should be no duplication of ID number .
the data is very huge in " Lacks "

i just try to insert by code but it will take loads of time ,.
how should i use bulk insert i need PAT_NAME,PAT_ID, PATIENT_NO these three column from oracle , i have only view of that table,.,.



protected void Button1_Click(object sender, EventArgs e)
       {
        string oradb = "Data Source=(DESCRIPTION=" + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=100.43.100.4)(PORT=1521)))"
              + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=HISP1)));" + "User Id=samir;Password=kodak;";
        OracleConnection conn = new OracleConnection(oradb);
        conn.Open();
        string sql = "select PAT_NAME,PAT_ID, PATIENT_NO from PAT_REG ";
           //where PATIENT_NO = '" + PatNo.Text + "'";
        OracleCommand cmd = new OracleCommand(sql, conn);
        cmd.CommandType = CommandType.Text;
        OracleDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            TextBox1.Text = dr["PAT_ID"].ToString();
            TextBox2.Text = dr["PAT_NAME"].ToString();
            TextBox3.Text = dr["PATIENT_NO"].ToString();
            string sq = "Data Source=TRAINING-ROOM-8;Initial Catalog=ksmc;Integrated Security=True";
            SqlConnection con = new SqlConnection(sq);
            con.Open();
            string s = ("insert into test(PatID,Patname,Pat_ID_NO) values ('" + TextBox3.Text + "','" + TextBox2.Text + "','" + TextBox1.Text + "')");


            SqlCommand cmnd = new SqlCommand(s, con);
            cmnd.ExecuteNonQuery();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";

        }

        conn.Close();
       }

解决方案

if only new records should be added you should first make update to make sure you only select new rows. I would probably add new column "processed" to oracle table bit/boolean that would state if record was already saved to sql.
Then Any record that is already saved into sql should have new property processed set to true.

Also why are you storing data from datareader into textbox? if it''s for temporary storage you should rather use some simple class.

And if you are expecting that there will be lot''s of data(at least thousands) every cycle you should consider using bulk insert.

Well for Bulk insert i would use codes from here
Bulk Insert in .NET applications: Part 1[^]

And because you have only view of table you can''t add tag saying that it was already downloaded to sql server.
If you use SQL Server 2008 or later I woudl always add data to tmp table(it would be same table as tabe in which you want to save data) and Then use TSQL MERGE in stored procedure that could be called aso from your code or periodically by SQL Agent.


这篇关于如何使用Windows窗体将所有记录从oracle保存到sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆