Azure / azure-libraries-for-net

Azure libraries for .Net
MIT License
379 stars 192 forks source link

How to get an existing SQL Server instance? #1282

Closed Aoshua closed 2 years ago

Aoshua commented 2 years ago

Question: How can I get an exiting SQL Service instance? Ultimately what I am trying to do is add a database to an exisiting server. Here is what I have tried, but it always returns null for the server:

var server = await azure.SqlServers.GetByIdAsync("/subscriptions/MySubscriptionId/resourceGroups/MyResourceGroup/providers/Microsoft.Web/serverFarms/MySeverName");
server.Databases.Define(newDatabaseName).Create();

I'm using azure before these lines, so I know that it's not an authentication issue.

I'm assuming this is just error on my part, since I'm new to Azure SDK for NET.

I'm using the most recent stable VS Code and Visual Studio 2019. I'm using the Microsoft.Azure.Management.Sql.Fluent package.

weidongxu-microsoft commented 2 years ago

@Aoshua The ID you are using is webapp or appservice (/subscriptions/MySubscriptionId/resourceGroups/MyResourceGroup/providers/Microsoft.Web/serverFarms/MySeverName), not sql server.

This SDK is in maintenance mode. New SDK will be available at https://aka.ms/azsdk/dotnet/mgmt

Aoshua commented 2 years ago

@weidongxu-microsoft You're totally right. I switched to azure.SqlServer.GetByReourceGroupAsync(myResourceGroup, serverName) and that worked perfectly. Thanks for pointing that out!