asp. net implementation calls stored procedure instances with output parameters
- 2021-07-13 05:03:15
- OfStack
This article example shows that the asp. net implementation calls stored procedures with output parameters. Share it for your reference, as follows:
Front Desk 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 file:
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> The user name already exists, please try another user name! </font>");
}
else
{
Response.Write("<font color=black> User name can be used! </font>");
}
}
Class 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 Class 1 must set the type and length of the output parameter or an error occurs
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)];// Add a return value
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 class
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();
}
}
I hope this paper is helpful to everyone's asp. net programming.