簡體   English   中英

如何將數據從一個表移動到另一個C#

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

我有一個名為test1test2的兩個表,我想以某種方式將數據從test1移至test2,例如條件是否匹配更新數據,否則插入數據庫中。我已經成功完成了我發布的oracle查詢。另外兩個任務

** 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 Function刪除前導空格。

這是代碼(未經測試,因為我沒有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;
                    }
                }
            }
        }
    }
}

參考

  1. MSDN
  2. Oracle TRIM功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM