我正在尝试在 Visual Studio 中执行存储过程。它在下面给出。
CREATE PROCEDURE [dbo].[addStudent]
@stuName varchar(50),
@address varchar(100),
@tel varchar(15),
@etel varchar(15),
@nic varchar (10),
@dob date
AS
BEGIN
SET NOCOUNT ON;
DECLARE @currentID INT
DECLARE @existPerson INT
SET @existPerson = (SELECT p_ID FROM Student WHERE s_NIC = @nic);
IF @existPerson = null
BEGIN
INSERT INTO Person (p_Name, p_RegDate, p_Address, p_Tel, p_EmergeNo, p_Valid, p_Userlevel)
VALUES (@stuName, GETDATE(), @address, @tel, @etel, 0, 'Student' );
SET @currentID = (SELECT MAX( p_ID) FROM Person);
INSERT INTO Student (p_ID, s_Barcode, s_DOB, s_NIC) VALUES (@currentID , NULL, @dob, @nic);
return 0;
END
ELSE
return -1;
END
我通过使用下面的代码来做到这一点。
SqlConnection con = new SqlConnection();
Connect conn = new Connect();
con = conn.getConnected();
con.Open();
cmd = new SqlCommand("addStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@stuName", SqlDbType.VarChar).Value = nameTxt.Text.ToString();
cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = addressTxt.Text.ToString();
cmd.Parameters.Add("@tel", SqlDbType.VarChar).Value = telTxt.Text.ToString();
cmd.Parameters.Add("@etel", SqlDbType.VarChar).Value = emerTxt.Text.ToString();
cmd.Parameters.Add("@nic", SqlDbType.VarChar).Value = nicTxt.Text.ToString();
cmd.Parameters.Add("@dob", SqlDbType.DateTime).Value = dobTime.Value.ToString("MM-dd-yyyy");
int n = cmd.ExecuteNonQuery();
MessageBox.Show(n.ToString());
但它返回我-1。我通过输入从调试中捕获的相同值来尝试此存储过程。它是成功的。可能的错误是什么?非常感谢!