2017-10-27 27 views
1

私はこのデータオブジェクトチームからデータを取得しようとしていましたが、リストに渡しています。データの代わりにプログラムを実行しているときに、System.Data.DataRowCollectionを取得しています。私がここでやっている間違ったことは何かを教えてください?データセットからデータを取得してリストに渡す方法は?

public ActionResult notifications() 
    { 
     List<notifications> Teamwon = Getsports(); 
     return View(Teamwon); 
    } 
    public List<notifications> Getsports() 
    { 
     string lname; 
     string lvenue; 
     string ldate; 
     string lteam1; 
     string lteam2; 
     string leaname = "0"; 
     string leavenue = "0"; 
     string leadate = "0"; 
     string wteam = "0"; 

     SqlConnection sqlConnection1 = new SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=Remember;server=DESKTOP-59SGH72\\SQLEXPRESS;Database=sport;Trusted_Connection=True"); 
     SqlCommand cmd = new SqlCommand(); 
     SqlDataAdapter usersport = new SqlDataAdapter("select sports_details_id from users_sports_following where user_id ='" + Session["userid"] + "'", sqlConnection1); 
     DataSet sportds = new DataSet(); 
     usersport.Fill(sportds); 
     int count = sportds.Tables[0].Rows.Count; 
     List<string[]> leaguelist = new List<string[]>(); 
     List<string[]> teamlist = new List<string[]>(); 
     while (count != 0) 
     { 
      Int16 id = Convert.ToInt16(sportds.Tables[0].Rows[count-1]["sports_details_id"].ToString()); 

      using (SqlDataAdapter leagueDetails = new SqlDataAdapter("select league_name, league_details_venue,league_details_date,sports_details_id1,sports_details_id2 from leagues, league_details where sports_details_id1 in ('" + id + "') or sports_details_id2 in ('" + id + "')", sqlConnection1)) 
      { 
       DataSet leagues = new DataSet(); 
       leagueDetails.Fill(leagues).ToString(); 

       lname = leagues.Tables[0].Rows[0]["league_name"].ToString(); 
       lvenue = leagues.Tables[0].Rows[0]["league_details_venue"].ToString(); 
       ldate = leagues.Tables[0].Rows[0]["league_details_date"].ToString(); 
       lteam1 = leagues.Tables[0].Rows[0]["sports_details_id1"].ToString(); 
       lteam2 = leagues.Tables[0].Rows[0]["sports_details_id2"].ToString(); 

       string[] fields = new string[5]; 
       fields[0] = lname; 
       fields[1] = lvenue; 
       fields[2] = ldate; 
       fields[3] = lteam1; 
       fields[4] = lteam2; 
       leaguelist.Add(fields); 
      } 
      count = count - 1; 
     }   
     List<notifications> notificationlist = new List<notifications>(); 
     foreach (string[] fields in leaguelist) 
     { 
      leaname = fields[0]; 
      leavenue = fields[1]; 
      leadate = fields[2]; 
      var id1 = fields[3]; 
      var id2 = fields[4]; 

      SqlConnection sqlConnection2 = new SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=Remember;server=DESKTOP-59SGH72\\SQLEXPRESS;Trusted_Connection=True;Database=sport"); 
      SqlDataAdapter ds = new SqlDataAdapter("team", sqlConnection2); 
      ////mention the adaptor that it needs to ru stored procedure 
      ds.SelectCommand.CommandType = CommandType.StoredProcedure; 
      ds.SelectCommand.Parameters.Add("@sports_details_id1", id1); 
      ds.SelectCommand.Parameters.Add("@sports_details_id2", id2); 

      //fill the dataset 
      DataSet da = new DataSet(); 
      ds.Fill(da).ToString(); 
      string team = da.Tables[0].Rows.ToString(); 
      notificationlist.Add(new notifications 
      { 
       leaguename = leaname, 
       leaguevenue = leavenue, 
       date = leadate, 
       teamwon = team 
      }); 
     } 
     return notificationlist; 
    } 
} 

}

チームのオブジェクトからデータを取得する方法で私を助けてください..!私は私のビューに通知リストを送信しています。私はこの方法が行の値の代わりにDataTable.Rowsの種類を返しますので、

+0

'da.Tables [0] .Rows.ToString()' =>確かに 'ToString'は' System行内容の代わりに.Data.DataRowCollection'型を使用します。あなたはテーブルの行で何をしたいですか? –

+0

@ 'Testsuya YAmamoto私はちょうどそのテーブルにいくつかの名前があります、私はチームオブジェクトとのリストにそれらを入れようとしています。 – 2strange

+0

@ 'Testsuya YAmamoto私は、行のデータが文字列に変換されると思ったので、ToString()を使用しました。私が間違っている場合は私を修正してください。 – 2strange

答えて

1

da.Tables[0].Rows.ToString();このToString()割り当ては確かSystem.Data.DataRowCollectionを返すチームのオブジェクトからデータを除くすべてのデータを取得しています。 、ds.Tables[0].Rowsからカウンタとfor又はforeachループを使用して

da.Tables[0].Rows[rowIndex]["columnName"].ToString(); 

いずれ行と列の両方を割り当てる:

// both 'rowIndex' and 'columnIndex' are int values (must be positive & start from 0) 
da.Tables[0].Rows[rowIndex][columnIndex].ToString(); 

または列の一部の列名を使用して:行値を取得するための適切な使用法は次のようでなければなりませんこの例のように格納された行から値を引き出すためのインデックス:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
{ 
    // other stuff 

    // assumed you want to pull "teamwon" column 
    string team = da.Tables[0].Rows[i]["teamwon"].ToString(); 

    // add values to the list 
    notificationlist.Add(new Notification { leaguename = leaname, leaguevenue = leavenue, date = leadate, teamwon = team }); 

    // other stuff 
} 
+0

私の見解コード<テーブルクラス=「テーブル」> @ Html.DisplayNameFor(モデル=> model.leaguename) を、それを見て、私は何をすべきか教えてください@ Html.DisplayNameFor(モデル=> model.teamwon)@ Html.DisplayNameFor(モデル=> model.date) @ Html.DisplayNameFor(モデル=> model.leaguevenue) @foreach(モデルにおけるVAR項目) { ​​@ Html.DisplayFor(modelitem => item.leaguename) ​​@ Html.DisplayFor(modelitem => item.leaguevenue) ​​@html .DisplayFor(modelitem => item.date) ​​@ Html.DisplayFor(modelitem => item.teamwon) } – 2strange

関連する問題