-1

重複の可能性:
プロパティを使用して2つのフォーム間でデータを渡す

次のコードを使用して、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 

別のフォームに渡すことができるように、userIDも返すように変更するにはどうすればよいですか。その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 に答える