如何在WHERE子句FOR SESSION中编写sql查询 [英] how to write sql query in WHERE clause FOR SESSION
问题描述
嘿所有,
我只是想知道是否有办法设置我的查询的WHERE子句作为会话变量?到目前为止,我已经有了以下内容,并且已经尝试过大量的教程,但它似乎没有用。这甚至可能吗?我知道以下是错误的,但有人可以帮我改变下面的代码,或指向我一个好的教程吗?
SqlCommand cmd = new SqlCommand( 从Userinfo中选择不同的分支,其中User_name = Session [userName]。ToString(),con);
请帮助...
Hey all,
I was just wonering if there is a way to set the WHERE clause of my query as the session variable? I''ve got the following so far, and have tried to go through a ton of tutorials, but it just does not seem to work. Is this even possible? I know the following is wrong, but can someone help me out with either changing the following code, or pointing me to a good tutorial?
SqlCommand cmd = new SqlCommand("Select distinct Branch from Userinfo where User_name = Session["userName"].ToString()",con);
Please help...
推荐答案
SqlCommand cmd = new SqlCommand(从Userinfo选择不同的分支,其中User_name =''+ Session [userName]。ToString()+'',con);
复制粘贴,你就完成了......
SqlCommand cmd = new SqlCommand("Select distinct Branch from Userinfo where User_name = ''"+ Session["userName"].ToString() +"''",con);
Copy paste this and you are done...
你必须在page_Load事件中设置Selectcommand,如: -
You have to set Selectcommand in page_Load event like :-
protected void Page_Load(object sender, EventArgs e)
{
// if value for seesion variable is already set
if (Session["userid"] != null)
{
string key = Session["userid"].ToString();
SqlDataSource1.SelectCommand = "SELECT fname,lastname,number,userid from users where userid= '" + key + "'";
YourGridView.DataSource= SqlDataSource1;
YourGridView.DataBind();
}
}
这篇关于如何在WHERE子句FOR SESSION中编写sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!