无法将类型“int”隐式转换为“System.Data.SqlClient.SqlDataReader
protected void Page_Load(object sender, EventArgs e)
{
if ((string)Session["userRole"] != "0")
Response.Redirect("Login.aspx");
string tn = (string)Session["userName"];
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from 课程 where 教师号='" + tn + "'";
conn.Open();
SqlDataReader dr = cmd.ExecuteNonQuery();//问题所在处1
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();
SqlCommand cmd1 = conn.CreateCommand();
cmd1.CommandText = "select 姓名 from 教师 where 教师号='" + tn + "'";
SqlDataReader dr1 = cmd1.ExecuteNonQuery();//问题所在处2
dr1.Read();
LabMes.Text = (string)dr1[0] + "老师的任课情况:";
Session["teacher"] = (string)dr1[0];
conn.Close();
}
cmd
cmd1
------解决方案--------------------SqlDataReader dr = cmd.ExecuteNonQuery();
=>
SqlDataReader dr = cmd.ExecuteReader();
------解决方案--------------------
正解
ExecuteNonQuery是对insert update delete的执行,你要查询返回结果集,用ExecuteReader
------解决方案--------------------把cmd.ExecuteNonQuery();改成cmd.ExecuteReader();