Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Running a Distributed Query with Visual Ba...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Running a Distributed Query with Visual Basic Using ADO
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-07-05

    Table of Contents:
  • Running a Distributed Query with Visual Basic Using ADO
  • Adding a linked server to the SQL 2000 server
  • Running a query against the Linked Server
  • Probing the Linked Server

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Running a Distributed Query with Visual Basic Using ADO - Running a query against the Linked Server


    (Page 3 of 4 )

    You can run queries with two kinds of command text. The four-part syntax to access all the columns from the Employee table in the linked server is: 'SELECT * from GoodLink...Employees'. Although this is supposed to consist of  four parts including  catalog and schema, these are empty, as Microsoft Jet does not support these objects.

    The other method is to use the openquery() function which takes the name of the linked server, and a query that can be run against that linked server in a string format. You may not pass variables to this query. Both are shown in the code.

    Once the connection is open you set some of the ADO Recordset properties such as CursorLocation and LockType. With these, and using the connection string, you can open the recordset as shown in the code. The commented code will write just one column of the Employees table to the immediate screen, stepping through all the records until it reaches the end. If you set the Datasource property of the DataGrid to the recordset, the data grid is automatically filled with all the columns and rows from the Employees table. When you are writing the code, make sure you use the intellisense feature; it simplifies coding, as shown here.

    Private Sub Command2_Click()
    Dim connstr As String
    Dim strsql As String
    'strsql = "SELECT * from GoodLink...Employees"
    strsql = "Select * from openquery" _
    & "(GoodLink, 'Select * from Employees')"
    connstr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
    & "Persist Security Info=False; Initial Catalog=Northwind;" _
    & "Data Source=XPHTEK"
    adocon.CursorLocation = adUseClient
    adocon.Open connstr
    if adors.state=1 then
    adors.close
    end if
    adors.Open strsql, connstr, adOpenStatic, adLockReadOnly
    'While Not adors.EOF
    'Debug.Print adors.Fields(0).Value
    'adors.MoveNext
    'Wend
    Set DataGrid1.DataSource = adors
    adocon.close
    End Sub
    

    When you run the form and click on the Command2 button you will see the following screen. After the first time you click on the Command1 button you must not click on this button (Command1) again as it will try to create an existing object, and VB objects, by producing a run time error. If you want, you can execute another stored procedure to drop the object when the form loads, or better still drop it from Enterprise Manager. However, you will not be creating the linked server every time, so you may arrange for the Command1 button to be inactive.

    More Visual Basic.NET Articles
    More By Jayaram Krishnaswamy


       · The trend in the future is for increased distibuted data as it adds to agility....
     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek