-
May 16th, 2003, 01:00 AM
#1
Thread Starter
Hyperactive Member
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.
-
May 16th, 2003, 01:21 AM
#2
-= B u g S l a y e r =-
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
-
May 16th, 2003, 01:42 AM
#3
Thread Starter
Hyperactive Member
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.
-
May 16th, 2003, 02:02 AM
#4
-
May 16th, 2003, 02:06 AM
#5
-= B u g S l a y e r =-
sample on using ini files
VB Code:
Option Explicit
Private Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString As _
String, ByVal nSize As Long, ByVal lpFileName As String) As _
Long
Private Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
lpString As Any, ByVal lpFileName As String) As Long
Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Private Sub Command1_Click()
'creates Test.ini that look like this:
'[DBParam]
'DBNAME=C:\myapp\db\mydb.mdb
'[Temp]
'TMPPATH=C:\myapp\tmp
WriteINI "DBParam", "DBName", "C:\myapp\db\mydb.mdb", "C:\TEST.INI"
WriteINI "Temp", "TMPPath", "C:\myapp\tmp", "C:\TEST.INI"
End Sub
Private Sub Command2_Click()
'reading the settings saved above :-)
MsgBox ReadINI("DBParam", "DBName", "C:\TEST.INI")
MsgBox ReadINI("Temp", "TMPPath", "C:\TEST.INI")
End Sub
Private Sub Command3_Click()
'Deletes the DBNAME key entierly
'so that the ini looks like this:
'[DBParam]
'[Temp]
'TMPPATH=C:\myapp\tmp
WriteINI "DBParam", "DBName", vbNullString, "C:\TEST.INI"
End Sub
-
May 16th, 2003, 02:08 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
May 16th, 2003, 02:26 AM
#7
Thread Starter
Hyperactive Member
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.
-
May 16th, 2003, 11:38 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
May 16th, 2003, 11:55 AM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
May 17th, 2003, 12:39 AM
#10
Thread Starter
Hyperactive Member
It is easy when you know it.
-
Nov 16th, 2012, 05:43 AM
#11
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|