?
Solved

Connection string for 2 databases on different servers in vb.net

Posted on 2014-02-20
3
Medium Priority
?
1,887 Views
Last Modified: 2014-02-20
Hi all.

I have the following code that populates a datagridview control in Visual Studio 2010. The problem is that my Select statement needs to join 2 tables from different databases and different servers. How can I make this work?

As you can see currently, my connection string only references db1 on server1.

server1, db1
server2, db2

Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=server1;Initial Catalog=db1;Integrated Security=True"

        con.Open()

        cmd.Connection = con
        'cmd.CommandText = "SELECT  MasterItemNumber, Description, SLXID, QuoteDate, UnitCost, QuotedCost, Notes FROM ItemQuote WHERE MasterItemNumber = @MasterItemNo"
        cmd.CommandText = "SELECT  MasterItemNumber, Description, SLXID, QuoteDate, UnitCost, QuotedCost, Notes FROM ItemQuote INNER JOIN db2.sysdba.ACCOUNT ON ItemQuote.SLXID = db2.sysdba.ACCOUNT.ACCOUNTID WHERE MasterItemNumber = @MasterItemNo"

        cmd.Parameters.AddWithValue("@MasterItemNo", txtMasterItemNo.Text)

        Dim sda As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()

        sda.Fill(dt)

        Return dt

        con.Close()

Open in new window

0
Comment
Question by:printmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39873973
You have two options

1) Populated data tables from both servers separately and then use DataRelation in dataset to join the two datatables.

http://www.codeproject.com/Articles/18304/Using-Data-Relation-with-Data-Table


2) Add one server as a linked server on the other and then use that to join the two tables
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39874306
You can connect to 1 server and use OpenRowset from within your query to get data from the second server. Check http://technet.microsoft.com/en-us/library/ms190312.aspx
0
 

Author Closing Comment

by:printmedia
ID: 39874569
Thank you! I went the linked server route and it works great.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question