Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取MVC4 Resp API上的错误“IndexOutoforAngeException未处理”_C#_Api_Asp.net Mvc 4_Rest - Fatal编程技术网

C# 获取MVC4 Resp API上的错误“IndexOutoforAngeException未处理”

C# 获取MVC4 Resp API上的错误“IndexOutoforAngeException未处理”,c#,api,asp.net-mvc-4,rest,C#,Api,Asp.net Mvc 4,Rest,我是c语言新手,在MVC4上制作Restful API, 我正在使用以下代码,并在 " foreach (DataRow menuDR in menuDataSet.Tables [7].Rows)" IndexOutOfRangeException未经用户代码处理,在show中的详细信息中找不到表7 我对1表数据库使用了相同的方法 foreach (DataRow menuDR in menuDataSet.Tables [0].Rows) 它一直在工作,现在我在数据库中有8个表,我不知道

我是c语言新手,在MVC4上制作Restful API, 我正在使用以下代码,并在

" foreach (DataRow menuDR in menuDataSet.Tables [7].Rows)"
IndexOutOfRangeException未经用户代码处理,在show中的详细信息中找不到表7 我对1表数据库使用了相同的方法

foreach (DataRow menuDR in menuDataSet.Tables [0].Rows)
它一直在工作,现在我在数据库中有8个表,我不知道如何处理这个异常,并尝试了各种方法对它进行排序

public IEnumerable<RestaurantsMenu> GetAllMenu()
{
menuCon = new SqlConnection ("Data Source=MyPc\\MyPc;Initial Catalog=MENU;Integrated Security=True");
menuDataAdapter = new SqlDataAdapter (" SELECT * FROM RestaurantsMenu ", menuCon);
foreach (DataRow menuDR in menuDataSet.Tables [7].Rows)
{
Rst_Menu.Add(new RestaurantsMenu(){Restaurant_ID = int .Parse (menuDR[0].ToString()), RestaurantName =menuDR[1].ToString(),
Menu =menuDR[2].ToString(), Price=int .Parse (menuDR[3].ToString()) } );
}
return Rst_Menu;
}
问题:您正在从数据库中仅查询单个表索引0数据,并尝试访问索引为7的表,但该表不可用

即使您的数据库中有8个表,下面的查询也不会提供所有8个表的数据,这只会提供索引为零的表数据

SELECT * FROM RestaurantsMenu
解决方案:使用上述select语句,您应该始终引用表[0],因为您只得到一个表

试试这个:得到多张桌子

SqlDataAdapter menuDataAdapter = new SqlDataAdapter("SELECT * FROM RestaurantsMenu1;       "SELECT * FROM RestaurantsMenu2;"SELECT * FROM RestaurantsMenu3;", connection);
menuDataAdapter.TableMappings.Add("Table1", "RestaurantsMenu1");
menuDataAdapter.TableMappings.Add("Table2", "RestaurantsMenu2");
menuDataAdapter.TableMappings.Add("Table3", "RestaurantsMenu3");
for(int i=0;i<menuDataSet.Tables.Count;i++)
{
  foreach (DataRow menuDR in menuDataSet.Tables[i].Rows)
  {
    Rst_Menu.Add(new RestaurantsMenu(){Restaurant_ID = int .Parse (menuDR[0].ToString()), RestaurantName =menuDR[1].ToString(),
    Menu =menuDR[2].ToString(), Price=int .Parse (menuDR[3].ToString()) } );
  }
}