Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The steps in this article show how to connect to a data source in the Visual Studio IDE. The data source can be a local database, online data service, or a database opened from an .mdf file. You can work directly with your data in Visual Studio. You can execute queries, edit data, create and edit tables and other schema properties, edit stored procedures and functions, triggers, and so on. These functions are independent of the programming language or .NET version you are using.
If you're working with an Access database (.accdb file), see Connect to an Access database in .NET Framework applications.
Server Explorer and SQL Server Object Explorer
Add a connection in Server Explorer
To create a connection to the database, click the Connect to database icon in Server Explorer, or right-click in Server Explorer on the Data Connections node and select Add Connection. From here, you can open a connection to a LocalDB database opened from an .mdf file, connect to a database on another server, a SharePoint service, or an Azure service.
This brings up the Add Connection dialog box. Here, we have entered the name of the SQL Server LocalDB instance, (localdb)\MSSqlLocalDB, which is usually installed with Visual Studio.
If you don't have access to another database, and you don't see LocalDB installed, you can install LocalDB through the Visual Studio Installer, as part of the Data storage and processing workload, the ASP.NET and web development workload, or as an individual component. See Modify Visual Studio.
Authentication types
You can choose from a variety of authentication types that cover a wide range of scenarios. For details, see Authentication types.
Open an MDF database file
The .mdf file stands for Master Database File and is a SQL database packaged into a file. These files have an associated .ldf (Log Database File) that contains the logs for that database. You can create new databases that are stored in .mdf files by adding a Service-based Database item to your project. See Create a database and add tables.
If you already have .mdf file in your project, you can double-click or right-click and choose Open to open it in Server Explorer.
To open an .mdf file that's not in your project in Visual Studio's Server Explorer, follow these steps:
In the Add connection dialog box, under Data Source, choose Microsoft SQL Server Database File (SqlClient).
Use the Browse button to find and select your master database file (
.mdffile), or enter the path in the Database filename box.
Choose the authentication method.
Test the connection, and if it is successful, choose OK. A node for the database opens in Server Explorer.
Change the provider
If the data source is not what you want, click the Change button to choose a new data source or a new ADO.NET data provider. The new provider might ask for your credentials, depending on how you configured it.
Test the connection
After you have chosen the data source, click Test Connection. If it doesn't succeed, you will need to troubleshoot based on the vendor's documentation.
If the test succeeds, you are ready to create a data source, which is a Visual Studio term that really means a data model that is based on the underlying database or service.
Next steps
If you're using the .NET Framework (not .NET Core or .NET 5 or later), and Windows Forms or WPF, you can use the Data Sources window, for example, to set up data binding for controls in Windows Forms and WPF Applications, see Add new data sources. These tools are designed to enable you to rapidly create Windows applications that need to allow users to enter, display, and manipulate data.
If you're using .NET 5 or later, .NET Core or ASP.NET Core, you can connect your app to the database using Connected Services. Using Connected Services, you can easily use a local development database, hosted by SQL LocalDB, SQL Server running in a container, or an on-premises instance of SQL Server, and then transition to Azure SQL Database when you're ready to deploy to the cloud. For .NET 5 or later, .NET Core and ASP.NET Core, you should consider using Entity Framework Core as your database framework.