注册
dm调用bulkCopy批量插入数据

dm调用bulkCopy批量插入数据

lz 2022/11/11 3089 4 1
摘要 本文主要是提供一个简单的dm调用bulkCopy批量插入数据的示例demo,供大家参考

1、测试环境

【DM版本】:dm8 03134283914-20220927-170566-20009
【操作系统】:win10-64位
【开发工具】:vs2010

2、测试前准备工作

--数据库执行建表并插入数据:
drop table TEST_DEST;
drop table TEST_SOURCE;
create table TEST_SOURCE(c1 varchar,c2 varchar,c3 varchar,c4 varchar);
create table TEST_DEST(c1 varchar,c2 varchar,c3 varchar,c4 varchar);
create or replace procedure p1 (c1 int)
as
begin
for i in 1..c1 loop
insert into test_source values(i,i,i,i);
end loop;
commit;
end;
/
call p1(100);

3、vs2010创建一个c# 控制台应用程序

image.png

4、Program.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dm;
/* 数据库执行建表并插入数据
drop table TEST_DEST;
drop table TEST_SOURCE;
create table TEST_SOURCE(c1 varchar,c2 varchar,c3 varchar,c4 varchar);
create table TEST_DEST(c1 varchar,c2 varchar,c3 varchar,c4 varchar);
create or replace procedure p1 (c1 int)
as
begin
for i in 1..c1 loop
insert into test_source values(i,i,i,i);
end loop;
commit;
end;
/
call p1(100);
 */
namespace ConsoleApplication1
{
    class conn
    {
        static void Main(string[] args)
        {
            conn.PerformBulkCopy();
        }
        private static void PerformBulkCopy()
        {
            //建立数据库连接
            string connectionString = ("Server=localhost:5236; UserId=SYSDBA; PWD=SYSDBA;");
            DmConnection sourceConnection = new DmConnection(connectionString);
            sourceConnection.Open();
            //创建DataTable且列名须保持跟目的表列名一致
            DataTable table = new DataTable();
            table.Columns.Add("C1");
            table.Columns.Add("C2");
            table.Columns.Add("C3");
            table.Columns.Add("C4");
            //读取源端表数据并插入到DataTable
            DmCommand myCommand = new DmCommand("SELECT * FROM TEST_SOURCE", sourceConnection);
            DmDataReader reader = (DmDataReader)myCommand.ExecuteReader();
            while (reader.Read())
            {
                DataRow row = table.NewRow();
                row["C1"] = reader.GetString(0);
                row["C2"] = reader.GetString(1);
                row["C3"] = reader.GetString(2);
                row["C4"] = reader.GetString(3);
                table.Rows.Add(row);
            }
            //插入前查询表行数
            DmCommand DestCommand0 = new DmCommand("SELECT count(*) FROM TEST_DEST", sourceConnection);
            DmDataReader Destreader0 = (DmDataReader)DestCommand0.ExecuteReader();
            while (Destreader0.Read())
            {
                int i = Destreader0.GetInt32(0);
                Console.WriteLine("TEST_DEST行数:" + i);
            }
            Destreader0.Close();

            var bulkCopy = new DmBulkCopy(sourceConnection);
            //在插入的目标表
            bulkCopy.DestinationTableName = "TEST_DEST";
            //DataTable列名与数据库列名的映射
            for (int i = 0; i < table.Columns.Count; i++)
            {
                bulkCopy.ColumnMappings.Add(table.Columns[i].ColumnName, table.Columns[i].ColumnName);
            }
            bulkCopy.WriteToServer(table);//写入到数据库中
            bulkCopy.Close();
            reader.Close();
            //验证数据插入行数
            DmCommand DestCommand = new DmCommand("SELECT count(*) FROM TEST_DEST", sourceConnection);
            DmDataReader Destreader = (DmDataReader)DestCommand.ExecuteReader();
            while (Destreader.Read())
            {
                int i = Destreader.GetInt32(0);
                Console.WriteLine("TEST_DEST行数:" + i);
            }
            Destreader.Close();
            sourceConnection.Close();      
        }
    }
}

5、解决方案资源管理器引用添加DmProvider驱动
image.pngimage.png
6、解决方案平台选择X64,然后debug执行
image.png

评论
后发表回复

作者

文章

阅读量

获赞

扫一扫
联系客服