如何读取与当前session相对应的用户名?

来源:百度知道 编辑:UC知道 时间:2024/05/31 15:10:09
我想在Gridview中显示用户名
请问,怎样根据当前session["userid"],从数据库中读取相应的用户名?
最好是有详细代码!
我的错误代码
string strID = Session["userid"].ToString();
string username ="select username from users where id='"+strID+"'";
SqlConnection con1=new SqlConnection(ConfigurationManager.AppSettings["SqlCon"]);
con1.Open();
SqlCommand com1=new SqlCommand(username,con1);
string strusername = com1.ExecuteReader();
con1.Close();

string username ="select username from users where id=此处该怎么写啊

string strID = Session["userid"].ToString();
string username ="select username from users where id='"+strID+"'";
SqlConnection con1=new SqlConnection(ConfigurationManager.AppSettings["SqlCon"]);
con1.Open();
SqlCommand com1=new SqlCommand(username,con1);
string strusername = com1.ExecuteScalar();
con1.Close();
最好还是登录成功的时候把用户名也存到session里面哈!

1、写一个SQL语句,select 用户名(username) from (userinfo)用户表 where userid='***'
2、在session初始化的时候直接读出相应的用户名。
然后再读session["username"]就行了。

string strusername = com1.ExecuteReader();
这句的问题了
右面返回的不是string类型哦
大概应该改成这个样子:
SqlDataReader dr = com1.ExecuteReader();
if(dr.read())
strusername = dr.GetString(0);