How to connect our C# application in Azure sql managed instance

Raki 481 Reputation points
2020-07-07T19:59:27.187+00:00

Hi,

My web application residing Azure cloud server and database residing into Azure SQL managed instance. They are in the same V Net but under different subnet. i am using sql server authentication to login into instance. in application i defined connection string into web.config file. what i need to do in my web application to get connect with sql managed instance? i am using below code for connection:

  private static string GetConnectString()
    {
        return ConfigurationManager.AppSettings.Get("ConnectString");
    }

    public static SqlConnection GetConnection(string connectString)
    {
        SqlConnection connection = new SqlConnection((!string.IsNullOrEmpty(connectString) ? connectString : GetConnectString()));

        connection.Open();

        return connection;
    }


    public static SqlConnection GetConnection()
    {
        SqlConnection connection = new SqlConnection(GetConnectString());

        connection.Open();

        return connection;
    }

Web.Config:

<add key="Connecstring" value="Data Source=prodsqlmi.87f427d03.database.windows.net;Initial Catalog=Test;User ID=Test;Password=*******;"/>

Thanks in advance.

Azure SQL Database
0 comments No comments
{count} votes

Accepted answer
  1. KalyanChanumolu-MSFT 8,316 Reputation points
    2020-07-08T09:19:22.54+00:00

    Please post the error message to troubleshoot further.
    While editing your post I noticed that the Key used in Web.config and your code do not match

    Please check if that is the reason

    Key in Web.Config: Connecstring
    Key in Code: ConnectString

    --
    If an Answer is helpful, please “Accept Answer” or Up-Vote for the same which might be beneficial to other community members reading this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. 2020-07-07T20:39:58.25+00:00

    Raki,

    Two things to better assist you:

    • Is there any logs or error messages happening that you could share?
    • Second, it's not recommended to share User / Password on database connections in a public manner, even for testing purposes.

    A possibility:

    • Check inside your SQL Server firewall if the option allow access to azure services is granted.
    0 comments No comments