问个让我头疼的简单问题,望高手们帮下忙
我在form1中添加一个DataGridView,单击button1后能显示类user中定义的方法selectuser返回的datareader的值,不知道button1的代码如何写,请高手们详细教下,谢谢。
public SqlDataReader selectuser(string userid)
{
SqlConnection myconn = new SqlConnection(mystring);
string cmdtext = "select username from users where userid= " + " ' " + userid.ToString() + " ' ";
SqlCommand mycommand = new SqlCommand(cmdtext, myconn);
SqlDataReader dr = null;
try
{
myconn.Open();
dr = mycommand.ExecuteReader();
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
myconn.Close();
}
return dr;
}
------解决方案--------------------myconn.Open();
dr = mycommand.ExecuteReader();
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
myconn.Close();
关了连接,DR就不能访问了,返回的是损坏的对象