Reputation: 1488
I am trying to connect to SQL server 2005 via Workbench/J. I entered everything correctly for the jdbc string and I can connect to the desired server. But, I have to type fully qualified names for a table with database name. I don't want to do that. I set my instanceName to the database and it did not work for me.
Is there a way to connect to the DB instead of just the server ?
jdbc:sqlserver://serverName[\instanceName][:portNumber]
Upvotes: 1
Views: 6854
Reputation: 123399
Microsoft SQL Server supports multiple installs on the same computer. Each install ("virtual" SQL Server, if you will) is identified by its "Instance name". So, we could have two separate "SQL Servers" on the same computer, e.g., one instance named \PRODUCTION
for the production databases, and another instance named \TEST
for a test environment. Each instance operates independently.
A default installation of SQL Server Express Edition creates a SQL Server instance named \SQLEXPRESS
. The other Editions of SQL Server normally create a "default instance" (sometimes identified as \
).
Each instance of SQL Server can contain multiple databases. You can set the default database for your connection like this:
jdbc:sqlserver://myservername;database=myDb
or
jdbc:sqlserver://myservername;instanceName=instance1;database=myDb
Upvotes: 3
Reputation: 108939
If you want to connect to an instance you need to do two things:
With regard to the second item, the documentation says:
For optimal connection performance, you should set the
portNumber
when you connect to a named instance. This will avoid a round trip to the server to determine the port number. If both aportNumber
andinstanceName
are used, theportNumber
will take precedence and theinstanceName
will be ignored.
The instance name is not the same thing as your database name. You specify the database name using the connection property databaseName
, eg:
jdbc:sqlserver://localhost;databaseName=AdventureWorks
Upvotes: 6
Reputation: 12756
I think you should be able connect to a specific database like this:
jdbc:sqlserver://serverName[\instanceName][:portNumber];databaseName=MyDatabase
Upvotes: 3