2012-03-03 15 views
0

こんにちは、良い日。私はコーディングに関する問題があります。SQL Serverから16個のテキストブロックまで16個の項目を表示するだけの長いコードがありますが、エラーはありませんが、短いコード行を維持したいのです。ここでは例のコード(16のうち)2つのテキストブロックです:16のテキストブロックにSQL Serverデータを表示

  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(); 
        } 
       } 
      } 
私はただ一つのループで16のテキストブロックへのSQLから16個の項目を表示するにはどうすればよい

?あなたの助けが必要です。前もって感謝します。

答えて

2

一般的に、あなたが繰り返されているコードを持っている場合は、次の操作を実行しよう:

  1. は、1つの項目のコードを取る関数に移動します。

  2. クエリ、テーブル名、列名、コントロールなどのアイテムごとに異なるものをすべて見つけます。それぞれのメソッドにパラメータを追加し、コード内の各オカレンスをパラメータに置き換えます。

  3. メソッドを各項目に対して1回呼び出し、正しいパラメータを渡します。

基本的に2段階のリファクタリングです。まず、抽出方法。次に、パラメータを抽出します。

しかし、あなたのケースでは、主に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構造の2つの枝には多くの類似点があるようです。

+0

ありがとうございました。あなたのフィードバックは本当に私を大いに助けました。恵まれた一日を :) – Sephiroth111

関連する問題