Results 1 to 11 of 11

Thread: How to save and retrieve SQL Server name?

  1. #1

    Thread Starter
    Hyperactive Member Utpal's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    393

    How to save and retrieve SQL Server name?

    Hi everybody,
    I have developed an Inventory application in VB 6.0 - SQL Server 7.0. When the application is run the first time, it will prompt the user to enter the SQL server name, to which the application will connect. I want the application to save the server name somewhere, so that when the application is run the next time, it retrieves the saved server name and connects to that server. I thought of saving to a text file and retrieving from it. But a text file could be easily deleted or modified. Is there a better way?
    It is easy when you know it.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    if this is a local setting, you could always use the registry.
    if not use a centralized inifile or txt file.

    set the files attributes to hidden and system. If someone deletes it, its not by accident
    -= a peet post =-

  3. #3

    Thread Starter
    Hyperactive Member Utpal's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    393
    Thanks Peet! It's a centralized setting I want. So, using a text/ini file is the only alternative it seems. Would using an ini file instead of a text file be better in any way?
    It is easy when you know it.

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    not a big difference. ini is in a way the standard for saving settings, might be what people expect. but then again, an ini file is just a text file built up with headers and items...

    I use ini files when saving settings.
    -= a peet post =-

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sample on using ini files

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetPrivateProfileString _
    3. Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
    4. lpApplicationName As String, ByVal lpKeyName As String, _
    5. ByVal lpDefault As String, ByVal lpReturnedString As _
    6. String, ByVal nSize As Long, ByVal lpFileName As String) As _
    7. Long
    8.  
    9. Private Declare Function WritePrivateProfileString _
    10. Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
    11. lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
    12. lpString As Any, ByVal lpFileName As String) As Long
    13.  
    14.  
    15.  
    16. Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
    17.    Dim strbuffer As String
    18.    Let strbuffer$ = String$(750, Chr$(0&))
    19.    Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    20. End Function
    21.  
    22. Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
    23.     Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    24. End Sub
    25.  
    26. Private Sub Command1_Click()
    27.     'creates Test.ini that look like this:
    28.     '[DBParam]
    29.     'DBNAME=C:\myapp\db\mydb.mdb
    30.     '[Temp]
    31.     'TMPPATH=C:\myapp\tmp
    32.     WriteINI "DBParam", "DBName", "C:\myapp\db\mydb.mdb", "C:\TEST.INI"
    33.     WriteINI "Temp", "TMPPath", "C:\myapp\tmp", "C:\TEST.INI"
    34. End Sub
    35.  
    36. Private Sub Command2_Click()
    37.     'reading the settings saved above :-)
    38.     MsgBox ReadINI("DBParam", "DBName", "C:\TEST.INI")
    39.     MsgBox ReadINI("Temp", "TMPPath", "C:\TEST.INI")
    40. End Sub
    41.  
    42. Private Sub Command3_Click()
    43.     'Deletes the DBNAME key entierly
    44.     'so that the ini looks like this:
    45.     '[DBParam]
    46.     '[Temp]
    47.     'TMPPATH=C:\myapp\tmp
    48.     WriteINI "DBParam", "DBName", vbNullString, "C:\TEST.INI"
    49. End Sub
    -= a peet post =-

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710
    It might be better to actually check for available SQL servers on
    the network instead. Use the SQLDMO object model.
    Once the user select the SQL server save it and upon the re-run
    of the application load it into the cbo, but compare against active SQL servers.

    Add the reference to "Microsoft SQLNamespace Object Library".
    Code:
    Public goSQLApp As SQLDMO.Application    
    Public goNames As SQLDMO.NameList
        
    'POPULATE CBO ONLY WITH SQL SERVERS
    Set goSQLApp = New SQLDMO.Application
    Set goNames = goSQLApp.ListAvailableSQLServers
    
    cboServer.Clear
    For i = 1 To goNames.Count
        cboServer.AddItem goNames.Item(i)
    Next
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Hyperactive Member Utpal's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    393
    Thanks to both!

    Once the user select the SQL server save it and upon the re-run
    of the application load it into the cbo, but compare against active SQL servers.
    Save the SQL server where? What is cbo? Combobox? Do you mean the combobox has to used if the server name is not found in goNames?
    It is easy when you know it.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710
    Use a combobox names cboServer and the code I posted for you
    to populate it so the user can select from the dropdown list, an
    active SQL server.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710
    If you load a saved SQL server name and it is not on-line you will
    get a connection error. So if you do a check on the saved SQL
    server name against the list of active SQL servers in the combo
    box you can trap for the connection error before actually
    connecting and avoind that 30 timeout delay.

    goNames is the object that contains the collection of active SQL
    servers.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Hyperactive Member Utpal's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    393
    Thanks!
    It is easy when you know it.

  11. #11
    New Member
    Join Date
    Oct 2012
    Posts
    3

    Re: How to save and retrieve SQL Server name?

    Hi

    Using this code i get the name "Local", I want to get the exact name of my local server.

    Thnkx

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