本文實例講述了asp.net實現調用帶有輸出參數的存儲過程。分享給大家供大家參考,具體如下:
前台jqurey
<script type="text/javascript"> $(document).ready(function(){ $('#change_image').click(function(){ $('#imgAuthenCode').attr('src','CheckCode.aspx?'+Math.random());}); $("#accounts").bind("blur",function(){ $.ajax({ url:"checkusername.aspx", type:"post", datatype:"html", data:{user_name:$("#accounts").val()}, success:function(msg){$("#tip_accounts").html(msg);} });}); }); </script>
aspx文件:
protected void Page_Load(object sender, EventArgs e) { Entity.User us = new Entity.User(); us.User_name = Request.Params["user_name"].ToString(); if (us.User_CheckName()) { Response.Write("<font color=red>用戶名已經存在,請嘗試其它用戶名!</font>"); } else { Response.Write("<font color=black>用戶名可以使用!</font>"); } }
user類
public bool User_CheckName() { try { string[,] sArray = new string[2, 2]; sArray[0, 0] = "@user_name"; sArray[1, 0] = "@r_id"; sArray[0, 1] = User_name; sArray[1, 1] = null; Factory.SqlModel sm = new Factory.SqlModel(); Id = sm.Proc_Return_R_ID("User_CheckName", sArray); if (Id > 0) { return true; } else { return false; } } catch (Exception e) { Log lg = new Log(); lg.ExceptionError(e); return false; } }
sqlmodel 類 一定要設置輸出參數的類型 及長度 否則出現 錯誤
String[1]: the Size property has an invalid size of 0.
public int Proc_Return_R_ID(string proc_name, string[,] sArray) { try { if (sArray.GetLength(0) >= 1) { DataBase db = new DataBase(); SqlParameter[] sqlpar = new SqlParameter[sArray.GetLength(0)];//加入返回值 for (int i = 0; i < sArray.GetLength(0); i++) { sqlpar[i] = new SqlParameter(sArray[i, 0], sArray[i, 1]); } sqlpar[sArray.GetLength(0) - 1].Direction = ParameterDirection.Output; sqlpar[sArray.GetLength(0) - 1].SqlDbType = SqlDbType.Int; return db.Proc_Return_R_ID(proc_name, sqlpar); } else { return 0; } } catch { return 0; } }
DATABASE.cs 類
public int Proc_Return_R_ID(string proc_name, params SqlParameter[] cmdParms) { try { OpenConnection(); if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } BaseSqlCommand.Parameters.Add(parameter); } BaseSqlCommand.CommandType = CommandType.StoredProcedure; BaseSqlCommand.CommandText = proc_name; BaseSqlCommand.ExecuteNonQuery(); return (int)BaseSqlCommand.Parameters["@r_id"].Value; } else { return 0; } } catch { return 0; } finally { BaseSqlCommand.Parameters.Clear(); CloseConnection(); } }
希望本文所述對大家asp.net程序設計有所幫助。