locked
Can a Azure data driven Web App uses another SQL Database on Azure under complete different user account RRS feed

  • Question

  • User-775831949 posted

    I used Account 1 (My company) to create a Web App for customer on Azure.

    But my customer does not want to store whole database under my account. He insists to store in Azure under his own account so he can always change passwords if our relationship ends in future.

    He is willing to give me access and password etc now so My Web App can get and update database in Azure under his account.

    So my need is, how to connect my Web App to his database in another Azure account ?

    Any link or tutorial ?

    Thanks

    Saturday, June 3, 2017 2:27 AM

Answers

  • User-271186128 posted

    Hi hkbeer,

    hkbeer

    how to connect my Web App to his database in another Azure account ?

    Any link or tutorial ?

    You could refer to the following articles to connect Azure SQL database:

    https://docs.microsoft.com/en-us/azure/sql-database/sql-database-connect-query-dotnet#get-connection-information

    After creating the Azure SQL database, you could navigate to the SQL Database server page to view the server admin name and get the database connection string.

    Then, refer to the following code to connect the database:

                try 
                { 
                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                    builder.DataSource = "your_server.database.windows.net"; 
                    builder.UserID = "your_user";            
                    builder.Password = "your_password";     
                    builder.InitialCatalog = "your_database";
    
                    using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                    {
                        Console.WriteLine("\nQuery data example:");
                        Console.WriteLine("=========================================\n");
    
                        connection.Open();       
                        StringBuilder sb = new StringBuilder();
                        sb.Append("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName ");
                        sb.Append("FROM [SalesLT].[ProductCategory] pc ");
                        sb.Append("JOIN [SalesLT].[Product] p ");
                        sb.Append("ON pc.productcategoryid = p.productcategoryid;");
                        String sql = sb.ToString();
    
                        using (SqlCommand command = new SqlCommand(sql, connection))
                        {
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
                                }
                            }
                        }                    
                    }
                }
                catch (SqlException e)
                {
                    Console.WriteLine(e.ToString());
                }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 8, 2017 7:38 AM