2010-08-11 83 views
1

我知道這可能很簡單,我知道我發佈的代碼是錯誤的,但我越來越接近我的想法。如何返回一個包含數據的對象

我想從文件pageDAL.cs中獲取下面的函數,以便返回對象頁面,併爲我傳入的任何頁面ID返回值。如果我不需要使用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; 
    } 
    } 
} 
+2

對於看起來相當孤立的問題,這是很多代碼。你認爲你可以製作更簡潔的樣本嗎? (關於我的意思,請閱讀:http://www.yoda.arachsys.com/csharp/complete.html) – 2010-08-11 13:17:31

+0

此代碼是否可以編譯? – 2010-08-11 13:21:07

回答

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

不要你只需要在你的GETPAGE()方法返回myPageOBJ,不是頁面?

您還需要使用「myPageObj。」將您的分配放在myPageOBJ成員之前。您可能還想查看對象初始值設定項,以使頁面對象的設置更清晰。

1

您應該避免使用SQL Injection,並使用NHibernate或Fluent NHibernate等ORM來查找分頁結果,例如:How can you do paging with NHibernate?

需要一點時間才能熟悉NHibernate,但一旦做到這一點,就會使事情變得更容易和簡單。

+0

我在學習LINQ,Entity和(NHibernate和/或Fluent NHibernate)。我目前正在獲得報酬以學習DotNet,然後在SharePoint之後,所以我會問我一些SharePoint問題,一旦我覺得我已經學會了足夠多的DotNet以在不同的工作中工作......(以防他們使用任何這些不同的風格獲取工作完成。) – 2010-08-11 14:18:14

+0

我希望我能夠學習.Net,我必須免費學習:) – 2010-08-11 14:20:34

相關問題