我有一个这样的 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 没有返回任何值。