|
-
Oct 13th, 2008, 09:29 PM
#1
Thread Starter
Hyperactive Member
Using Connection String from App.Config
For the very first time I am using a SQL file that is not actualy from the server. I am using a local sql datbase. I have added the connection string to my app.config file but now I am not sure how to use it in code
my config string is named MateCare
VB Code:
Private Sub AddPatient()
Dim con As String = ConfigurationSettings.AppSettings("MateCare")
Dim strSp As String = "InsertPatient"
Dim cn As SqlConnection = New SqlConnection
Dim cmd As SqlCommand = New SqlCommand(strSp, cn)
cn.ConnectionString = con
Try
cn.Open()
With cmd
.CommandType = CommandType.StoredProcedure
.Parameters.Add("PatientID", _
SqlDbType.Int).Direction = ParameterDirection.Input
.Parameters("PatientID").Value = txtPatientID.Text
End With
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & _
ex.Source, "ExecuteStartStop Dates", MessageBoxButtons.OK)
End Try
End Sub
-
Oct 13th, 2008, 09:45 PM
#2
Re: Using Connection String from App.Config
You should add your connection string on the Settings tab of the project properties. You can then access it via My.Settings. You might like to follow the Protected Configuration link in my signature for an example of storing a connection string in My.Settings and also accessing the config file in code.
-
Oct 13th, 2008, 09:50 PM
#3
Re: Using Connection String from App.Config
Hey,
The first thing that I noticed was that you are using the ConfigurationSettings class, as of .Net 2.0, you should really be using the ConfigurationManager class. This includes the ability to get to the ConnectionStrings collection in your configuration file using ConfigurationManager.ConnectionStrings, for instance:
Code:
Dim MySettings As ConnectionStringSettings = _
ConfigurationManager.ConnectionStrings("AdventureWorksString")
If Not MySettings Is Nothing Then
Dim MyConnection As New SqlConnection(MySettings.ConnectionString)
Console.WriteLine(MySettings.ConnectionString)
End If
Hope this helps!!
Gary
-
Oct 13th, 2008, 10:07 PM
#4
Thread Starter
Hyperactive Member
Re: Using Connection String from App.Config
I am really lost. I did add the Connection String in the Project Settings and I also followed your link about encrypting the string but I still unsure of how to use the connection string in my block of code.
gep when I tried that I get a Error Type 'ConnectionStringSettings' is not defined.
-
Oct 13th, 2008, 10:16 PM
#5
Re: Using Connection String from App.Config
There's not that much code in that project. I already said in my previous post that you access it via My.Settings and the only place that that's done in my code, which you could have found with a Find In Files, is here:
Code:
'Load the current connection string.
Dim builder As New SqlConnectionStringBuilder(My.Settings.PrimaryConnectionString)
That IS your connection string.
That said, you're making an attempt to get the connection string in your original code but you never use the value you get. It should be obvious that the connection string relates to the SqlConnection so if you don't know how to put them together the logical thing to do SHOULD be to go to the MSDN documentation and read about the SqlConnection class. Alternatively, just about every ADO.NET example or tutorial, such as the Data Access link in my signature, will show you how to use a connection string.
-
Oct 13th, 2008, 10:43 PM
#6
Thread Starter
Hyperactive Member
Re: Using Connection String from App.Config
Thanks I'll keep working on it.
-
Oct 13th, 2008, 10:46 PM
#7
Re: Using Connection String from App.Config
 Originally Posted by FastEddie
I am really lost. I did add the Connection String in the Project Settings and I also followed your link about encrypting the string but I still unsure of how to use the connection string in my block of code.
gep when I tried that I get a Error Type 'ConnectionStringSettings' is not defined.
To clarify, the sample that I posted will work if you include a reference in your project to System.Configuration, and then add an Imports System.Configuration at the top of your code file.
Gary
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
|