Stored Procedure CreatePROCEDUREMYSQL @ aint, @ bint, @ cintoutputASSet @ c @ a @ bGOSqlConnectionconnnewSqlConnection (ConfigurationManager. ConnectionStrings [LocalSqlServer]. ToString (); conn. Open (); SqlCommandMyComm
// Stored PROCEDURE // Create procedure mysql // @ a int, // @ B int, // @ c int output // AS // Set @ c = @ a @ B // GO SqlConnection conn = new SqlConnection (ConfigurationManager. connectionStrings [LocalSqlServer]. toString (); conn. open (); SqlCommand MyComm
// Stored Procedure
// Create PROCEDURE MYSQL
// @ A int,
// @ B int,
// @ C int output
//
// Set @ c = @ a + @ B
// GO
SqlConnection conn = new SqlConnection (ConfigurationManager. ConnectionStrings ["LocalSqlServer"]. ToString ());
Conn. Open ();
SqlCommand MyCommand = new SqlCommand ("MYSQL", conn );
MyCommand. CommandType = CommandType. StoredProcedure;
MyCommand. Parameters. Add (new SqlParameter ("@ a", SqlDbType. Int ));
MyCommand. Parameters ["@ a"]. Value = 20;
MyCommand. Parameters. Add (new SqlParameter ("@ B", SqlDbType. Int ));
MyCommand. Parameters ["@ B"]. Value = 20;
MyCommand. Parameters. Add (new SqlParameter ("@ c", SqlDbType. Int ));
MyCommand. Parameters ["@ c"]. Direction = ParameterDirection. Output;
MyCommand. ExecuteNonQuery ();
Response. Write (MyCommand. Parameters ["@ c"]. Value. ToString ());
C # receive Stored Procedure return values:
Public static int User_Add (User us)
{
Int iRet;
SqlConnection conn = new SqlConnection (Conn_Str );
SqlCommand cmd = new SqlCommand ("User_Add", conn );
Cmd. CommandType = CommandType. StoredProcedure;
Cmd. Parameters. AddWithValue ("@ UName", us. UName );
Cmd. Parameters. AddWithValue ("@ UPass", us. UPass );
Cmd. Parameters. AddWithValue ("@ PassQuestion", us. PassQuestion );
Cmd. Parameters. AddWithValue ("@ PassKey", us. PassKey );
Cmd. Parameters. AddWithValue ("@ Email", us. Email );
Cmd. Parameters. AddWithValue ("@ RName", us. RName );
Cmd. Parameters. AddWithValue ("@ Area", us. Area );
Cmd. Parameters. AddWithValue ("@ Address", us. Address );
Cmd. Parameters. AddWithValue ("@ ZipCodes", us. ZipCodes );
Cmd. Parameters. AddWithValue ("@ Phone", us. Phone );
Cmd. Parameters. AddWithValue ("@ QQ", us. QQ );
Cmd. Parameters. Add ("@ RETURN_VALUE", ""). Direction = ParameterDirection. ReturnValue;
Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
IRet = (int) cmd. Parameters ["@ RETURN_VALUE"]. Value;
}
Catch (SqlException ex)
{
Throw ex;
}
Finally
{
Conn. Close ();
}
Return iRet;
}
C # receiving Stored Procedure output parameters:
Public static decimal Cart_UserAmount (int UID)
{
Decimal iRet;
SqlConnection conn = new SqlConnection (Conn_Str );
SqlCommand cmd = new SqlCommand ("Cart_UserAmount", conn );
Cmd. CommandType = CommandType. StoredProcedure;
Cmd. Parameters. AddWithValue ("@ UID", UID );
Cmd. Parameters. Add ("@ Amount", SqlDbType. Decimal). Direction = ParameterDirection. Output;
Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
IRet = (decimal) cmd. Parameters ["@ Amount"]. Value;
}
Catch (SqlException ex)
{
Throw ex;
}
Finally
{
Conn. Close ();
}
Return iRet;
}