-1

可能重复:
使用属性在两个表单之间传递数据

我正在使用以下代码来验证 winform 应用程序的登录。

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 来显示谁在处理什么文件。

4

1 回答 1

1

代替

set @OutRes = (SELECT count(*) FROM [dbo].Log_Users   
WHERE Username = @Username And [Password] = @UPassword)

你可以试试

select @OutRes = userid from x where blah.

如果查询没有返回任何行,则返回 0,如果有多行,则返回最后一行。

于 2012-11-20T22:50:45.617 回答