ADO.NET: Can't connect to mdf database file

Posted by Nabo on Stack Overflow See other posts from Stack Overflow or by Nabo
Published on 2010-12-25T15:21:13Z Indexed on 2010/12/25 17:54 UTC
Read the original article Hit count: 233

I'm writing an application that uses a SQL Server 2005 database. In the connection string i'm specifying the mdf file like this:

connstr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=" + fileLocation + "; Integrated Security=True; User Instance=True";

When i execute the code:

public static void forceConnection()
{
    try
    {
        conn = new SqlConnection(connstr);
        conn.Open();
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if(conn != null)
            conn.Close();
    }
}

I receive an exception:

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

This code works on XP but not in Vista.. I tryed run Visual Studio in admin mode and moved the mdf file to "user data" folders but the error persists..

Any help? Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql-server