如何验证登录并传递用户 ID [英] How to validate login and pass userID
本文介绍了如何验证登录并传递用户 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
使用属性在两个表单之间传递数据
我正在使用以下代码来验证 winform 应用程序上的登录.
I am using the following code to validate a login on a winform application.
public int Validate_Login(String Username, String Password)
{
//SqlConnection con = new SqlConnection(@"User id=judgment_day;Password=Kit$hen;Server=216.40.232.82, 2153;Database=new_judgment-system");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["rawr"].ConnectionString);
SqlCommand cmdselect = new SqlCommand();
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.CommandText = "[dbo].[prcLoginv]";
cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = Username;
cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 50).Value = Password;
cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
cmdselect.Connection = con;
int Results = 0;
try
{
con.Open();
cmdselect.ExecuteNonQuery();
Results = (int)cmdselect.Parameters["@OutRes"].Value;
}
catch (SqlException ex)
{
lblMessage.Text = ex.Message;
}
finally
{
cmdselect.Dispose();
if (con != null)
{
con.Close();
}
}
return Results;
}
protected void btnlogin_Click(object sender, EventArgs e)
{
int Results = 0;
if (txtUsername.Text != null && txtPassword.Text != null)
{
Results = Validate_Login(txtUsername.Text, txtPassword.Text);
if (Results == 1)
{
lblMessage.Text = "Login is Good";
this.Hide();
frmSwitch frm = new frmSwitch();
frm.Show();
}
else
{
lblMessage.Text = "Invalid Login";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblMessage.Text = "Please make sure that the username and the password is Correct";
}
}
SP 如下:
Create Proc [dbo].[prcLoginv]
(
@Username VarChar(50),
@UPassword varChar(50),
@OutRes int OUTPUT
)
AS
set @OutRes = (SELECT count(*) FROM [dbo].Log_Users
WHERE Username = @Username And [Password] = @UPassword)
if(@OutRes = 1)
begin
set @OutRes = 1--Login is Correct
end
else
begin
set @OutRes = 0 --Bad login
end
我将如何修改它以返回用户 ID,以便我可以将其传递给另一个表单.我需要使用该 ID 来显示谁在处理什么文件.
How would I modify that to also return the userID so I can pass it to another form. I need to use that ID to show who is working on what file.
推荐答案
代替
set @OutRes = (SELECT count(*) FROM [dbo].Log_Users
WHERE Username = @Username And [Password] = @UPassword)
你可以试试
select @OutRes = userid from x where blah.
如果查询没有返回任何行,则返回 0,如果有多行,则返回最后一行.
If the query returns no rows you get 0, if there are multiple rows it gives you the last one.
这篇关于如何验证登录并传递用户 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文