如何使用VB.NET将数据从一个SQL Server数据库导入到另一个 [英] How to import data from one sql server database to another using VB.NET

查看:160
本文介绍了如何使用VB.NET将数据从一个SQL Server数据库导入到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用VB.NET和SQL Server 2005开发应用程序.已经开发了一种软件,并且已经在运行模式下运行,现在我必须开发一种功能,用户可以在该软件上添加一条记录,并且将添加相同的记录.也进入我的数据库表.

为此,当我在应用程序中打开特定的屏幕/页面时,我必须编写一些代码,它将检查所述软件/应用程序上的数据库表,并查找是否有比其新的条目/记录也将出现在我的应用程序屏幕中,在换句话说,它将从所述软件的数据库中检索数据并保存到我的应用程序数据库中.

简而言之,我必须使用VB.NET代码将数据库表的记录从一个数据库导入另一个数据库.
请指教.

任何人都可以帮我吗?

I have been developing application using VB.NET and SQL Server 2005. There is one software is already been developed and in running mode, now I have to develop facility that user can add one record on that software and same record will add into my database table also.

For that I have to write some code for when I will open particular screen / page in my application it will check the database table on said software/application and found if any new entry/record than it will appear in my application screen also, in other words it will retrieve data from said software''s Database and save into my application database.

In short I have to import record for database table from one database to another database using VB.NET code.

Please advice.

Anyone can please help me on this??

推荐答案



您应该为此使用SQL触发器功能,通过使用触发器,您应该可以轻松地获得它.为此,您必须在您的
上编写一个插入触发器 第一个DB(目标DB-现有软件DB).在(目标数据库-现有软件数据库)表中的特定表上记录插入时,它将自动
使用触发器将您插入到源数据库特定表中.

在下面的示例SQL代码中查看

CREATE TRIGGER another_db_insert ON test_table FOR INSERT
AS
开始
开始交易test_insert

-目标数据库Test_Destination_DB
插入到Test_Destination_DB.dbo.destination_table(Id,Name)
SELECT ID,名称FROM插入的AS Source_table

IF(@@ ERROR!= 0)
开始
回滚事务test_insert
返回0
END
ELSE
开始
提交事务test_insert
返回1
END
END

希望这对您有用!祝你好运

注意:如果两个数据库都在同一SQL服务器上,则此方法将起作用.
Hi,

You should use SQL Trigger functionality for this, by using Trigger you should get this easily. For this you have to write one Insert Trigger on your
First DB (Destination DB - Existing software DB). When record insert on particular table in (Destination DB - Existing software DB)''s table it will automatically
insert into you Source DB particular table using Trigger.

Review below sample SQL code for

CREATE TRIGGER another_db_insert ON test_table FOR INSERT
AS
BEGIN
BEGIN TRANSACTION test_insert

-- Destination Database Test_Destination_DB
INSERT INTO Test_Destination_DB.dbo.destination_table(Id, Name)
SELECT Id, Name FROM inserted AS Source_table

IF (@@ERROR != 0)
BEGIN
ROLLBACK TRANSACTION test_insert
RETURN 0
END
ELSE
BEGIN
COMMIT TRANSACTION test_insert
RETURN 1
END
END

Hope this will works for you! Good luck

NOTE: This will works if both databases are on same SQL server.


这篇关于如何使用VB.NET将数据从一个SQL Server数据库导入到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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