我试图从这个数据对象团队获取数据,然后将其传递给列表。当我运行程序而不是数据时,我得到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;
}
}
}
请帮助我如何从团队对象中获取数据.. !!我正在将通知列表发送到我的视图中。我获取除团队对象
中的数据之外的所有数据答案 0 :(得分:1)
ToString()
中的da.Tables[0].Rows.ToString();
赋值肯定会返回System.Data.DataRowCollection
,因为该方法返回DataTable.Rows
的类型而不是行值。检索行值的正确用法应如下所示:
// both 'rowIndex' and 'columnIndex' are int values (must be positive & start from 0)
da.Tables[0].Rows[rowIndex][columnIndex].ToString();
或使用列部分的列名称:
da.Tables[0].Rows[rowIndex]["columnName"].ToString();
使用来自for
的计数器的foreach
或ds.Tables[0].Rows
循环,分配行和列索引以从存储的行中提取值,如下例所示:
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
}