How to connect to azure SQL server using visual studio

how to connect to azure sql database from visual studio c#
connect to azure database from visual studio 2017
connect to azure sql database c#
azure data studio
asp.net connect to azure sql database
dotnet connect to sql server
sql server management studio
connect to azure sql database from on premise

I have an azure function triggered by Cosmos DB. I am getting the data from the JSON file and send it over to my SQL database on Azure. I wanted to know if I can connect directly to the Azure SQL using my Visual Studio? I have connected to it once through the portal, but I can't see that I am connected to my database under View/Cloud Explorer in Visual Studio. The database is only listed under View/SQL Server Object Explorer. I assume this connection is through my local machine, and not directly to the cloud. This is my code:

        public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task Run([CosmosDBTrigger(
            databaseName: "ToDoList",
            collectionName: "Items",
            ConnectionStringSetting = "CosmosDB",
            LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                var cnnString = "Server=tcp:server.database.windows.net,1433;Initial Catalog=myDatabase;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
                using (var sqlConnection = new SqlConnection(cnnString))
                {
                    sqlConnection.Open();
                    var cmd = new SqlCommand
                    {
                        CommandText = @"insert into [dbo].[Player] ([User],[Timestamp] values(@User,@Timestamp)",
                        CommandType = CommandType.Text,
                        Connection = sqlConnection,
                    };

                    var record = new Record();
                    //set parameters
                    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@User", record.Email));
                    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Timestamp", record.Timestamp));

                    //Execute command
                    await cmd.ExecuteNonQueryAsync();
                }         
            }
        }
    }

You may try the following steps to connect to Azure SQL Server using Visual Studio:

Open Visual Studio and login.

Click on Open Cloud Explorer to view all subscription to select your subscription.

Select your subscription => Select SQL Databases => Select your Database =>Right Click and select Open SQL Server Object Explorer => Enter credentials and connect.

After connecting your database => Right click => New Query => Enter the query => On the top of query you will find available databases (Shift+Alt+PgDn) and click on execute (Ctrl+Shift+E).

Hope this helps.

Use Visual Studio with .NET and C# to query, 1. Connect to your SQL pool. Open Visual Studio 2019. Open SQL Server Object Explorer by selecting View > SQL Server Object Explorer  Connecting to an Azure SQL Database with Visual Studio Code (Code) To connect to an Azure SQL database with Visual Studio Code, you'll need to install the mssql extension for Visual Studio Code. Once installed, press Ctrl + Shift + P to open the dialog box and search for MS SQL: Connect.

Connect to Azure Synapse Analytics with Visual Studio and SSDT, Connect to your database. Use Visual Studio Code to establish a connection to your server. Important. Before continuing, make sure that you  For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance. Create code to query the database in Azure SQL Database In Visual Studio, select File > New > Project. In the New Project dialog, select Visual C#, and then select Console App (.NET Framework).

You can see the Azure SQL Database in Visual Studio Server explorer

Use Visual Studio Code to connect and query, You may try the following steps to connect to Azure SQL Server using Visual Studio: Open Visual Studio and login. Click on Open Cloud  For that, click "Configure your firewall" link, as shown below. Then, click "Add Client IP" option and click "Save" to add your IP to the Azure SQL Database. Next, click "Open in Visual Studio" button. Next, it asks to create the Weblinks to connect our SQL to Visual Studio and then click "Open Link".

How to connect to azure SQL server using visual studio, Now, connect to SQL Server using the credentials specified on step 7. Figure 17. Database credentials. You are now connected to Azure. Expand  Open Visual Studio 2019. Open SQL Server Object Explorer by selecting View > SQL Server Object Explorer. Click the Add SQL Server icon. Fill in the fields in the Connect to Server window.

Working with Visual Studio and SQL Azure databases, A Windows 10, Mac OS, or Linux laptop or desktop with PowerShell, Visual Studio Code and SQL Server Management Studio installed. In this chapter, we will work with Visual Studio to connect to SQL Databases in Azure. The first part of this chapter will be to create a Database in the Azure Portal and the second part will be to connect in Visual Studio and create some Database Objects. Requirements An Azure subscription. A local machine with Windows and Visual Studio

How to Connect to an Azure SQL Database (Step by Step), Then, Visual Studio opens with "Connect to Server" window already set to connect to the server and database that we selected in the portal. Type  Server admin account password The password from the server admin account used to create the server. Select Options in the Connect to Server dialog box. In the Connect to database drop-down menu, select mySampleDatabase.

Comments
  • Thanks a lot, but unfortunately I cant get passed the step after selecting my subscription, because I cant expand that, there is nothing under my subscription unfortunately. I dont understand why. I have my Azure SQL listed under SQL Server Object Explorer but cant see it under Cloud Explorer
  • @SSRH Could you please sign-out and sign-in. After sign-in => Go to View => Select Cloud Explorer => Wait for few seconds and hit on Refresh all. Can you find you subscription now?
  • Thanks a lot, it works now! I should have thought about it. I followed the rest of the steps and I managed it. So now that means I am connected to the server, right? If I can query and see the data from the Azure SQL database.
  • Yes, once you have connected to Azure SQL Server using visual studio. You can query directly from visual studio.
  • Unfortunately when I run my code, I get this error as a new document enters in to CosmosDB: Core .Net SqlClient Data Provider: Login failed for user {Admin} Could you have any guess why?
  • I may have been vague in what I meant, I dont want to manage my SQL at this stage, if I cant write my data to the SQL database, managing it would be pointless since no data is contained in the database. I just need to connect to SQL in Visual Studio to be able to write to the database.
  • Thanks but it tells me one or more subscriptions are not supported by Server Explorer, and tells me to open Cloud Explorer. When I open Cloud explorer I see the same thing as I saw before.