2012-03-03 24 views
0

大家好,好日子。我有一個關於編碼的問題,我有很長的代碼行來顯示16個項目從SQL服務器到16個文本塊,沒有錯誤,但我想保持短的代碼行。這是2周的TextBlocks(滿分16)例子代碼:將sql server數據顯示爲16個文本塊

  orgDa.SelectCommand = conn.CreateCommand(); 
      orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=1"; 
      orgDa.SelectCommand.CommandType = CommandType.Text; 
      orgDa.Fill(ds, "Organizationtbl"); 

      deptDa.SelectCommand = conn.CreateCommand(); 
      deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=1"; 
      deptDa.SelectCommand.CommandType = CommandType.Text; 
      deptDa.Fill(ds, "Departmenttbl"); 

      if (ds.Tables["Organizationtbl"].Rows.Count == 1) 
      { 
       foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows) 
       { 
        if (orgItem.IsNull("OrganizationName")) 
        { 
         foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows) 
         { 
          textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString(); 
         } 
        } 
        else 
        { 
         textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString(); 
        } 
       } 
      } 
      else 
      { 
       foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows) 
       { 
        if (deptItem.IsNull("DepartmentName")) 
        { 
         foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows) 
         { 
          textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString(); 
         } 
        } 
        else 
        { 
         textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString(); 
        } 
       } 
      } 

      orgDa.SelectCommand = conn.CreateCommand(); 
      orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=2"; 
      orgDa.SelectCommand.CommandType = CommandType.Text; 
      orgDa.Fill(ds, "Organizationtbl"); 

      deptDa.SelectCommand = conn.CreateCommand(); 
      deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=2"; 
      deptDa.SelectCommand.CommandType = CommandType.Text; 
      deptDa.Fill(ds, "Departmenttbl"); 

      if (ds.Tables["Organizationtbl"].Rows.Count == 1) 
      { 
       foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows) 
       { 
        if (orgItem.IsNull("OrganizationName")) 
        { 
         foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows) 
         { 
          textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString(); 
         } 
        } 
        else 
        { 
         textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString(); 
        } 
       } 
      } 
      else 
      { 
       foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows) 
       { 
        if (deptItem.IsNull("DepartmentName")) 
        { 
         foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows) 
         { 
          textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString(); 
         } 
        } 
        else 
        { 
         textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString(); 
        } 
       } 
      } 

我該怎麼讓從SQL 16個到16周的TextBlocks只在一個循環?我需要你的幫助。先謝謝你。

回答

2

一般來說,如果您有重複的代碼,儘量做到以下幾點:

  1. 就拿代碼一個項目,將其移動到一個功能。

  2. 查找每個項目不同的所有內容:如查詢,表名稱,列名稱,控件等。向每個方法的方法中添加一個參數,並將代碼中的每個匹配項替換爲參數。

  3. 調用該方法一次對每個項目,傳遞正確的參數。

這基本上是一個兩步重構:首先,提取方法。其次,提取參數。

然而,在你的情況,主要是ORGID和DEPTID似乎改變(和控制分配的值),所以瞭解參數化查詢,例如http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

你的提取方法可能看起來像這樣(沒有測試過,也沒有編譯):

void LoadStuff(int id, TextBox control){ 
      orgDa.SelectCommand = conn.CreateCommand(); 
      orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where [email protected]"; 
      orgDa.SelectCommand.CommandType = CommandType.Text; 
      var parameter = orgDa.SelectCommand.CreateParameter(); 
      parameter.ParameterName = "@orgId"; 
      parameter.Value = id; 
      orgDa.SelectCommand.AddParameter(parameter); 
      orgDa.Fill(ds, "Organizationtbl"); 

      deptDa.SelectCommand = conn.CreateCommand(); 
      deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where [email protected]"; 
      deptDa.SelectCommand.CommandType = CommandType.Text; 
      parameter = orgDa.SelectCommand.CreateParameter(); 
      parameter.ParameterName = "@deptID"; 
      parameter.Value = id; 
      orgDa.SelectCommand.AddParameter(parameter); 
      deptDa.Fill(ds, "Departmenttbl"); 

      if (ds.Tables["Organizationtbl"].Rows.Count == 1) 
      { 
       foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows) 
       { 
        if (orgItem.IsNull("OrganizationName")) 
        { 
         foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows) 
         { 
          control.Text = deptItem["DepartmentName"].ToString(); 
         } 
        } 
        else 
        { 
         control.Text = orgItem["OrganizationName"].ToString(); 
        } 
       } 
      } 
      else 
      { 
       foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows) 
       { 
        if (deptItem.IsNull("DepartmentName")) 
        { 
         foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows) 
         { 
          control.Text = orgItem["OrganizationName"].ToString(); 
         } 
        } 
        else 
        { 
         control.Text = deptItem["DepartmentName"].ToString(); 
        } 
       } 
      } 
    } 

然後用叫它:

LoadStuff(1, textblock_EventTitle0); 
LoadStuff(2, textblock_EventTitle1); 
LoadStuff(3, textblock_EventTitle2); 
.... 

或一些更聰明的循環。

當然,這可以進一步簡化。似乎在if-else構造的兩個分支中有很多相似之處。

+0

感謝您的幫助。你的反饋真的幫助了我很多。有一個幸運的日子:) – Sephiroth111 2012-03-03 09:43:18

相關問題