How to connect to azure SQL server using visual studio

∥☆過路亽.° 提交于 2021-01-29 08:46:02

问题


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();
                }         
            }
        }
    }

回答1:


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.




回答2:


Note: You need to use the following tools to connect Azure Sql Server.

Which tool should I choose?

  • Do you want to manage a SQL Server instance or database, in a light-weight editor on Windows, Linux or Mac? Choose Azure Data Studio

  • Do you want to manage a SQL Server instance or database on Windows with full GUI support? Choose SQL Server Management Studio (SSMS)

  • Do you want to create or maintain database code, including compile time validation, refactoring and designer support on Windows? Choose SQL Server Data Tools (SSDT)

  • Do you want to query SQL Server with a command-line tool that features IntelliSense, syntax high-lighting, and more? Choose mssql-cli

  • Do you want to write T-SQL scripts in a light-weight editor on Windows, Linux or Mac? Choose Visual Studio Code and the mssql extension

For more details, refer “SQL Tools and Utilities for SQL Server, Azure SQL Database, and Azure SQL Data Warehouse”.




回答3:


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



来源:https://stackoverflow.com/questions/53727420/how-to-connect-to-azure-sql-server-using-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!