1

I am trying to connect to database at window form level in Studio 2010/VB.Net. I am using two different databases in Sql 2008r2. One database I am using to populate records at form load event (run time) and when users pick their selection or any modification from that form, data should be updated to other database by instert, update, delete and save commands for user's future reference.

Is it possible to connect with two different databases using one Connection String?

Do I need to add two database name in below mentioned code somewhere?

Imports System.Data.SqlClient

Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
Dim cmd As New SqlCommand()    
cmd.Connection = con    
con.Open()    
con.Close()

OR

Should I use two database below somewhere?

Imports System.Data.SqlClient

Public Class NEW_PERSONAL_INFORMATION

  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim con As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")

    Dim cmd As New SqlCommand()    
    cmd.Connection = con    
    con.Open()    
    con.Close()
  End Sub

End Class

Apologies If I am seeking help in simple thing or not able to explain it enough.

I would appreciate any help.

3
  • This isn't quite clear... You've got 2 databases housed in two different database server instances? Two tables in a single db? Commented Dec 3, 2012 at 16:17
  • Are you saying you have two different servers? Or two different databases in the same server? Commented Dec 3, 2012 at 16:19
  • I have two different database in the same server. Sorry If I was not cleared. Commented Dec 3, 2012 at 16:37

2 Answers 2

7

I can't tell what you're asking, so I'll give two answers.

If you're trying to connect to two different servers then you'll need two different connection strings. In fact, you'll need two different SqlConnections, and you'll need to keep track of which connection is which.

Dim con1 As New SqlClient.SqlConnection("data source=ROOM310-40\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")
Dim con2 As New SqlClient.SqlConnection("data source=ROOM310-40-2\SQLEXPRESS;initial catalog=DTIS;Integrated Security=True")

If you're talking about multiple catalogs on the same server, then you don't need multiple connections. You can just specify the catalog name directly in your query:

SELECT MyColumn FROM MyDatabase1.dbo.MyTable;
SELECT MyColumn FROM MyDatabase2.dbo.MyTable;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. Apologies If I have not explained it in correct manner. It is two database on the same server.
0

Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASEm,Another Database Name,And SO ;Integrated Security=True")

1 Comment

Please restructure your answer. Add code blocks (select your code and hit CTRL+K), add an explanation to your code. This really doesn't help anyone looking for help in this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.