Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connection using .ini or config file?

Status
Not open for further replies.

Corinne

Programmer
May 15, 2001
146
US
Hi,

I have programmed several years using VB 6 and am now moving to .NET. I have always used an .ini file to create my connection rather than "hard coding" the connection. I have been able to find many examples of building the connection string by referencing the actual string but am only interested in finding a way to do this outside of the code/properties. Has anyone come across this before? Any help is appreciated.

Thanks,
Corinne
 
Connection to what? A database? You can make registry keys for your connection. If I am not using Windows Authentication, I never store the user name and password, I always prompt for it, although it does make sense to store the text for the provider. Is this what you want to do?
 
Sorry that I wasn't clear. Yes, when I try to connect to a database. I will be using Windows authentication.

Thanks,
Corinne
 
You can also use the XML-based config file (do a search in msdn for info). It's the same name as your assembly, but with a .config added on to the end.

I personally like using the registry. One of the drawbacks to using the .config file is that it's read-only (your program can't write to it without jumping through some hoops).

Chip H.
 
chiph,

Thanks for responding to my question. Could you perhaps give me some detailed info on how to use the registry. I'm very new to .NET & am still trying to feel my way around. Also, if using the XML based config file, where exactally would I place the reference to it. I'm use to placing my connection in an .ini file and then calling a small function that grabs the .ini and passes the connection string to my app. What would be the process with .NET? Similar or completely different? I've found several places that give info about hard coding a connection but right now I can't even get that(hard coding) to work. Any help is appreciated.

Thanks,
Corinne
 
I use the following

Code:
Dim Settings As System.Configuration.AppSettingsReader
Dim cnSQL As SqlConnection

cnSQL = new SqlConnection(Ctype( settings.GetValue("SQL.Connection",Gettype(System.String)), String));

and your App.Config file would look like this

<?xml version=&quot;1.0&quot; encoding=&quot;Windows-1252&quot;?>
<configuration>
<appSettings>
<!-- For mor information on the applications settings look in the techinical documentation -->

<!-- The connection strings for the database connections -->
<add key=&quot;SQL.Connection&quot; value=&quot;Server=YourServer;Database=YourDB;integrated security=SSPI&quot; />

<!-- Any other application settings -->


</appSettings>
</configuration>

Regards
 
Corinne,

If you use a config file--its easier to more dynamic I suppose...if say, you changed to a different provider that does not yet exist in the future, just replace that file. But it can be read, deleted, etc, etc.

If you use the registry, you can use the SaveSetting and GetSetting methods like you did in VB 6, or there are new namespaces to access different parts of the registry, but for all practical purposes, the vb and vba programs area will probably work good enough. You can read about the other namespaces in the help.
 
JohnEfford,

Thanks for your post. I tried that in my app & now receive the error:
&quot;Unhandled exception of type &quot;System.NullReferenceException&quot; occured in .exe. Object reference not set to an instance of an object.

What references do I need to add to my project when using the above code?

Thanks,
Corinne

 
Upon researching for this error I found that the above error message is referenced when on object is set to &quot;Nothing&quot;. When debugging I found that the &quot;Settings&quot; value in John's original post is &quot;Nothing&quot;. I'm just not sure what do do with this information.

Thanks,
Corinne
 
Corinne -

John left out a few steps, such as instantiating his settings object (hint: use New)

So far as using the registry, take a look at the Registry and RegistryKey classes. It's in the Microsoft.Win32 namespace, and you'll have to add a reference to it in your project settings window.

Some pseudo code for reading from the registry is:

1. Get a RegistryKey object from the LocalMachine property of the Registry object.

2. Open the key underneath that using OpenSubKey, assigning the result to another RegistryKey object

3. Use the GetValue method to read the value of your key as a string object.

Writing is very similar, only you'd call SetValue instead.

Don't forget that reading/writing the registry is a privledged operation, and the caller needs the RegistryPermission permission. Which is present by default for code running on your local machine. It may or may not be present for code running off your network, and is certainly not allowed for code running off the Internet.

Chip H.


 
Corinne

Sorry I posted code that did not work, I was cut and pasting it from an app and did not test it on its own to check that it worked, as ChipH says you need to instantiate the appsettings opject:

use

Code:
 Dim Settings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader()


to declare and instantiate the app settings reader object.

For more information an the sttings file check out this article



Personally I prefer to use an settings file, I just do not like creating and changing registry settings.

Regards
 
Hi...

Can this be addapted to read a config file from a remote system for ID perposes? I mean, I'm writing an app that checked a set of systems for data and the first thing that has to happen is it reads an ID file that contains the systems remote ID...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top