我知道这可能很简单,我知道我发布的代码是错误的,但我认为我越来越近了。
我想从文件pageDAL.cs获取下面的函数,返回对象页面,其中包含我传入的任何pageID的值。如果我不需要使用DataSet或DataTable那么好。什么是最好的方式?
public page getPage(int _pageID)
{
DataTable dt = new DataTable;
using (SqlDataAdapter da = new SqlDataAdapter("select PageID,ParentID,CategoryID,Name,PageHTMLContent,NavigationText,TopMenu,SubMenu,DisplayOrder,Active from Page where PageID = " + _PageID, ConnectionStrings.StaceysCakes))
{
da.Fill(dt);
dt.AsEnumerable().Select(r => page)
{
page myPageOBJ = new page();
myPageObj.PageID = r.Field<int>("PageID"),
ParentID = r.Field<int>("ParentID"),
CategoryID = r.Field<int>("CategoryID"),
Name = r.Field<string>("Name"),
PageHTMLContent = r.Field<string>("PageHTMLContent"),
NavigationText = r.Field<string>("NavigationText"),
TopMenu = r.Field<bool>("TopMenu"),
SubMenu = r.Field<bool>("SubMenu"),
DisplayOrder = r.Field<int>("DisplayOrder"),
Active = r.Field<bool>("Active"),
});
}
return page;
}
page.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace sc2.Models.page
{
public class page
{
public int PageID { get; internal set; }
public int ParentID { get; internal set; }
public int CategoryID { get; internal set; }
public string Name { get; internal set; }
public string PageHTMLContent { get; internal set; }
public string NavigationText { get; internal set; }
public bool TopMenu { get; internal set; }
public bool SubMenu { get; internal set; }
public int DisplayOrder { get; internal set; }
public bool Active { get; internal set; }
public page()
{
}
}
}
pageBLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
//using sc2.Models;
namespace sc2.Models.page
{
public class pageBLL
{
pageDAL myPageDAL = new pageDAL();
public pageBLL(){
}
public page getPage()
{
page PageOBJ = new page();
return PageOBJ;
}
public page getPage(int _pageID)
{
return myPageDAL.getPage(_pageID);
}
public string save(page _page)
{
return myPageDAL.save(_page);
}
public List<page> Select()
{
return (myPageDAL.Select());
}
public List<page> Select(string _OrderBy)
{
return (myPageDAL.Select(_OrderBy));
}
public DataSet Get(int _PageID)
{
return (myPageDAL.Get(_PageID));
}
public void DeletePage(int _PageID)
{
myPageDAL.DeletePage(_PageID);
}
}
}
pageDLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;
namespace sc2.Models.page
{
public class pageDAL
{
public pageDAL()
{
}
// List Order By
public List<page> Select(string _OrderBy)
{
string sqlStatement = _OrderBy;
return All(sqlStatement);
}
// Select List of Objects
public List<page> Select()
{
string sqlStatement = "DisplayOrder";
return All(sqlStatement);
}
// Return List of Objects
public List<page> All(string _sqlStatement)
{
var sqlResults = new DataTable();
string sqlStatement = "select PageID,ParentID,CategoryID,Name,PageHTMLContent,NavigationText,TopMenu,SubMenu,DisplayOrder,Active from page";
if (!String.IsNullOrEmpty(_sqlStatement))
{
sqlStatement += " Order By " + _sqlStatement;
}
using (SqlConnection conn = new SqlConnection(ConnectionStrings.StaceysCakes))
{
using (SqlCommand command = new SqlCommand(sqlStatement, conn))
{
var adapter = new SqlDataAdapter(command);
adapter.Fill(sqlResults);
}
}
return sqlResults.AsEnumerable().Select(r => new page()
{
PageID = r.Field<int>("PageID"),
ParentID = r.Field<int>("ParentID"),
CategoryID = r.Field<int>("CategoryID"),
Name = r.Field<string>("Name"),
PageHTMLContent = r.Field<string>("PageHTMLContent"),
NavigationText = r.Field<string>("NavigationText"),
TopMenu = r.Field<bool>("TopMenu"),
SubMenu = r.Field<bool>("SubMenu"),
DisplayOrder = r.Field<int>("DisplayOrder"),
Active = r.Field<bool>("Active"),
}).ToList();
}
public page getPage(int _pageID)
{
DataTable dt = new DataTable;
using (SqlDataAdapter da = new SqlDataAdapter("select PageID,ParentID,CategoryID,Name,PageHTMLContent,NavigationText,TopMenu,SubMenu,DisplayOrder,Active from Page where PageID = " + _PageID, ConnectionStrings.StaceysCakes))
{
da.Fill(dt);
dt.AsEnumerable().Select(r => page)
{
page myPageOBJ = new page();
myPageObj.PageID = r.Field<int>("PageID"),
ParentID = r.Field<int>("ParentID"),
CategoryID = r.Field<int>("CategoryID"),
Name = r.Field<string>("Name"),
PageHTMLContent = r.Field<string>("PageHTMLContent"),
NavigationText = r.Field<string>("NavigationText"),
TopMenu = r.Field<bool>("TopMenu"),
SubMenu = r.Field<bool>("SubMenu"),
DisplayOrder = r.Field<int>("DisplayOrder"),
Active = r.Field<bool>("Active"),
});
}
return page;
}
// (DataSet) get
public DataSet Get(int _PageID)
{
using (SqlDataAdapter da = new SqlDataAdapter("select PageID,ParentID,CategoryID,Name,PageHTMLContent,NavigationText,TopMenu,SubMenu,DisplayOrder,Active from Page where PageID = " + _PageID, ConnectionStrings.StaceysCakes))
{
DataSet ds = new DataSet();
da.Fill(ds, "Pages");
return (ds);
}
}
// Save
public string save(page _page)
{
string errorMessage = "";
if (_page.CategoryID == 0)
{
InsertPage(_page.Name, _page.PageHTMLContent, _page.NavigationText, _page.TopMenu, _page.SubMenu, _page.DisplayOrder, _page.Active);
}
else
{
UpdatePage(_page.CategoryID, _page.Name, _page.PageHTMLContent, _page.NavigationText, _page.TopMenu, _page.SubMenu, _page.DisplayOrder, _page.Active);
}
return errorMessage;
}
// Insert Page
public string InsertPage(string _Name, string _PageHTMLContent, string _NavigationText, bool _TopMenu, bool _SubMenu, int _DisplayOrder, bool _Active)
{
SqlConnection myConnection = new SqlConnection(ConnectionStrings.StaceysCakes);
string SqlStatement = "insert into Page (Name,PageHTMLContent,NavigationText,TopMenu,SubMenu,DisplayOrder,Active) values ('"
+ _Name + "','"
+ _PageHTMLContent + "','"
+ _NavigationText + "','"
+ _TopMenu + "','"
+ _SubMenu + "',"
+ _DisplayOrder + ",'"
+ _Active + "');";
string errorMessage = "";
try
{
SqlCommand myCommand = new SqlCommand(SqlStatement);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
errorMessage = "There was an error with the database insert.";
Trace.Write("Database unavailable with Message: ", e.Message);
Trace.Write("Stack Trace: ", e.StackTrace);
// Throw the exception higer for logging and notification
throw;
}
// Clean up any loose ends.
finally
{
myConnection.Close();
}
// Return the error message if there is one.
return errorMessage;
}
// Update Page
public string UpdatePage
(int _PageID,
string _Name,
string _PageHTMLContent,
string _NavigationText,
bool _TopMenu,
bool _SubMenu,
int _DisplayOrder,
bool _Active)
{
string SqlStatement = "UPDATE Page SET Name='" + _Name + "', PageHTMLContent='" + _PageHTMLContent + "', NavigationText='" + _NavigationText + "', TopMenu='" + _TopMenu + "', SubMenu='" + _SubMenu + "', DisplayOrder=" + _DisplayOrder + ", Active='" + _Active + "' where PageID = '" + _PageID + "'";
SqlConnection myConnection = new SqlConnection(ConnectionStrings.StaceysCakes);
string errorMessage = "";
try
{
SqlCommand myCommand = new SqlCommand(SqlStatement);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
errorMessage = "There was an error with the database update.";
Trace.Write("Database unavailable with Message: ", e.Message);
Trace.Write("Stack Trace: ", e.StackTrace);
// Throw the exception higer for logging and notification
throw;
}
// Clean up any loose ends.
finally
{
myConnection.Close();
}
// Return the error message if there is one.
return errorMessage;
}
// Delete Page
public string DeletePage(int _PageID)
{
string SqlStatement = "Delete from Page where PageID = '" + _PageID + "'";
SqlConnection myConnection = new SqlConnection(ConnectionStrings.StaceysCakes);
string errorMessage = "";
try
{
SqlCommand myCommand = new SqlCommand(SqlStatement);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
errorMessage = "There was an error with the database delete.";
Trace.Write("Database unavailable with Message: ", e.Message);
Trace.Write("Stack Trace: ", e.StackTrace);
// Throw the exception higer for logging and notification
throw;
}
// Clean up any loose ends.
finally
{
myConnection.Close();
}
// Return the error message if there is one.
return errorMessage;
}
}
}
答案 0 :(得分:1)
查看 SqlDataReader 类。它比SqlDataAdapter和DataTable更简单,开销更少。
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
另外,请查看here
var results = new List<page>();
string queryString = "SELECT PageID,ParentID...";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
page p= new page();
p.PageID = reader["PageID"];
//...
results.Add(p);
}
reader.Close();
}
return results;
答案 1 :(得分:1)
您是否只需要在getPage()方法中返回myPageOBJ而不是页面?
您还需要在使用“myPageObj”进行myPageOBJ成员作业之前。您可能还想检查对象初始值设定项,以使页面对象的设置更清晰。
答案 2 :(得分:1)
你应该避免使用SQL Injection并使用像NHibernate或Fluent NHibernate这样的ORM来提供分页结果,例如参见:How can you do paging with NHibernate?。
使用NHibernate加快速度需要一段时间,但一旦你这样做就会让事情变得更容易和简单。