存儲過程的寫法 以及 在C#中調用存儲過程。
寫法:
CREATE PROCEDURE dbo.StoredProcedure4
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
/* SET NOCOUNT ON */ -------sql語句
RETURN
C#中的調用方法:
SqlCommand borrowbook = new SqlCommand();
borrowbook.Connection = this.sqlConnection1;
borrowbook.CommandType = CommandType.StoredProcedure;
borrowbook.CommandText = "dbo.StoredProcedureBorrowedNumber";
SqlParameter parinput = borrowbook.Parameters.Add("@ReaderID", SqlDbType.Char);
parinput.Direction = ParameterDirection.Input;
parinput.Value = ReaderID;
SqlParameter paroutput = borrowbook.Parameters.Add("@BorrowedNumber", SqlDbType.Int);
paroutput.Direction = ParameterDirection.Output;
try
{
this.sqlConnection1.Open();
borrowbook.ExecuteNonQuery();
return Convert.ToInt16(paroutput.Value);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return 3;
}
finally { this.sqlConnection1.Close(); }
}