Why create connection with ConnectionString that placed in Web.Config?
The answer very simple: because we will possible to change it without compile our application...and its very hopeful in PROD environment, isn't? :-)
Suppose, we have MyDataSet of typed DataSet with one table:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Dashboard.DAL;
namespace Dashboard.Dal
{
public class GetData
{
public static string ConnectionString { get; set; }
public static MyDataset GetData()
{
MyDataSet ds = new MyDataSet();
ConnectionString = ConfigurationManager.ConnectionStrings["
ConnectionString"].ToString();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
string storeProc = "DataBase.dbo.ProcedureName";
SqlCommand command = new SqlCommand(storeProc, conn);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(command);
conn.Open();
da.Fill(ds.TypedDataSet);
}
return ds;
}
}
}
and file web.config:
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=SQL_SERVER_NAME;Initial
Catalog=INIT_CATALOG_OF_DB;Persist Security Info=True;
User ID=USER_NAME;Password=1234" />
</connectionStrings>
</configuration>
Good Connection...