简体   繁体   English

如何将数据从一个表移动到另一个C#

[英]how to Move data from one table to another c#

I have a two table named test1 and test2 i want to move data from test1 to test2 in a way like if a condition matches update data else insert in to database.I have successfully done the oracle query which i posted down.I have to achieve two more tasks 我有一个名为test1test2的两个表,我想以某种方式将数据从test1移至test2,例如条件是否匹配更新数据,否则插入数据库中。我已经成功完成了我发布的oracle查询。另外两个任务

**1>I have to move the operation in to console c# application ** 1>我必须将操作移至控制台C#应用程序

2>i have to remove the leading blank spaces for entries t2_fNAME and ACCOUNT_NUMBER** How i can achieve this task do i need to do ado.net c# code if so how to do it 2>我必须删除条目t2_fNAME和ACCOUNT_NUMBER的前导空格**如何实现此任务,如果需要,我需要做ado.net c#代码吗?

merge into test2 a
using test1 b
   on (a.t2_NAME = b.t1_NAME)
when matched then update
  set a.t2_fNAME = b.t1_fNAME,
      a.ACCOUNT_NUMBER = b.ACCOUNT_NO,

when not matched then
insert (t2_slno,t2_NAME,t2_fNAME,ACCOUNT_NUMBER)
values (t2_NODE_SEQ.NEXTVAL, b.t1_NAME,b.t1_fNAME,b.ACCOUNT_NO);
  1. You can create a Console application and Use ADO.Net to execute the query 您可以创建一个控制台应用程序,并使用ADO.Net执行查询。

  2. Use Trim Function in Oracle to remove leading blank spaces. 在Oracle中使用Trim Function删除前导空格。

Here is the code (Not tested as I don't have Oracle DB) 这是代码(未经测试,因为我没有Oracle DB)

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

namespace TestApp
{
    class Program
    {
        static void Main()
        {
            string connectionString = "Data Source=ThisOracleServer;Integrated Security=yes;";
            string queryString = @"merge into test2 a
                                    using test1 b
                                        on (a.t2_NAME = b.t1_NAME)
                                    when matched then update
                                        set a.t2_fNAME = TRIM(b.t1_fNAME),
                                            a.ACCOUNT_NUMBER = TRIM(b.ACCOUNT_NO),

                                    when not matched then
                                    insert (t2_slno,t2_NAME,t2_fNAME,ACCOUNT_NUMBER)
                                    values (t2_NODE_SEQ.NEXTVAL, b.t1_NAME,TRIM(b.t1_fNAME),TRIM(b.ACCOUNT_NO));";

            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                using (OracleCommand command = connection.CreateCommand())
                {
                    command.CommandText = queryString;

                    try
                    {
                        connection.Open();
                        command.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        //Log Exception here;
                        throw;
                    }
                }
            }
        }
    }
}

References 参考

  1. MSDN MSDN
  2. Oracle TRIM Function Oracle TRIM功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM