如何将数据从一个 table 移动到另一个 c#

how to Move data from one table to another c#

我有一个名为 test1test2 的两个 table 我想将数据从 test1 移动到 test2,就像条件匹配更新数据一样插入到 database.I 已成功完成我发布的 oracle 查询 down.I 还需要完成两个任务

**1>我必须将操作移至控制台 C# 应用程序

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. 您可以创建一个控制台应用程序并使用 ADO.Net 执行查询

  2. 使用 Oracle 中的 Trim 函数删除前导空格。

这是代码(没有测试,因为我没有 Oracle 数据库)

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;
                    }
                }
            }
        }
    }
}

参考资料

  1. MSDN
  2. Oracle TRIM Function