How do I get the connection string

T

Troy Bull

Greetings

I have a simple app with a datasource, and I want to make a call to a
stored procedure. So, what I was trying to do was get the connection
string from Settings.settings,
then basically do this:

SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into test values ('testdata')";
int rows_affected = cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();


except in my code I will call my stored procedure, is this the right
thing to do? How do I get the connection string that is stored in
Settings.settings?

thanks
troy
 
M

Mr. Arnold

Troy Bull said:
Greetings

I have a simple app with a datasource, and I want to make a call to a
stored procedure. So, what I was trying to do was get the connection
string from Settings.settings,
then basically do this:

SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into test values ('testdata')";
int rows_affected = cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();


except in my code I will call my stored procedure, is this the right thing
to do? How do I get the connection string that is stored in
Settings.settings?

http://msdn2.microsoft.com/en-us/library/yy6y35y8(VS.71).aspx
 
P

Peter Bradley

Ysgrifennodd Troy Bull:
Greetings

I have a simple app with a datasource, and I want to make a call to a
stored procedure. So, what I was trying to do was get the connection
string from Settings.settings,
then basically do this:

SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into test values ('testdata')";
int rows_affected = cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();


except in my code I will call my stored procedure, is this the right
thing to do? How do I get the connection string that is stored in
Settings.settings?

thanks
troy

Assuming you have the following in your App.config or Web.config file:

<connectionStrings>
<add name="MyConnectionString"
connectionString="<whatever your connection string is>"
providerName="<whatever your provider is>" />
</connectionStrings>

.... and that you are using .NET 2.0. You can say in the method where
you want to retrieve the connection string, something like:

private string ConnStr =
ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

HTH


Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top