How can I make the code
string connStr = ConfigurationManager.ConnectionStrings "staceys_cakesConnectionString"].ConnectionString;
work generically and not need the staceys_cakesConnectionString? Or how can I set it somewhere else so I only have to change it one place when I change it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SC1.Models.DAL
{
public class CategoryDAL
{
public CategoryDAL()
{
}
string connStr = ConfigurationManager.ConnectionStrings["staceys_cakesConnectionString"].ConnectionString;
// select all
public DataSet Select()
{
SqlConnection sqlConnection1 = new SqlConnection();
string SqlString = "select * from Categories";
SqlDataAdapter da = new SqlDataAdapter(SqlString, connStr);
DataSet ds = new DataSet();
da.Fill(ds, "Categories");
return (ds);
}
// save
// insert
// update
// delete
}
}
Example of a page function I have how can I make this one better using your suggestion @Adam or anyone else?
// List
public List<page> Select()
{
List<page> _list = new List<page>();
string SqlStatement = "select * from Pages";
SqlConnection thisConnection = new SqlConnection(connStr);
// Open the Connection
thisConnection.Open();
var thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = SqlStatement;
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
// Create a new instance of the Current Page Object
page currentPage = new page();
// Fill the instance of the Current Page Object
currentPage.PageID = Convert.ToInt32(thisReader["PageID"]);
currentPage.ParentID = Convert.ToInt32(thisReader["ParentID"]);
currentPage.CategoryID = Convert.ToInt32(thisReader["CategoryID"]);
currentPage.Name = thisReader["Name"].ToString();
currentPage.PageHTMLContent = thisReader["PageHTMLContent"].ToString();
currentPage.NavigationText = thisReader["NavigationText"].ToString();
currentPage.TopMenu = Convert.ToBoolean(thisReader["TopMenu"]);
currentPage.SubMenu = Convert.ToBoolean(thisReader["SubMenu"]);
currentPage.DisplayOrder = Convert.ToInt32(thisReader["DisplayOrder"]);
currentPage.Active = Convert.ToBoolean(thisReader["Active"]);
// Add the instance of the Current Page Object to the List<>.
_list.Add(currentPage);
}
// Close the Database
thisConnection.Close();
return _list;
}