2012-03-27 96 views
1

我是一名初學者,編寫代碼使用Asp.Net和c#在Gridview中顯示數據。我想知道我遵循的方法是否正確。我希望提出有關標準和架構問題的建議,以及我的代碼的最佳實踐,以便我可以相應地修改我的代碼。我感謝你的偉大建議和代碼增補。最佳做法代碼和問題

連接代碼:

public class DemoProjConnectionClass 
    { 
     public SqlConnection DemoProjConnection() 
     { 
      SqlConnection con = new SqlConnection("Data Source=Localhost;Initial Catalog=master;Integrated Security=True"); 
      return con; 
     } 
    } 

域代碼(得到&套):

public class DemoProjDomainClass 
{ 

public int EmpId { get; set; } 
public string EmpName { get; set; } 
public int Salary { get; set; } 

} 

類庫代碼:

public class DemoProjServiceClass 
{ 
    public IList<DemoProjDomainClass> getDemoProjList() 
    { 
     string sqlDemoProjList; 
     sqlDemoProjList = "SELECT EmpId,EmpName,Salary from Employee"; 
     DemoProjConnectionClass x = new DemoProjConnectionClass(); 
     SqlConnection con = x.DemoProjConnection(); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand(sqlDemoProjList, con); 
     cmd.CommandType = CommandType.Text; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds, "tempTable1"); 



     IList<DemoProjDomainClass> DemoProjList = new List<DemoProjDomainClass>(); 
     for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
     { 
      DemoProjDomainClass _obj = new DemoProjDomainClass(); 

      _obj.EmpId = Convert.ToInt16(ds.Tables[0].Rows[i][0]);_obj.EmpName = ds.Tables[0].Rows[i][1].ToString();_obj.Salary = Convert.ToInt16(ds.Tables[0].Rows[i][2]);DemoProjList.Add(_obj); 
     } 
     return DemoProjList; 
    } 
} 

UI代碼

protected void Page_Load(object sender, EventArgs e) 
{ 
    DemoProjServiceClass ob=new DemoProjServiceClass(); 
    GridView1.DataSource = ob.getDemoProjList(); 
    GridView1.DataBind(); 

} 

回答

3

連接代碼:

我不會像硬編碼的ConnectionString的。我將保存在一個配置文件(web.config左右..),並從那裏讀取,這樣我可以隨時更改我的連接字符串,如果需要重新編譯。

類庫代碼

你getDemoProjList方法不要有任何異常處理。我會換行代碼using語句,這樣我不需要擔心華南簡介關閉我的連接

UI

我不認爲你應該加載在pageLoad的數據不檢查它是否是一個回發與否。所以將使用isPostBAck屬性checkk。在將它作爲網格的數據源進行綁定之前,我也會進行空檢查。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
    DemoProjServiceClass ob=new DemoProjServiceClass(); 
    List<DemoProjDomainClass> objList=ob.getDemoProjList(); 
    if(objList!=null) 
    { 
     GridView1.DataSource = objList; 
     GridView1.DataBind(); 
    } 
    } 

} 
+1

請告訴我爲什麼選擇投票? – Shyju 2012-03-27 03:43:47

+0

優秀的評論。我注意到他們。萬分感謝。 – Kurkula 2012-03-27 04:48:00

+0

在類庫代碼中,數據集是最佳選擇使用,然後將其循環列表? – Kurkula 2012-03-27 04:51:04

5

我建議你:

  1. 使用MVC,而不是asp.net經典
  2. 使用LINQ或替代的EntityFramework(CMD,康涅狄格州,適配器,數據集)
  3. 使用中繼器,而不是網格更多性能

在asp.net網站上看​​看tutorial

1

不是。 從這個角度來看,你需要的是數據訪問層。 http://martinfowler.com/eaaCatalog/ 使用數據源體系結構模式項目。 關於你的代碼連接,大部分時間不應該被硬編碼,而是在某種配置文件中定義。 讓我們假設域是好的,但是大部分時間你需要實現在目錄中更多地描述的域模式,如果你有一些域模型的額外邏輯。 避免由於可能的SQL注入而導致SQL查詢的硬編碼,並在適當的地方使用一次性模式(在C#開發中使用「關鍵字」)。對於主要情況,ORMs可以很好地完成基本功能,甚至更多,所以使用SqlCommand只有兩個很好的理由: 當您獲得最佳性能或學習基本知識時。還有缺點。您的可維護性降低,代碼量增加。 從我的預期ASP.Net MVC爲您提供高度可維護和可配置的代碼級別。這就是爲什麼你可以真正關注它。但如果要這樣做,取決於你。

0

爲您的連接代碼的SqlConnection的括號內移動,一切都變成你的webconfig這樣的:

<connectionStrings> <add name="abcConnectionString" connectionString="Data Source=Localhost;Initial Catalog=master;Integrated Security=True providerName="System.Data.SqlClient" /> </connectionStrings> ,然後在你的代碼是這樣引用它:

using System.Configuration; 

string connStr = ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString; 
     SqlConnection Con = new SqlConnection(connStr); 
0

使用企業庫來處理你的數據訪問代碼。

+0

企業庫太舊了 – pylover 2012-03-27 04:47:10