如何在SQL查询中使用会话值? [英] How to use session value in SQL query?

查看:79
本文介绍了如何在SQL查询中使用会话值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在母版页中有一个母版页有一个登录和注销会话,这个会话名为session [user]。



i想要使用session [用户查询标签信息打印。



我尝试过:



i have a master page in master page have a login and logout session this session named is session["user"] .

i want to use session["user"] in query for a label message print.

What I have tried:

SqlCommand cmd = new SqlCommand("select id_users from users where usermail=@user ", con);
        cmd.Parameters.AddWithValue("@user",Session["user"]);

推荐答案

试试这个



try this

int userID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);


如果您在会话变量中存储用户信息,例如Session [User] =UserName,则登录到应用程序后,您可以通过页面访问此会话变量。

因此,如果要在页面中打印用户信息,则可以使用此语法。



lblID.text = Convert.Tostring(Session [User]);



如果你想要按照你的例子传递给sql server。



SqlCommand cmd = new SqlCommand(select user_users from users user user = @ user,con);

cmd.Parameters.AddWithValue(@ user,Convert.Tostring(Session [User]));
After logging to the application if you store user info in the session variable like Session["User"] = "UserName" then you can access this session variable through out the page.
So if you want to print in user information in the page then you can use this syntax.

lblID.text= Convert.Tostring(Session["User"]);

If You want to pass to the sql server as per your example.

SqlCommand cmd = new SqlCommand("select id_users from users where usermail=@user ", con);
cmd.Parameters.AddWithValue("@user",Convert.Tostring(Session["User"]));


这篇关于如何在SQL查询中使用会话值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆