共用方式為


Add new connections

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

The steps in this article show how to connect to a database in the Visual Studio IDE. You can use these steps to work directly with your data, such as 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.

Add a connection in Server Explorer

To create a connection to the database, click the Add Connection icon in Server Explorer, or right-click in Server Explorer on the Data Connections node and select Add Connection. From here, you can also 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.

Change the provider

If the data source is not what you want, click the Change button to choose a new data source and/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 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.

See also