简体   繁体   English

创建一个sqlCommand来从另一台mssql服务器更新一台mssql服务器上的表

[英]Creating a sqlCommand to Update a table on one mssql server from another mssql server

This is the code i have right now that is working 这是我现在正在运行的代码

'Declaring Connection String
        Dim sqlConnectionString As String
        sqlConnectionString = ConfigurationManager.ConnectionStrings("baminterchangerConnectionString").ConnectionString
        Dim sqlConnection As New SqlConnection(sqlConnectionString)

'Declaring SQL Queries
        Dim insertSQL As String = "insert into [dbo].[imports](OEMPartNumber) values (@OEMPartNumber)"
        Dim update As String = "UPDATE a SET a.AMIPartNumber = coalesce(c.Item,e.Item), a.AMIDescription = coalesce(c.Description, e.Description),a.OEMDescription = coalesce(b.OEMDescription,d.OEMDescription)FROM imports as a LEFT JOIN JD as b ON a.OEMPartNumber = b.OEMSubNumber LEFT JOIN amipartnumbers as c ON b.OEMCurrentPartNumber = c.OEMItem LEFT JOIN IH as d ON a.OEMPartNumber = d.OEMSubNumber LEFT JOIN amipartnumbers as e ON d.OEMCurrentPartNumber = e.OEMItem"

'Declaring SQL Commands
        Dim sqlInsertCommand As New SqlCommand(insertSQL)
        Dim sqlSelectCommand As New SqlCommand("SELECT * FROM [dbo].[imports]", sqlConnection)
        Dim sqlUpdateCommand As SqlCommand = New SqlCommand(update, sqlConnection)

This does exactly what i need it to do. 这正是我需要它做的。

HOWEVER, now i am trying to add another variable. 但是,现在我正在尝试添加另一个变量。 For items that don't get matched in imports.AMIPartNumber i need to query another table on my other sqlserver. 对于imports.AMIPartNumber中不匹配的项目,我需要查询其他sqlserver上的另一个表。

Can that be done? 能做到吗? Update the table 'imports' on one sql server with data from another table 'JD_ALL' that is on my other sql server? 使用来自另一台SQL服务器上另一表'JD_ALL'的数据来更新一台SQL服务器上的表'imports'吗?

I have this working to display data on site but this is for a csv file import. 我正在努力在现场显示数据,但这是用于csv文件导入的。 When displaying data on site im able to use multiple connections to fill 2 different data tables that don't have to be connected. 在现场显示数据时,无法使用多个连接来填充2个不必连接的不同数据表。

You have two choices. 您有两种选择。 Either: 要么:

1) Create a new connection string to the other server in your config file and then create a new connection to it in your code. 1)在配置文件中创建与其他服务器的新连接字符串,然后在代码中为其创建新连接。 You would then execute another command to retrieve your data from this other database before you use your other connection and commands. 然后,在使用其他连接和命令之前,您将执行另一个命令以从该另一个数据库检索数据。

For example: 例如:

'Declaring Connection String
        Dim sqlConnectionString As String
        sqlConnectionString = ConfigurationManager.ConnectionStrings("baminterchangerConnectionString").ConnectionString
        Dim sqlConnection As New SqlConnection(sqlConnectionString)

       Dim sqlConnectionStringOtherDatabase As String
        sqlConnectionStringOtherDatabase = ConfigurationManager.ConnectionStrings("otherConnectionString").ConnectionString
        Dim sqlConnectionOtherDatabase As New SqlConnection(sqlConnectionStringOtherDatabase)


Dim sqlSelectCommandToOtherDB As New SqlCommand("SELECT * FROM [dbo].[OtherTable]", sqlConnectionStringOtherDatabase)

.... etc

2) as @Dean mentioned you can add a linked server on your sql-server instance. 2)正如@Dean所述,您可以在sql-server实例上添加链接服务器。 This does assume you have access rights to play with the configuration of SQL Server though. 但这确实假定您具有访问权限,但可以使用SQL Server的配置。 You can then query the other database on a different as if it's on the same server. 然后,您可以查询另一个数据库上的另一个数据库,就像它位于同一服务器上一样。 This may save some code but requires more setup and config. 这样可以节省一些代码,但是需要更多的设置和配置。

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

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