locked
Cannot connect to SQL database RRS feed

  • Question

  • User-1887096301 posted

    Hi All

    I am having some trouble connecting to a SQL database.

    The Database is found on the local machine in the  %user%\Documents\Visual Studio 2008\Projects\Proj1\Proj1\App_Data folder.

    I have taken the code from MSDN (http://msdn.microsoft.com/en-us/library/s4yys16a(VS.71).aspx) .

    I am unsure of what I should set teh datasource as but it does not seem to work.

    The error message I am receiving is Failed to connect to data source. A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    Can anyone help please?

    Below is my code

    1            public void ConnectToSql()
    2            {
    3                System.Data.SqlClient.SqlConnection conn =
    4                    new System.Data.SqlClient.SqlConnection();
    5                // TODO: Modify the connection string and include any
    6                // additional required properties for your database.
    7                conn.ConnectionString =
    8                 string.Format (@"integrated security=SSPI;data source=(local);" +
    9                 "persist security info=False;initial catalog={0}", GetDatapath) ;
    10               try
    11               {
    12                   conn.Open();
    13                   // Insert code to process data.
    14               }
    15               catch (Exception ex)
    16               {
    17                   lblError.Text = "Failed to connect to data source. " + ex.Message;
    18               }
    19               finally
    20               {
    21                   conn.Close();
    22               }
    23           }
    24           
    25           private string GetDatapath
    26           {
    27               get
    28               {
    29                   return Server.MapPath(@"app_data\dbPlug.mdf");
    30               }
    31   
    32           }
    
      
    Monday, January 26, 2009 6:28 PM

Answers

  • User2130758966 posted

     If you have installed the free version of sql server express then your instance name (server name) will likely be:

     data source=.\SQLEXPRESS;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2009 7:23 PM