在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
说明:很多时候,在DBHelper函数中,都能看到以下的代码: cmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); 其起的作用是:当调用的存储过程中如果有Return @xxx 就会把这个值返回给这个ReturnValue 参数。这样你在代码里就可以调用这个值了。 例子: 数据库: 操作的表:tb_user 在数据库中添加一个一个存储过程: create proc Test @id int,@name varchar(20),@pwd varchar(20) as begin declare @count int insert into tb_login(id,name,pwd) values(@id,@name,@pwd) select @count = (select count(*) from tb_login) return @count end go
后台代码: 数据库操作 【Class】DataBase: 数据库库连接自己写了 /// <summary> /// 执行命令 /// </summary> /// <param name="procName">命令文本</param> /// <param name="prams">参数对象</param> /// <returns></returns> public int RunProc(string procName, SqlParameter[] prams) { SqlCommand cmd = CreateCommand(procName, prams); cmd.ExecuteNonQuery(); this.Close(); //得到成功执行的返回值 return (int)cmd.Parameters["ReturnValue"].Value; } /// <summary> /// 创建一个SqlCommand对象以此来执行命令文本 /// </summary> /// <param name="procName">命令文本</param> /// <param name="prams"命令文本所需参数</param> /// <returns>返回SqlCommand对象</returns> private SqlCommand CreateCommand(string procName, SqlParameter[] prams) { this.Open(); SqlCommand cmd = new SqlCommand(procName, conn); //cmd.CommandType = CommandType.Text;// 执行类型:命令文本 cmd.CommandType = CommandType.StoredProcedure; //执行类型:存储过程 //一次把参数传入命令文本 if (prams != null) { foreach (SqlParameter param in prams) { cmd.Parameters.Add(param); } } //加入返回参数 cmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return cmd; }
实体类: Class User public class User { private int id; private string name; private string pwd; public string Pwd { get { return pwd; } set { pwd = value; } } public string Name { get { return name; } set { name = value; } } public int Id { get { return id; } set { id = value; } } }
测试数据库类: Class TestDataBase public class TestDataBase { DataBase db = new DataBase(); public int AddUser(User newUser) { SqlParameter[] prams = { db.MakeInParam("@id",SqlDbType.Int,100,newUser.Id), db.MakeInParam("@name",SqlDbType.VarChar,20,newUser.Name), db.MakeInParam("@pwd",SqlDbType.VarChar,20,newUser.Pwd) }; return db.RunProc("Test", prams); } } 前台: Test.aspx: <body> <form id="form1" runat="server"> <div> <asp:Label ID="label1" runat="server"></asp:Label> </div> </form> </body>
Test.aspx.cs: public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { User newUser = new User(); newUser.Id = 3; newUser.Name = "kkK"; newUser.Pwd = "kkk"; TestDataBase tdb = new TestDataBase(); int num = tdb.AddUser(newUser); label1.Text = num.ToString(); } } }
执行前,查看数据库: 执行后: 返回结果:
其他的例子: 摘自:http://bbs.csdn.net/topics/330002062 在更新数据表数据库时,可以通过返回值,确定更新是否成功。 通常的用法: c# code: SqlConnection conn = CreateSqlConnection(); SqlCommand cmd = new SqlCommand("sp_InsertInstitute", conn); cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] parms = new SqlParameter[3]{ new SqlParameter("@InstituteNO","2"), new SqlParameter("@InstituteName","计算机学院"), new SqlParameter("@return",SqlDbType.Int) }; foreach (SqlParameter parm in parms) { cmd.Parameters.Add(parm); } cmd.Parameters["@return"].Direction = ParameterDirection.ReturnValue; try { cmd.ExecuteScalar(); } catch (SqlException ex) { Console.WriteLine("操作失败!" + ex.Message); return; } finally { conn.Close(); } string strTmp = cmd.Parameters["@return"].Value.ToString(); switch (strTmp) { case "0": Console.WriteLine("添加成功"); break; case "1": Console.WriteLine("数据有重复"); break; case "2": Console.WriteLine("数据操作失败"); break; case "3": Console.WriteLine("插入数据为空"); break; } Console.ReadKey();
sql code: CREATE PROCEDURE [dbo].[sp_InsertHotspot] @hotTime int, @hotContent xml AS BEGIN TRANSACTION INSERT INTO t_Hotspot([iHotTime],[tHotContent] ) VALUES(@hotTime ,@hotContent ) IF @@ERROR <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1--返回表示操作失败 END ELSE BEGIN COMMIT TRANSACTION RETURN 0--返回表示操作成功 END
|
请发表评论