Nat*_*ord 1 c# asp.net connection-string
我该如何制作代码
string connStr = ConfigurationManager.ConnectionStrings "staceys_cakesConnectionString"].ConnectionString;
Run Code Online (Sandbox Code Playgroud)
一般工作而不需要staceys_cakesConnectionString?或者我如何在其他地方设置它,所以我只需要在更改它时将其更改为一个位置?
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
}
}
Run Code Online (Sandbox Code Playgroud)
页面功能示例我如何使用您的建议@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;
}
Run Code Online (Sandbox Code Playgroud)
只需使用常量.就此而言,只需使用静态属性并以此方式获取字符串.
public static class ConnectionStrings
{
public static string StacyesCakes
{
get
{
ConfigurationManager.ConnectionStrings[
"staceys_cakesConnectionString"].ConnectionString;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许您执行以下操作:
using(var conn = new SqlConnection(ConnectionStrings.StaceysCakes))
{
...
}
Run Code Online (Sandbox Code Playgroud)
或者(只调整现有代码):
public DataSet Select()
{
SqlConnection sqlConnection1 = new SqlConnection();
string SqlString = "select * from Categories";
SqlDataAdapter da=new SqlDataAdapter(SqlString,ConnectionStrings.StaceysCakes);
DataSet ds = new DataSet();
da.Fill(ds, "Categories");
return (ds);
}
Run Code Online (Sandbox Code Playgroud)
(你不需要sqlConnection1;你没有在任何地方使用它).
但请注意,因为SqlDataAdapter实现IDisposable并且在执行此代码后您已完成它,所以应将其包含在using块中.
我会将你的函数改写成这样的东西:
public DataSet Select()
{
using(SqlDataAdapter da = new SqlDataAdapter(
"select * from Categories",
ConnectionStrings.StaceysCakes))
{
DataSet ds = new DataSet();
da.Fill(ds, "Categories");
return ds;
}
}
Run Code Online (Sandbox Code Playgroud)