Dapper additions, deletions, modifications and inspections of Dapper stored procedures, transactions, etc.

Continued from the previous article " Dapper Additions, Deletes, Modifications and Checks "

 

0. Stored procedure

create  proc p_login
 @Name  nvarchar ( 16 ),
 @Password  nvarchar ( 128 ),
 @result  nvarchar ( 128 ) output
 as 
begin 
if ( exists ( select  *  from Users where Name = @Name  and Password = @Password ))
  set  @result = ' Login successful ' 
else 
 set  @result = ' Username or password is incorrect '
end

1. Execute the stored procedure

IDbConnection conn = new SqlConnection(connStr);
var pars = new DynamicParameters();
pars.Add("@Name", "bj");
pars.Add("@Password", "123456");
pars.Add("@result", "", DbType.String, ParameterDirection.Output);
conn.Execute("p_login", pars, commandType: CommandType.StoredProcedure);
string result = pars.Get<string>("@result");

2. Transaction Operations

IDbConnection conn = new SqlConnection(connStr);
conn.Open(); // manually open 
IDbTransaction tran = conn.BeginTransaction();
 int result = conn.Execute( " delete from users " , transaction: tran); // assign transaction to command 
if (result > 0 )
    tran.Rollback();
else
    tran.Commit();

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324634946&siteId=291194637