如何使用Visual Studio连接到Azure SQL Server [英] How to connect to azure SQL server using visual studio

查看:79
本文介绍了如何使用Visual Studio连接到Azure SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Cosmos DB触发的天蓝色功能.我正在从JSON文件中获取数据,并将其发送到Azure上的SQL数据库.我想知道是否可以使用Visual Studio直接连接到Azure SQL吗?我已经通过门户一次连接到它,但是看不到我已在Visual Studio中的 View/Cloud Explorer 下连接到我的数据库.该数据库仅列在 View/SQL Server Object Explorer 下.我假设此连接是通过我的本地计算机,而不是直接连接到云.这是我的代码:

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

推荐答案

您可以尝试以下步骤来使用Visual Studio连接到Azure SQL Server :

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

打开Visual Studio并登录.

Open Visual Studio and login.

点击打开Cloud Explorer以查看所有订阅以选择您的订阅.

选择您的订阅 => 选择SQL数据库 => 选择您的数据库 => 右键单击并选择打开SQL Server对象"资源管理器 => 输入凭据并连接.

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

连接数据库后=> 右键单击 => 新查询 => 输入查询 =>在查询顶部,您将找到可用数据库(Shift + Alt + PgDn),然后单击执行(Ctrl + Shift + E).

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).

希望这会有所帮助.

这篇关于如何使用Visual Studio连接到Azure SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆