CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2001
    Posts
    1

    Executing SQL script

    Hi
    I have a .sql file which I need to execute by calling it through my VB code. How is this done? Please help
    Sarah


  2. #2
    Join Date
    Sep 2001
    Posts
    11

    Re: Executing SQL script

    Hi,

    First make sure you have a DAO-reference in your references, so VB knows what a database is. Import the sql-code into a string in your program and then execute it like this:

    dim datDb as Database
    dim rsResult as Recordset

    set datdb = YourDatabase
    set rsresult = datdb.openrecordset(yourstring, dbopendynaset)
    do while not rsresult.eof
    print rsresult.fields(0) 'show first tupel
    rsresult.movenext
    loop
    rsresult.close
    datdb.close
    set rsresult=nothing
    set datdb = nothing




    This only works with select-statements, if you have other statements, like insert or delete, you've gotta use Querydef:

    dim datDb as Database
    dim qdfQuery as Querydef

    set datdb = YourDatabase
    set qdfQuery=datDB.createquerydef(Name, Yourquery)
    qdfquery.execute
    datdb.querydefs.delete qdfquery.name
    set qdfquery=nothing
    datdb.close
    set datdb=nothing




    Hope to have been of help,

    BaronQ



  3. #3
    Join Date
    Jun 2001
    Location
    China
    Posts
    28

    Re: Executing SQL script

    I used to use open file to read the contents into a variable, then use SQLExec to execute it. I hope it is helpfule.

    Sam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured