存儲過程

  存儲過程的寫法 以及 在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(); }
        } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章