原来的代码
public bool IsHasGroupRight(int id,int curid) { MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@id", id), new MySqlParameter("@curid", curid), }; var val = db.Database.ExecuteSqlRaw("CALL ishasgroupright(@id,@curid)", paras); return val > 0; }
返回结果一直是-1,我的存储过程是执行“select cout(0) ..........”得到的几个是1
后来找到原因ExecuteSqlRaw是执行SQL语句影响结果的行数,我这个是对数据库没更新没影响行数。
修改代码
private int ExecuteNoQuery(string sql, MySqlParameter[] sqlParams) { DbConnection connection = db.Database.GetDbConnection(); DbCommand cmd = connection.CreateCommand(); var result = 0; db.Database.OpenConnection(); cmd.CommandText = sql; cmd.CommandType = CommandType.Text; if (sqlParams != null) { cmd.Parameters.AddRange(sqlParams); } DbDataReader dr = cmd.ExecuteReader(); if(dr.Read()) { result = dr.GetInt32(0); } db.Database.CloseConnection(); return result; } public bool IsHasGroupRight(int id,int curid) { MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@id", id), new MySqlParameter("@curid", curid), }; var val= ExecuteNoQuery("CALL ishasgroupright(@id,@curid)", paras); //var val = db.Database.ExecuteSqlRaw("CALL ishasgroupright(@id,@curid)", paras); return val > 0; }