Results 1 to 25 of 25

Thread: a tough one, regarding sql server databases

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    Cool a tough one, regarding sql server databases

    Greetings,

    OK, here's one that's got me intimidated. Someone just pointed me in the right direction regarding allowing the user to browse for an mdb (Microsoft Access database) file, and it works great.

    Now, I need to allow the user to select any of the existing SQL Server 7.0 databases on the system. I know its not the same as browsing for a file. So what would I need to do for the event cmdSQLServer_Click to allow the user to select a SQL Server 7.0 database?

    Thank you,
    Jim

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Would it still somehow be possible to use the browse for file dialog box for this somehow, or does it require another direction?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    What? N0 one has an answer after several hours?

  4. #4
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    What you are asking is quite straight forward if you are using ADO or ODBC. How are you connecting to the mdb?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Originally posted by freewilly
    What you are asking is quite straight forward if you are using ADO or ODBC. How are you connecting to the mdb?
    ADO. But how can we browse for soemthing that is not a file?

  6. #6
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    I'll do bit of research to find out how to get a list of available SQL Servers. What you will end up with is a list of Servers similar to the list you get when making an ODBC connection.

    I'll have a look within the hour. Get back to me me if you find the answer before that.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Hey Freewilly, or Anybody,

    Did anyone figure anything out. I keep looking around and finding nothing.

    Heeelllppp!!!

    I just need to click on abutton and have a list of SQL Server databases displayed for the user to choose from. How do we do it since we can't necesarily browse for one?

    Thank you,
    Jim

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Use SQLDMO ... I know it's possible to get not only a list of SQL Servers, but once connected, a list of databases on that server.
    Give me a few and I will see if I can find the code.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    I don't have the code for this, but my thoughts are that you would first look for the available DSN's on the computer and then connect using each, which you would then be able to find out more about the DB, i.e. Databases, tables, etc.
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Originally posted by seoptimizer2001
    I don't have the code for this, but my thoughts are that you would first look for the available DSN's on the computer and then connect using each, which you would then be able to find out more about the DB, i.e. Databases, tables, etc.
    So what's the code for searching for a DSN, since its not a directory search?

  11. #11
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    File DSN's are in C:\Program Files\Common Files\ODBC\Data Sources
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    okay.... here it is......
    VB Code:
    1. Private Sub cmdConnect_Click()
    2.     If chkUseNT Then
    3.         Login cboServername, "", "", chkUseNT
    4.     Else
    5.         Login cboServername, txtUserName, txtPassWord, chkUseNT
    6.     End If
    7. End Sub
    8.  
    9. Sub FillComboFromCollection(oCollection As Object, cbo As ComboBox)
    10.     On Error GoTo HandleError
    11.     cbo.Clear
    12.    
    13.     Dim oObject As Object
    14.     For Each oObject In oCollection
    15.         Select Case oObject.TypeOf
    16.         Case SQLDMOObj_TargetServer
    17.             cbo.AddItem oObject.ServerName
    18.            
    19.         Case SQLDMOObj_RemoteLogin
    20.             cbo.AddItem oObject.LocalName
    21.            
    22.         Case SQLDMOObj_LinkedServerLogin
    23.             cbo.AddItem oObject.LocalLogin
    24.            
    25.         Case Else
    26.             cbo.AddItem oObject.Name
    27.         End Select
    28. NoValue:
    29.     Next oObject
    30.    
    31.     Exit Sub
    32.    
    33. HandleError:
    34.     Debug.Print Err, Err.Description
    35.     Resume NoValue
    36.  
    37. End Sub
    38.  
    39. Public Sub Login(ServerName As String, UserName As String, Password As String, Optional UseNT As Boolean = False)
    40.    
    41.     NL = Chr$(13) & Chr$(10)
    42.     bConnected = False
    43.    
    44.     Set oSQLServer = New SQLDMO.SQLServer
    45.     oSQLServer.LoginTimeout = 10
    46.     oSQLServer.ODBCPrefix = False
    47.     oSQLServer.ApplicationName = "SQL Documentor"
    48.     oSQLServer.LoginSecure = UseNT
    49.     oSQLServer.Connect ServerName, UserName, Password
    50.     oSQLServer.Application.GroupRegistrationServer = ""
    51.    
    52.     oSQLServer.Configuration.ShowAdvancedOptions = True
    53.    
    54.     Set oJobServer = oSQLServer.JobServer
    55.     Set oReplication = oSQLServer.Replication
    56.     Set oConfiguration = oSQLServer.Configuration
    57.    
    58.    
    59.     FillComboFromCollection oSQLServer.Databases, frmTableSelector.cboDatabases
    60. End Sub

    It's going to take some tweaking..... but I think you can follow it ..... it's a chunk from an Excell add in we created to login to a SQL Server, then allows the user to select a DB, then run some DB functions..... but anyway.... the key pieces are here.
    Good luck!
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by seoptimizer2001
    File DSN's are in C:\Program Files\Common Files\ODBC\Data Sources
    Not every one uses DSNs....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    Originally posted by techgnome
    Not every one uses DSNs....
    Well I would think that it would be a good place to start looking. Besides that I don't know how else you could check "Available" SQL servers to the machine if you don't know what servers you are looking for.
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Originally posted by techgnome
    okay.... here it is......
    VB Code:
    1. Private Sub cmdConnect_Click()
    2.     If chkUseNT Then
    3.         Login cboServername, "", "", chkUseNT
    4.     Else
    5.         Login cboServername, txtUserName, txtPassWord, chkUseNT
    6.     End If
    7. End Sub
    8.  
    9. Sub FillComboFromCollection(oCollection As Object, cbo As ComboBox)
    10.     On Error GoTo HandleError
    11.     cbo.Clear
    12.    
    13.     Dim oObject As Object
    14.     For Each oObject In oCollection
    15.         Select Case oObject.TypeOf
    16.         Case SQLDMOObj_TargetServer
    17.             cbo.AddItem oObject.ServerName
    18.            
    19.         Case SQLDMOObj_RemoteLogin
    20.             cbo.AddItem oObject.LocalName
    21.            
    22.         Case SQLDMOObj_LinkedServerLogin
    23.             cbo.AddItem oObject.LocalLogin
    24.            
    25.         Case Else
    26.             cbo.AddItem oObject.Name
    27.         End Select
    28. NoValue:
    29.     Next oObject
    30.    
    31.     Exit Sub
    32.    
    33. HandleError:
    34.     Debug.Print Err, Err.Description
    35.     Resume NoValue
    36.  
    37. End Sub
    38.  
    39. Public Sub Login(ServerName As String, UserName As String, Password As String, Optional UseNT As Boolean = False)
    40.    
    41.     NL = Chr$(13) & Chr$(10)
    42.     bConnected = False
    43.    
    44.     Set oSQLServer = New SQLDMO.SQLServer
    45.     oSQLServer.LoginTimeout = 10
    46.     oSQLServer.ODBCPrefix = False
    47.     oSQLServer.ApplicationName = "SQL Documentor"
    48.     oSQLServer.LoginSecure = UseNT
    49.     oSQLServer.Connect ServerName, UserName, Password
    50.     oSQLServer.Application.GroupRegistrationServer = ""
    51.    
    52.     oSQLServer.Configuration.ShowAdvancedOptions = True
    53.    
    54.     Set oJobServer = oSQLServer.JobServer
    55.     Set oReplication = oSQLServer.Replication
    56.     Set oConfiguration = oSQLServer.Configuration
    57.    
    58.    
    59.     FillComboFromCollection oSQLServer.Databases, frmTableSelector.cboDatabases
    60. End Sub

    It's going to take some tweaking..... but I think you can follow it ..... it's a chunk from an Excell add in we created to login to a SQL Server, then allows the user to select a DB, then run some DB functions..... but anyway.... the key pieces are here.
    Good luck!


    I'm running your code and creating the controls as necessary; however, I'll need your user type SQLDMO definition, used in your Login method.

    Set oSQLServer = New SQLDMO.SQLServer

    Thank you,
    Jim

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    It's not a User Type...... Add a reference to SQLDMO Object Library. In fact, I think that's what it's called.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Hmm, OK you're right on the SQLDMO reference; now I'm thinking that when the form loads, a list of servers should be displayed, so that one can be selected for connection, sot ht we can then view its databases?

    Where, or how, are the servers loaded into the combo box?
    Jim

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Hey techgnome, are you out there? I posted aquestion for you in the above post last night.

  19. #19
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Sorry, didn't see it until this AM......
    This snippet is from another internal app we use. it uses the same SQLDMO to get the SQL Servers on the form load.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim dmoApp As SQLDMO.Application
    3.     Dim dmoServerGroup As ServerGroup
    4.     Dim dmoServer As RegisteredServer
    5.    
    6.     Set dmoApp = New SQLDMO.Application
    7.     lstServers.AddItem "(Local)"
    8.     For Each dmoServerGroup In dmoApp.ServerGroups
    9.         For Each dmoServer In dmoServerGroup.RegisteredServers
    10.             lstServers.AddItem dmoServer.Name
    11.         Next
    12.     Next
    13.     lstServers.Text = "(Local)"
    14.    
    15.     Set dmoApp = Nothing
    16.     Set dmoServerGroup = Nothing
    17.     Set dmoServer = Nothing
    18. End Sub
    Let me know how this works for ya.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Originally posted by techgnome
    Sorry, didn't see it until this AM......
    This snippet is from another internal app we use. it uses the same SQLDMO to get the SQL Servers on the form load.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim dmoApp As SQLDMO.Application
    3.     Dim dmoServerGroup As ServerGroup
    4.     Dim dmoServer As RegisteredServer
    5.    
    6.     Set dmoApp = New SQLDMO.Application
    7.     lstServers.AddItem "(Local)"
    8.     For Each dmoServerGroup In dmoApp.ServerGroups
    9.         For Each dmoServer In dmoServerGroup.RegisteredServers
    10.             lstServers.AddItem dmoServer.Name
    11.         Next
    12.     Next
    13.     lstServers.Text = "(Local)"
    14.    
    15.     Set dmoApp = Nothing
    16.     Set dmoServerGroup = Nothing
    17.     Set dmoServer = Nothing
    18. End Sub
    Let me know how this works for ya.

    Thanks! Let me give it a shot right now, and I'll get back with ya in a few.
    Jim

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Hey techgnome,

    Thanks for tfopr th eFomLoad code, it seemst tpwork. Can you also please send me the frmTableSelector form, which is called in the line below?

    FillComboFromCollection oSQLServer.Databases, frmTableSelector.cboDatabases


    Thank you,
    Jim

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Unfortunatly, we're starting to tread into proprietary code areas. Since it was an internaly developed app, I can't just give it all away, but I'll give you what I can (stupid NDS's). The form you referenced, is basically just the main form, and the call fills the specified combo box with the list of databases.
    Once you have the login info, the server name and a database name, what you want to do with it is completely up to you.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    Hey techgnome

    is there a way to do this with ADO? I just want a list of the databases on a server.

  24. #24
    Addicted Member PhilRob56's Avatar
    Join Date
    Oct 2002
    Location
    New York
    Posts
    249
    I found this.....
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    4.  
    5. Private Declare Function NetServerEnum Lib "netapi32" ( _
    6.         strServername As Any, _
    7.         ByVal level As Long, _
    8.         bufptr As Long, _
    9.         ByVal prefmaxlen As Long, _
    10.         entriesread As Long, _
    11.         totalentries As Long, _
    12.         ByVal servertype As Long, _
    13.         strDomain As Any, _
    14.         resumehandle As Long) As Long
    15.  
    16. Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long
    17.  
    18. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    19.  
    20. Private Const SV_TYPE_SERVER As Long = &H2
    21. Private Const SV_TYPE_SQLSERVER As Long = &H4
    22.  
    23. Private Type SV_100
    24.     platform As Long
    25.     name As Long
    26. End Type
    27.  
    28. Public Sub GetSQLServers()
    29.     '
    30.     ' You could change this to be a function returning
    31.     ' a list of the SQL servers in a ADOR Recordset or an array etc.
    32.     '
    33.     ' At present, it just does a debug.print of all the
    34.     ' SQL servers on the network.
    35.     '
    36.     '
    37.     Dim l As Long
    38.     Dim entriesread As Long
    39.     Dim totalentries As Long
    40.     Dim hREsume As Long
    41.     Dim bufptr As Long
    42.     Dim level As Long
    43.     Dim prefmaxlen As Long
    44.     Dim lType As Long
    45.     Dim domain() As Byte
    46.     Dim i As Long
    47.     Dim sv100 As SV_100
    48.  
    49.     level = 100
    50.     prefmaxlen = -1
    51.  
    52.     lType = SV_TYPE_SQLSERVER
    53.     domain = "placeYourDomainNameHere" & vbNullChar
    54.     l = NetServerEnum(ByVal 0&, _
    55.             level, _
    56.             bufptr, _
    57.             prefmaxlen, _
    58.             entriesread, _
    59.             totalentries, _
    60.             lType, _
    61.             domain(0), _
    62.             hREsume)
    63.  
    64.     If l = 0 Or l = 234& Then
    65.         For i = 0 To entriesread - 1
    66.             CopyMemory sv100, ByVal bufptr, Len(sv100)
    67.             Debug.Print Pointer2stringw(sv100.name)
    68.             bufptr = bufptr + Len(sv100)
    69.         Next i
    70.     End If
    71.     NetApiBufferFree bufptr
    72.     '
    73. End Sub
    74.  
    75. Private Function Pointer2stringw(ByVal l As Long) As String
    76.     Dim buffer() As Byte
    77.     Dim nLen As Long
    78.     '
    79.     nLen = lstrlenW(l) * 2
    80.     If nLen Then
    81.         ReDim buffer(0 To (nLen - 1)) As Byte
    82.         CopyMemory buffer(0), ByVal l, nLen
    83.         Pointer2stringw = buffer
    84.     End If
    85. End Function
    Some days you're the dog,
    and some days you're the hydrant.


    VB6 Enterprise

  25. #25
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    Someone posted in the forums...

    though I can't remember where, how to do it with ADO. just use a SQL string of:

    SELECT RTrim(name) AS Name FROM master.dbo.sysdatabases

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