string role = ds.Tables[0].Rows[0][3].ToString();//得到用户权限字符串

string role = ds.Tables[0].Rows[0][3].ToString();//得到用户权限字符串
运行报错System.IndexOutOfRangeException:“在位置 0 处没有任何行。”
是table表你没数据,但怎么才能让table里有数据?

string role ="";
if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.count > 0)
{
role = ds.Tables[0].Rows[0][3].ToString();
}
最好的写法。

你这个问题不是回答过你吗,因为你数据库没有匹配到你查询的数据,所以,你这个datatable里没有返回数据,也就是空,空的话,ds.Tables[0].Rows[0][3].ToString(); 中.Rows[0][3].ToString(); 就会报错,因为找不到第一条信息,当然报错了。

想解决问题: 去数据库造一条符合你查询的数据。

或者可以在datatable里做假数据。假数据只是让你知道数据怎么获取。

写这一类的代码必须先判断rows的数量大于0才能用下标的方式调用tostring()方法