C # with input output parameter call stored procedure

zhaozj2021-02-16  89

The last article said how to control thread events in an instant messaging system This time this is how to call the stored procedure in C #. The principle of Dengfeng is to use short code to explain :)

1. The corresponding stored procedure in SQL Server

Create Procedure SP_TEST @ln varchar (10), @msg varchar (80) Output asserad @flag intDeclare @un varchar (80) set @ flag = 99

SELECT @ un = username from msg_userinfo where loginname = @ ln

SET @ msg = "this message from sp_test:" SELECT @msg = @ msg @ un return @flaggo

2.C # call the stored procedure

private void TestSP () {SqlCommand _cmd = new SqlCommand (); DbBase db = DbBase.GetInstance (); _cmd.Connection = db.conn; _cmd.CommandType = CommandType.StoredProcedure; _cmd.CommandText = "sp_test"; // SQL Server Procedure name // enter the login name SqlParameter un = new SqlParameter (); un.ParameterName = "@ ln"; un.DbType = DbType.String; un.Direction = ParameterDirection.Input; un.SourceVersion = DataRowVersion.Current; _cmd .Parameters.add (un); _cmd.parameters ["@ ln"]. Value = "zsb";

// RETURN_VALUE parameter SqlParameter _RETURN_VALUE = new SqlParameter (); _RETURN_VALUE.ParameterName = "@ RETURN_VALUE"; _RETURN_VALUE.DbType = DbType.Int32; _RETURN_VALUE.Direction = ParameterDirection.ReturnValue; _RETURN_VALUE.SourceVersion = DataRowVersion.Current; _cmd.Parameters.Add (_RETURN_VALUE);

// Return Message SqlParameter outpara = new SqlParameter (); outpara = new SqlParameter ( "@ msg", SqlDbType.VarChar, 100); outpara.Direction = ParameterDirection.Output; _cmd.Parameters.Add (outpara); _cmd.ExecuteNonQuery ( );

Console.writeline ((int) _cmd.parameters ["@ return_value"]. Value); console.writeline (_cmd.parameters ["@ msg"]. Value.toString ());

// Username Parameter

}

转载请注明原文地址:https://www.9cbs.com/read-12902.html

New Post(0)