سؤال

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]
هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

If you want to connect to an instance you need to do two things:

  1. Make sure the SQL Server Browser service is running on your SQL Server host (disabled by default IIRC)
  2. Do not include the portnumber in the connection string if you want to connect by instance name. The JDBC driver will ignore the instance name when the connection string includes a portnumber (each instance has its own port number)

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 a portNumber and instanceName are used, the portNumber will take precedence and the instanceName 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

I think you should be able connect to a specific database like this:

jdbc:sqlserver://serverName[\instanceName][:portNumber];databaseName=MyDatabase
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top