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

1

试试这个

SqlDataReader reader = com.ExecuteReader();

while (reader.Read())
{
  status = reader["Status"] == DBNull.Value ? 0 : (int)reader["Status"];
  IDCabang = reader["IDCabang"] == DBNull.Value ? null : (string)reader["IDCabang"];
}
于 2013-04-12T06:17:59.820 回答
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 回答
0

您正在使用 ExecuteScalar,它只会查看返回集的第一行和第一列:http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar .aspx

您真的想使用 ExecuteReader 或类似的东西。此外,我会在您的选择中为您的变量设置别名,以便您在查找它们时可以更轻松地引用它们(即,SELECT @status as Status, @IDCabang as IDCabang)。

SqlDataReader reader = com.ExecuteReader();

while (reader.Read())
{
  status = reader["Status"] == DBNull.Value ? 0 : (int)reader["Status"];
  IDCabang = reader["IDCabang"] == DBNull.Value ? null : (string)reader["IDCabang"];
}

编辑:参考 Aaron Bertrand 的评论,您还可以在存储的过程中设置一个输出参数并阅读它。这里有更多关于如何做到这一点的详细信息:Get output parameter value in ADO.NET

于 2013-04-11T17:13:48.073 回答