는 어떻게 staceys_cakesConnectionString 필요 일반적 아닌 코드어떻게 한 곳만 변경하면 연결 문자열을 설정할 수 있습니까?
string connStr = ConfigurationManager.ConnectionStrings "staceys_cakesConnectionString"].ConnectionString;
일을 할 수 있습니까? 아니면 다른 곳으로 설정할 수 있도록 어떻게 변경할 수 있습니까?
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
}
}
페이지 기능의 예 저는이 제안을 @Adam 또는 다른 사람이 사용하여 어떻게 개선 할 수 있습니까?
// 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;
}
매번 사용하지 않아도되도록 웹 설정에 클래스를 추가하는 방법이 있습니까? –
@Nathan : 사용법은 매우 다른 목적으로 사용됩니다. –
정적이므로 코드를 참조하기 위해 1 줄만 사용하기 때문에 위의 예를 그대로 사용하는 것이 좋습니다. – Jeroen