0

このようなSQL作成プロシージャクエリがあります

    USE KOST
    GO
    --Cabang means Branch in Bahasa.

CREATE PROCEDURE login_pro
        --received parameter
        @NoIDStaff VARCHAR(5),
        @Password VARCHAR (50)


    AS

    --sent parameter
    DECLARE @status INT
    DECLARE @IDCabang VARCHAR(5)

    --owner login
    IF EXISTS(SELECT IDCabang, [Password]
                FROM MsStaff 
                WHERE @NoIDStaff=NoIDStaff AND @Password=[Password]
                AND NoIDStaff LIKE('B%'))
            SET @status=1

    --staff login
    ELSE IF EXISTS(SELECT IDCabang, [Password]
                FROM MsStaff 
                WHERE @NoIDStaff=NoIDStaff AND @Password=[Password]
                AND NoIDStaff LIKE('A%'))
            BEGIN
            SELECT @IDCabang=IDCabang FROM MsStaff
            SET @status=2
            SET @IDCabang = (select IDCabang FROM Msstaff where @NoIDStaff=NoIDStaff AND @Password=[Password]
                AND NoIDStaff LIKE('A%'))
            END

    --neither of them login
    ELSE
            set @status=0

    select @status, @IDCabang
    GO

クエリはステータスの値を返しますが、IDCabang は返しません。IDCabang の値が返されるようにするにはどうすればよいですか?

これは私のC#コードです:

 private void submit_Click(object sender, EventArgs e)
    {
        NoIDStaff = new SqlParameter();
        Password = new SqlParameter();
        SqlConnection con = new SqlConnection(strCon);

        com = new SqlCommand();
        com.Connection = con;
        con.Open();

        com.CommandType = CommandType.StoredProcedure;
        com.CommandText = "login_pro";

        NoIDStaff.SqlDbType = SqlDbType.VarChar;
        NoIDStaff.Size = 50;
        NoIDStaff.ParameterName = "@NoIDStaff";
        NoIDStaff.Value = username.Text.ToString();
        NoIDStaff.Direction = ParameterDirection.Input;

        Password.SqlDbType = SqlDbType.VarChar;
        Password.Size = 50;
        Password.ParameterName = "@Password";
        Password.Value = password.Text.ToString();
        Password.Direction = ParameterDirection.Input;

        com.Parameters.Add(NoIDStaff);
        com.Parameters.Add(Password);



        int status;
        string IDCabang;
        status = Convert.ToInt16(com.ExecuteScalar());
        IDCabang = Convert.ToString(com.ExecuteScalar());


        //owner login
        if (status == 1)
        {
            this.Hide();
            frm = new Form2();
            frm.Show();
        }

        //staff login
        else if (status == 2)
        {
            this.Hide();
            frm4 = new Form4(IDCabang);
            frm4.Show();
        }


        else if (username.Text.Equals(""))
        {
            MessageBox.Show("Username Must be Filled!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        else if (password.Text.Equals(""))
        {
            MessageBox.Show("Password Must be Filled!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        else
        {
            MessageBox.Show("Invalid Username or Password", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        con.Close();
    }

IDCabangを文字列で受け取りたいので、Form4に送信できます。しかし、IDCabang は値を返さないようです。

4

3 に答える 3

0

このように試しましたか:

    USE KOST
    GO
    --Cabang means Branch in Bahasa.

CREATE PROCEDURE login_pro(@IDCabang VARCHAR(5))
        --received parameter
        @NoIDStaff VARCHAR(5),
        @Password VARCHAR (50)


    AS

    --sent parameter
    DECLARE @status INT


    --owner login
    IF EXISTS(SELECT IDCabang, [Password]
                FROM MsStaff 
                WHERE @NoIDStaff=NoIDStaff AND @Password=[Password]
                AND NoIDStaff LIKE('B%'))
            SET @status=1

    --staff login
    ELSE IF EXISTS(SELECT IDCabang, [Password]
                FROM MsStaff 
                WHERE @NoIDStaff=NoIDStaff AND @Password=[Password]
                AND NoIDStaff LIKE('A%'))
            BEGIN
            SELECT @IDCabang=IDCabang FROM MsStaff
            SET @status=2
            SET @IDCabang = (select IDCabang FROM Msstaff where @NoIDStaff=NoIDStaff AND @Password=[Password]
                AND NoIDStaff LIKE('A%'))
            END

    --neither of them login
    ELSE
            set @status=0

    select @status, @IDCabang
    GO

こちらもご覧ください

于 2013-04-11T16:31:08.627 に答える