SQL Server Connecting to an Azure SQL Database from offline PCs via intermediary (client server)

a1o7rhls  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(116)

I have a desktop application running on a few PCs on a local network, the application is connected to a Microsoft SQL Server database installed on a server.

This server is connected to the internet, while the PCs are not.

My question here, is that I want to migrate my database to Azure SQL database, but since my PCs do not have a direct access to the internet and VPN is not an option, is there a way where I can make my server to work as an intermediary (client-server) between my application running on local PCs (LAN) and my Azure database online?

Thanks

uxh89sit

uxh89sit1#

Actually there is a solution for this... First you link the Azure SQL DB server to the on-prem one, sp_addlinkedserver or follow this guide: How to create Linked server from SQL Server to Azure SQL Database

Once the server link is established, you can use a 4 part name system to query tables in the linked server:

Server.Database.Schema.Table

Example

SELECT sensors.*
     , instance.CurrentValue
     , history.EntryTime as Hist_EntryTime
     , history.Value as Hist_Value
FROM Sensor sensors -- local table to the executing database
INNER JOIN RealTimeDataServer.RTDatabase.dbo.SensorInstance instance 
        ON instance.SensorId = sensors.Id
INNER JOIN LakeStorage.HistoryData.dbo.Readings history
        ON history.SensorId = sensors.Id

If your intent is to migrate the data without making any changes to the source code, so not re-writing any queries, then you might be interested in CREATE EXTERNAL TABLE

External table replicates the schema for specific tables (but none of the data) so with this and an empty database you can effectively provide remote access to the external databases.

  • I say empty database because you must have unique table names in the same schema, but this works just as well with databases that have existing tables.

If I create External tables for SensorInstance and Readings for the above example then the query looks like this:

SELECT sensors.*
     , instance.CurrentValue
     , history.EntryTime as Hist_EntryTime
     , history.Value as Hist_Value
FROM Sensor sensors
INNER JOIN SensorInstance instance ON instance.SensorId = sensors.Id
INNER JOIN history ON history.SensorId = sensors.Id

You could also host an API on the server that executes the SQL calls for you. This however requires modification to your application and for you to design the API. If you do this,

If your PCs have access to the server, and the server has access to the internet, then VPN is a viable solution, you use the server as a gateway and should be able to VPN tunnel through that gateway...

相关问题