3

I am currently trying to establish a connection between an ASP.NET web site project and a Database built by SQL Server 2008 R2.

The way I am required to do so is to use the connectionString from the Web.config page, but I have no idea what value to give it or how to establish a connection using said value. (Using C#)

Any help would be appreciated, as I found next to no information about the subject.

Here is the (default) value that is currently in the Web.config page:

<connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
5
  • 2
    This may be useful to you...Its a trick for creating Connection strings stackoverflow.com/questions/3954029/… Commented Nov 4, 2011 at 19:47
  • 2
    "I found next to no information about the subject" - there are over 5000 hits on SO alone for 'connectionstring'!!! Commented Nov 4, 2011 at 19:51
  • @KreepN I did, but I am not sure about how proper my connection string is... this is all I put in it, and it works: Server=loca5lhost;Trusted_Connection=yes;Database=levidb. Commented Nov 8, 2011 at 20:37
  • Correct. When you're connection to a local DB, the string itself doesn't require much configuring. When you're connection to a SQL server instance on another machine is when it seems to look different and require more fields. Commented Nov 8, 2011 at 20:38
  • Here's my local one which I snagged from visual studio: Data Source=J\SQLEXPRESS;Initial Catalog=MyCatalog;Persist Security Info=True;User ID=*****;Password=*********** Commented Nov 8, 2011 at 20:39

3 Answers 3

5

Use Configuration Manager:

using System.Data.SqlClient;
using System.Configuration;

string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

using(SqlConnection SqlConnection = new SqlConnection(connectionString));

//The rest is here to show you how this connection would be used. But the code above this comment is all you really asked for, which is how to connect.

{

   SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();
   SqlCommand SqlCommand = new SqlCommand();

   SqlConnection.Open();
   SqlCommand.CommandText = "select * from table";
   SqlCommand.Connection = SqlConnection;
   SqlDataReader dr = SqlCommand.ExecuteReader(CommandBehavior.CloseConnection);

}
Sign up to request clarification or add additional context in comments.

3 Comments

If en exception will occur before a reader execution, the connection will not be closed. Thus I recommend to use using block.
Personal preference, as he could be using it in a try-catch block. I'd agree, however, and will make sure I notate that accordingly.
This does show me how to retrieve the connection string, so that part is pretty much answered for me. What to write in the Web.config file is another matter. Currently I simply did this: Server=localhost;Trusted_Connection=yes;Database=mydb and it works, but it seems far off from the default value that is in there.
0

This article about Connect to SQL Server Using SQL Authentication in ASP.NET will probably give you a better idea of what need to be done.

As a pre check, just check if your mssqlserver services are running.

1 Comment

Problem is, I'm using ASP.Net 4 and I don't know how applicable information about ASP.Net 2 is.
0
string connectionString =  ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
    command.CommandText = commandText;

    // command.Parameters.AddWithValue("@param", value);

    connection.Open();
    command.ExecuteNonQuery(); // or command.ExecuteScalar() or command.ExecuteRader()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.