How to connect to SQL Server Express DB from Serve

2019-05-22 23:24发布

I'm trying to connect to SQL DB from Server Explorer to generate context classes for Linq to SQL. Doing it like described here: http://visualcsharptutorials.com/ado-net/querying-database-linq-to-sql/

I'm getting "This file is in use." error when I try to connect. Even if I stop SQL Server in Configuration Manager I get the same error.

I can connect to DB using SSMS at the same time.

What am I doing wrong? Is it possible to connect to a DB simultaneously from VS and SSMS?

Thanks

1条回答
女痞
2楼-- · 2019-05-23 00:06

When you say I can connect to DB using SSMS at the same time this sounds to me as if you've attached the database to SQL Server Express, and you're talking to it through its logical database name.

When you have done this (which is the best approach, in my opinion), then SQL Server (Express) is using that file and controls all access to it; you cannot just reference the .mdf file from your VS solution - you need to use the SQL Server approach and connect to the SQL Server (Express) instance and pick the database:

enter image description here

Select

  • Microsoft SQL Server (SqlClient) from the Data Source dropdown (NOT something like "database file")
  • the server name and instance you want to use (typically: .\SQLExpress - the SQLExpress instance on your local machine)
  • the database from the Select or enter a database name dropdown

  • DO NOT use anything that has to do with "attach database file" or anything like that

查看更多
登录 后发表回答