A lot depends on your circumstances and who uses the program. I have a
program that reads the SQL Server name from the local registry. I have a
utility the customer runs that prompts for the server, verifies that it can
connect, then writes the server name to the local registry. Other options
are to have the app prompt for the server or read it from a text file.
Assuming the database and instance names never change, your program needs
some way to determine the name of the SQL Server without you rebuilding the
exe.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Save the connection string, or the computer or file name to an INI file, and
use that. Add an option in your program and call it "Database Setup", etc.
Search the newsgroups fro "vb GetPrivateProfileString" for samples.
>I have a program in vb6 that connects to a database in SQL. I don't have
Maybe I'm missing something, but couldn't this be solved by using "(local)"
as the server name in the connection string instead of an explicit computer
name? (Of course, this won't work if you're accessing a named instance of
SQL Server.)
"Richard Mueller [MVP]" wrote:
Thank you for answering my question. I'm not very professional in system
registry, so I don't know how I can find the server name from the local
registry. can you help me about that?
I wrote a utility that writes the name of the server to the registry. The
server name is not otherwise in the registry. I needed a place to save this
information and the only choices are the registry and a text file (like a
*.ini file). I used HKLM\Software\MyCompany\MyApp\Server, where "MyCompany"
is the name of my company and "MyApp" is the name of my application. My
application reads the name of the server when it starts.
When my application is installed the setup program runs my utility at the
end. This prompts the user for the name of the SQL server, verifies that it
can connect, then writes the name to HKLM\Software\MyCompany\MyApp\Server.
The customer must run the utility again if they move the SQL Server database
to another computer. I have seen several other applications that use a text
file to save the server name (and sometimes other information).
Two methods I know of to read and write values to the registry. One is to
use the StdRegProv class of WMI. See this link:
http://www.microsoft.com/technet/scriptcenter/guide/sas_reg_utys.mspx
Another is to use the RegRead and Regwrite methods of the wshShell object.
http://msdn.microsoft.com/en-us/library/x05fawxd.aspx
Since Windows 2000, only Power Users and higher have write access to HKLM
and "Program Files", so use HKCU, "Application Data", or All Users
"Documents" folder. For writing to HKCU, you can use the built-in
GetSetting/SaveSetting functions, which saves to the following key:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\<appname>\<section>
Here are some locations that you can save your data to, and the restrictions
on them:
1 - HKCU: Can be read/written to by the current user, but you can't see
other users settings. Use this for small per user settings.
2 - Per User "Application Data" can be read/written to using the current
user. Put any small Per User INI or files here.
3 - Per User "Local Settings\Application Data" can be read/written to using
the current user. Put any large Per User INI or files here. The files in
this folder do not roam with the user. When a roaming user logs in or out,
HKCU and the user's profile are downloaded or uploaded to a server, except
the files and folders under "Local Settings\Application Data", to save
bandwidth, and reduce the time for logon/logout. For instance, IE saves the
Temporary Internet Files there so they are not uploaded/downloaded. Roaming
users see their settings and desktop the same way regardless of which
computer they login at.
4 - All Users "Application Data": Put any INI or files that are supposed to
be shared among all users here. This folder is not truly shared read/write
by default, you have to adjust the permissions at install time so any user
can read or write to it. If you are using Inno Setup, this can be done by 2
simple lines:
[Dirs]
Name: "{commonappdata}\Company\Software"; Permissions: users-full
By default, in 2000 and XP, files created by Admins or Power Users cannot be
written to by the limited "Users" group, so it's necessary to adjust the
permissions like the above. Microsoft says and does adjust the permissions
for their applications. To adjust the permissions using PDW, you have to
modify it and add code to do it, see MSKB Q240176 for sample code.
5 - Per User "My Documents" folder can be read/written to by an user. Use a
subfolder with your software name to save files that the user is expected to
open later, like graphics files created by your application.
6 - All Users "Documents" folder can be read/written to by an user by
default. This is displayed as "Shared Documents" in Windows Explorer(At the
bottom of the folder list). I think this is the Public folder in Vista.
Use SHGetSpecialFolderLocation() to get the locations above, don't hardcode
them. Search the newsgroup for "vb SHGetSpecialFolderLocation" for samples.
Here is a link for where the above is explained:
"expvb" wrote:
thank you very much.
"Richard Mueller [MVP]" wrote:
thank you very much.