Results 1 to 13 of 13

Thread: Connectting SQL 2005 with VB6

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    110

    Connectting SQL 2005 with VB6

    Could you please tell me how I can connect VB6 with SQL 2005 server via ADODB.


    Does anyone know what connection string to use to connect to the database (mdf files)?


    thans

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Connectting SQL 2005 with VB6


  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connectting SQL 2005 with VB6

    In the "SQL Server 2005" sticky thread at the top of this forum (which is also linked from the SQL Server section of the FAQs thread), there is a link in post 4 to an example/tutorial by Microsoft on exactly this.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    110

    Re: Connectting SQL 2005 with VB6

    thanks

    i've tried this

    Code:
    Provider=SQLNCLI;Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=yes;
    but what should i put in the place of a provider..??

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Connectting SQL 2005 with VB6

    You already have SQLNCLI as a Provider. That should be sufficient.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    110

    Re: Connectting SQL 2005 with VB6

    Code:
    Set cn = New ADODB.Connection
        cn.ConnectionString = "Provider=SQLNCLI.1;Integrated Security=SSPI;" & _
            "Persist Security Info=False;" & _
            "AttachDBFileName= C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\bank.mdf;Data Source=KONG-DCCEC8B2B4\sql2005"
    
        cn.Open
            Set rs = New ADODB.Recordset
            rs.Open "Select * from orders", cn
        cn.Close
    i've used te above code but it keeps giving different errors. now its telling me that i've already got that databse existing and therefore i cannot use cn.open

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Connectting SQL 2005 with VB6

    How many times are you trying to open it?

    What is the exact wording of the error message?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connectting SQL 2005 with VB6

    I'm pretty sure you only need to Attach the file once, as it will then be registered in the database engine. From then on, you should specify the database name instead (see what it is called in Management Studio, I presume it will be "bank"), eg: Database=bank

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    110

    Re: Connectting SQL 2005 with VB6

    Error: an attemp to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\bank.mdf failed. A database with the same name exists, or specfied file cannot be opened, or it is located on UNC share.


    I have used the following:
    in the general area:
    vb Code:
    1. Public cn As ADODB.Connection 'this is the connection
    2. Public rs As ADODB.Recordset 'this is the recordset

    in the form load area:
    vb Code:
    1. Set cn = New ADODB.Connection
    2.     cn.ConnectionString = "Provider=SQLNCLI;Integrated Security=SSPI;" & _
    3.         "Persist Security Info=False;" & _
    4.         "AttachDBFileName= C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\bank.mdf;Data Source=KONG-DCCEC8B2B4\sql2005"
    5.  
    6.     cn.Open
    7.         Set rs = New ADODB.Recordset
    8.         rs.Open "Select * from client", cn
    9.     cn.Close

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connectting SQL 2005 with VB6

    That is what I suspected - so you need to change the AttachDBFileName part to a Database part, using the name of the database that is shown in Management Studio.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    110

    Re: Connectting SQL 2005 with VB6

    thanks a lot .. that error is gone...

    now its telling me that the log in has failed. i suppose i need to give it te user name and the password.

    could you please tell me how and where in the string do i specify it?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    110

    Re: Connectting SQL 2005 with VB6

    i am using windows authenticated login... where i use a blank password to log into windows..

    could someone please tell me the syntax of the connection string please ?

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connectting SQL 2005 with VB6

    The part "Integrated Security=SSPI" (or the equivalent "Trusted_Connection=yes") specifies to use your Windows login details, and those details will be passed automatically.

    I have never known that to fail, so I would assume that your Windows user hasn't got permission to connect to the Database, or perhaps even the database server itself. To check (and change it if apt), go into the Security section of Management Studio.

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