如何在Web方法中返回Userid并在网站ASP.NET中使用返回的结果,使其成为会话变量以便在ASP.NET中的不同网页上使用它?
[的WebMethod] public bool UserLogin(string EmailAdd,string Password) {
SqlConnection myConnection = new SqlConnection(@"user id=BankUser;password=Computer1;server=(local)\sql2008;database=BillionBank;");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM [BillionBank].[dbo].[tblCustomerProfile] WHERE EmailAdd='" + EmailAdd + "' AND Password ='" + Password + "'", myConnection);
SqlDataReader myreader;
myreader = myCommand.ExecuteReader();
if (myreader.Read())
{
return true;
}
else
return false;
}
这是我的代码我希望返回一个Userid作为结果并在网站中将其用作会话变量,当我在我的网站中调用web方法时
答案 0 :(得分:1)
在web方法中设置会话变量,如:
Session["userId"] = 1234;
然后在您的应用程序中,您可以再次调用会话。请注意,您需要再次将会话解析为int
int userId
if(int.TryParse(Session["userId"].toString(), out userId)
{
//do something
}
答案 1 :(得分:1)
在网络方法上保留[WebMethod(EnableSession = true)]
,然后您可以返回会话变量。
答案 2 :(得分:0)
public string GetUserID(string EmailAdd, string Password)
{
SqlConnection myConnection = new SqlConnection(@"user id=BankUser;password=Computer1;server=(local)\sql2008;database=BillionBank;");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM [BillionBank].[dbo].[tblCustomerProfile] WHERE EmailAdd='" + EmailAdd + "' AND Password ='" + Password + "'", myConnection);
SqlDataReader myreader;
string result = "";
myreader = myCommand.ExecuteReader();
if (myreader.Read())
{
result = Convert.ToString(myreader["UserID"]);
}
return result;
}
protected void btnLogin_Click(object sender, EventArgs e)
{
Session["UserID"] = GetUserID(YOUR_EMAILID, YOUR_PASSWORD);
}