Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I debug a windows based project in visual studio2015,but face error in objet sqlconnection be open

What I have tried:

I change connection string,attachdbfilename,user instance ... but cannot conect to database and open
Posted
Updated 15-Jun-17 7:35am

Start by not attaching databases to SQL - it's a "special" for development only, and doesn't work at all in non-Express versions of Sql Server. You should create the DB within SQL and let it manage it, not try to attach a file as needed.

Second, start with your connection string - try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Until you have that working, your C# or VB based code is never going to work.
 
Share this answer
 
Comments
Reza.kvn 15-Jun-17 12:07pm    
When i click test connection button,it has error As follows:
The user instance login flag is not supported on this version of sql server. The connection will be closed
OriginalGriff 15-Jun-17 12:13pm    
And that error is saying "I am not an Express edition of SQL, and you can't attach a database to non-express editions".

Which is what I told you to start with!
Reza.kvn 16-Jun-17 8:15am    
Thank you very much، I'd already put data source=.\sqlexpress. But changed it to: (localdb)\mssqllocaldb.and changed attribute user instance to:user instance=false .
My problem was resolved
About AttachDBFileName:
Quote:
This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
More information here: Bad habits : Using AttachDBFileName - SQL Sentry[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900