Compartir a través de


How to: Connect to a File-based Database

You can attach an existing database file to a local instance of SQL Server, or you can create a database file to attach to the local instance.

Note

Visual Studio 2012 has changes that apply to database files (.mdf). For more information, see Local Data Overview.

Note

In Visual Studio 2013, you can connect to a SQL Server database from either SQL Server Object Explorer or the Data Connections node in Server Explorer. SQL Server Object Explorer provides a view that is like SQL Server Management Studio. To open this explorer from the menu bar, choose View, SQL Server Object Explorer. For more information, see Connected Database Development.

Important

If you connect a SQL Server Express file to a local instance of SQL Server 2005, the file will be changed to the format for SQL Server 2005. If you connect a SQL Server Express file to a local instance of SQL Server 2008, then the file will be changed to the format for SQL Server 2008.

To temporarily attach a database file (.mdf) from the Data Connections node

  1. In Server Explorer, open the shortcut menu for Data Connections and choose Add Connection.

    The Add Connection dialog box appears.

  2. Choose the Change button.

    The Change Data Source dialog box appears.

  3. Select Microsoft SQL Server Database File and choose the OK button.

    The Add Connection dialog box reappears, with Microsoft SQL Server Database File (SqlClient) shown in the Data source text box.

  4. Choose the Browse button and browse to an existing .mdf file.

    If you type a file name that does not exist, a blank .mdf file will be created.

  5. Select either Use Windows Authentication or Use SQL Server Authentication.

    For more information on SQL Server database access authentication, see Create New SQL Server Database Dialog Box.

  6. Choose the OK button.

    The database appears in Server Explorer. The database is attached to the local SQL Server instance, and will remain connected until you close the connection.

You must have adequate Microsoft SQL Server permissions to create or attach a new database on the target server.

For information about how to create, edit, and browse objects in SQL Server databases, see Connected Database Development.

To permanently attach a database file (.mdf) from the Data Connections node

  1. Open the shortcut menu for Data Connections and choose Add New Connection.

    The Add Connection dialog box appears.

  2. Choose the Change button.

    The Change Data Source dialog box appears.

  3. Select Microsoft SQL Server and choose the OK button.

    The Add Connection dialog box reappears, with Microsoft SQL Server (SqlClient) displayed in the Data source text box.

  4. In the Server Name box, type or browse to the path to the local instance of SQL Server. You can type the following:

    • "." for the default instance on your computer.

    • "(LocalDB)\v11.0" for the default instance of SQL Server Express LocalDB.

    • ".\SQLEXPRESS" for the default instance of SQL Server Express.

    For information about SQL Server Express LocalDB and SQL Server Express, see Local Data Overview.

  5. Select either Use Windows Authentication or Use SQL Server Authentication.

  6. Choose Attach a database file, Browse, and open an existing .mdf file.

  7. Choose the OK button.

    The new database appears in Server Explorer. It will remain connected to SQL Server until you explicitly detach it.

See Also

Concepts

Database Connections and Database References

Other Resources

Database Connections