在asp.net中使用SQL Server存储过程输出c#

msh*_*yam 4 c# sql-server asp.net ado.net stored-procedures

我刚开始使用存储过程.

我们有一个现有系统,它使用检查用户名和URL路径的存储过程.存储过程将检查用户详细信息是否存在.如果存在,则返回1,否则返回0.

我试图通过提供用户详细信息和路径来编写asp.net代码来调用此存储过程,然后在asp.net中使用返回的0或1值.

Hat*_*oft 8

看起来你需要带输出参数的存储过程

int errorId = 0;

using(SqlConnection sqlConnection = new SqlConnection(connectionString))
{
    using(SqlCommand cmd = new SqlCommand("YourStoredProcedureName", sqlConnection))
    {
    cmd.CommandType=CommandType.StoredProcedure;
    SqlParameter parm=new SqlParameter("@username", SqlDbType.VarChar); 
    parm.Value="mshiyam";
    parm.Direction =ParameterDirection.Input ; 
    cmd.Parameters.Add(parm); 

    SqlParameter parm2=new SqlParameter("@path",SqlDbType.VarChar); 
    parm2.value = "Some Path";
    parm2.Direction=ParameterDirection.Output;
    cmd.Parameters.Add(parm2); 


    SqlParameter parm3 = new SqlParameter("@errorId",SqlDbType.Int);
    parm3.Direction=ParameterDirection.Output; 
    cmd.Parameters.Add(parm3); 

    sqlConnection.Open(); 
    sqlConnection.ExecuteNonQuery();

    errorId = cmd.Parameters["@errorId"].Value; //This will 1 or 0
   }

}
Run Code Online (Sandbox Code Playgroud)