SQL server name?

SmokyRick

Member
Joined
Mar 14, 2018
Messages
11
Programming Experience
Beginner
I am totally new to this database stuff. I am working in Visual Studio 2017 trying to link a database with a program. The name of the SQL Server seems to be a problem. I get the following exception when I try to run the program.
ExceptionUnhandled.jpg
From this and other errors I am thinking I have the name of the server wrong. However, I have no idea what to name it. I used SQL Server Configuration Manager and found I have 2 servers on this computer. They are "SQL Server (SQLEXPRESS)" and "SQL Server (MSSQLSERVER)". I can't seem to find a good way to let VB know the proper name. Can anyone help?
Thanks in advance for any and all bits of knowledge.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
I don't think that I've ever seen a Data Source of "(LocalDB)\SQLEXPRESS". If you have actually installed SQL Server Express then you probably need ".\SQLEXPRESS" or "(local)\SQLEXPRESS". In those cases, "." and "(local)" both refer to the local machine, whereas "(LocalDB)" refers to s specific type of SQL Server instance. If you check www.connectionstrings.com, you'll see that they use "(localdb)\v11.0" in there examples, which would have been for SQL Server 2012. I have SQL Server 2016 installed and for me it's "(LocalDB)\MSSQLLocalDB". What I did to see that was add a new Service-based Database to my project, which is a SQL Server MDF file, and then opened that Server Explorer (or Database Explorer), right-clicked the database and selected Properties, then looked at the Connection String property.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
I should also point out that MSSQLSERVER is the default instance of SQL Server, so you can connect to that using just the machine name, i.e. ".", "(local)" or the actual name of the machine. I'm not sure whether non-Express instances allow attaching files on demand as I've never tried it. If you do try and it works, please post back and let me know.
 

SmokyRick

Member
Joined
Mar 14, 2018
Messages
11
Programming Experience
Beginner
Thanks for the reply. I think that may have been it. I tried "(localdb)\MSSQLLocalDB" and get a different error now.
NewError2018-04-14-11-06.jpg
It now seems to find the server, but still can't open the file for whatever reason. I truly wish there was a good tutorial for this stuff, but I am not finding one in my Internet searches.
Thanks again for your help, I am another step closer now.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
As the error message says, it cannot attach that file at run time because there is already a database with the same name attached. Do you still have the database open in VS? If so, close it and try again. If it still doesn't work, close VS and reopen your project without reopening the database and try again. If it still doesn't work, restart the SQL Server service and try again. If it still doesn't work, restart Windows and try again.

Sometimes this can happen because VS doesn't close the connection properly after running your project but it can also happen because you're trying to open the database twice. I'm not sure but I think the latter can be avoided if you do things as were intended, i.e. add the MDF file to your project so that a copy is made in your output folder when you build and your application connects to that instead of the source file.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
Wouldn't you get that error if you're trying to attach a database file when there's already a different database with that name on the server?
This could be Sql telling him there's a Sql database name conflict.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Wouldn't you get that error if you're trying to attach a database file when there's already a different database with that name on the server?
This could be Sql telling him there's a Sql database name conflict.

If it's a LocalDB instance then there's no permanently-attached databases so it couldn't be a clash that way. You would get that error message if you had two applications trying to attach the same file or two files with the same name. That's possible in this case but unlikely so, given that it's a VB tutorial, I was assuming that that would not be the case.
 

SmokyRick

Member
Joined
Mar 14, 2018
Messages
11
Programming Experience
Beginner
Well, Thank you all for the help. I was finally able to work this out as you can see on the picture attached. However, I now have another problem which seems to defy explanation.
I am totally unsure what the incorrect syntax is, let alone how to fix it. Has anyone else run into this? Or can you see what I did wrong?
Thanks in advance for any help you can offer.
 

Attachments

  • LatestError.jpg
    LatestError.jpg
    352.4 KB · Views: 39

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+

SmokyRick

Member
Joined
Mar 14, 2018
Messages
11
Programming Experience
Beginner
Thank you, I see now that I put 'form' instead of 'from'. Some days are just like that I guess.
Thanks again for the speedy reply!
 
Top Bottom