简体   繁体   中英

Accessing Azure SQL Database with Windows Service

I am new to Azure. Following the tutorials of Microsoft I have downloaded the "TodoAzure" Client project for Xamarin.Forms as well as the corresponding backend project. I also set up the Azure web app and published the backend code. This works all well as expected.

What I would like to know is how I can access the Azure cloud database (presumably using the web app) from a completely different source than the client app - I would like to access it with a windows service. I already have this service that runs on my desktop computer, and this service has access to a local SQL database. Some parts of this database should be uploaded to the Azure cloud database, so that this data becomes visible in the mobile (Xamarin) app.

Is there a best-practice on how to do this? I can't find any suitable examples. I need only a link or a description on which packages to use.

Accessing an Azure SQL database is almost the same as accessing an onprem SQL Server. If you would like a quickstart, you can find one here , but your existing code should work if it is connecting to your local instance.

There are a couple steps to change over the connection.

  1. The biggest difference is that you must add your local IP address to the firewall in your Azure SQL Server. You can get your IP address by typing "What's my ip" into your browser or clicking "Add Client IP" in the portal. Make sure to save your changes. 在此处输入图片说明 在此处输入图片说明
  2. Add a user to your database for your desktop app. You can use SSMS, Visual Studio, or there is a query editor available in the portal. The command to add a user to your Azure SQL database is CREATE USER user_name WITH PASSWORD = 'strong_password'; . You can add users to the master database as you would in SQL Server, but it is better in Azure SQL to add them to specific DBs. You will then need to give it permissions, usually db_datareader and db_datawriter if you want it to be able to edit the data. The command for this is ALTER ROLE db_datawriter ADD MEMBER user_name; .

  3. Get your connection string and replace the one in your app. You can either change the server and database in your current string, or you can grab a fresh copy from the database's blade in the portal. Make sure to update the username and password with the ones you created. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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