繁体   English   中英

将 OUT 参数打印到 c# winform

[英]Print OUT parameter to c# winform

我正在使用 3 层架构在 c# 语言中开发 winform 应用程序。 我需要在 winform 消息中打印 sql 服务器错误 (@ErrorPrint)。 我已经尝试了几种方法,但我无法得到我的结果。 我的代码是... SQL SERVER PROCEDURE AS

 CREATE PROC [dbo].[sp_CheckLogin]
        @userName VARCHAR(20),
        @PassCode VARCHAR(20),
        @ErrorPrint varchar(200) OUT
        AS
        BEGIN   
            --SELECT COUNT(*) from UserTable where UserName=@userName and  @PassCode=CONVERT(VARCHAR(50),DECRYPTBYPASSPHRASE('PharmaPro',PassCode))
            SELECT COUNT(*) FROM UserTable WHERE UserName=@userName AND @PassCode=CONVERT(VARCHAR(50),DECRYPTBYPASSPHRASE('PharmaPro',PassCode)) AND IntruderLocked=0 and IsBlocked=0   
        END
        
        IF EXISTS ( SELECT * FROM UserTable WHERE IsBlocked=1 )
            BEGIN
                SET @ErrorPrint= 'Your account is blocked. Contact administrator'
            END
            
            
        IF NOT EXISTS (SELECT * FROM UserTable WHERE PassCode=@PassCode)
            BEGIN   
                UPDATE UserTable SET IntruderCapture=IntruderCapture+1      
            END 
        
        IF  EXISTS(SELECT * FROM  UserTable WHERE IntruderCapture>3)
            BEGIN
                UPDATE UserTable SET IntruderLocked=1
            END
            
        IF EXISTS (SELECT * FROM UserTable WHERE IntruderLocked=1)
            BEGIN
                SET @ErrorPrint='Intruder locked. Contact administrator'
            END

我的数据逻辑层就像

  public string login_details(Login_Entity Users)
                {
                    SqlConnection connection = new SqlConnection(conn);
                    connection.Open();
                    SqlCommand cmd = new SqlCommand("sp_CheckLogin", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@userName", Users.UserName_Details);           
                    cmd.Parameters.AddWithValue("@PassCode", Users.Password_Details);
                    cmd.Parameters.Add("@ErrorPrint", SqlDbType.Char, 500);
                    cmd.Parameters["@ErrorPrint"].Direction = ParameterDirection.Output;
                    string message = cmd.Parameters["@ErrorPrint"].Value.ToString();
                    cmd.ExecuteScalar().ToString();
                    connection.Close();
                    return message;
                }     

类似的演示代码就像

private void button1_Click(object sender, EventArgs e) {

    if (txtUserName.Text == "" && txtPassword.Text == "")
    {
        lblError.Text = ("UserName and Password is Empty");
        txtUserName.Focus();
    }
    else if (txtUserName.Text == "")
    {
        lblError.Text = ("UserName is Empty");
        txtUserName.Focus();
    }
    else if (txtPassword.Text == "")
    {
        lblError.Text = ("Password is Empty");
        txtPassword.Focus();
    }
    else
    {                
        Login_Entity LE = new Login_Entity();
        Login_BL LB = new Login_BL();
        LE.UserName_Details = txtUserName.Text;               
        LE.Password_Details = txtPassword.Text;
        try
        {
            string _login = LB.Login_BLL(LE);
            int i = int.Parse(_login);
            if (i > 0)
            {
                Home hm = new Home();
                hm.Show();
                username = txtUserName.Text;
                hm.lblusername.Text = username;
                this.Hide();
            }
            else
            {
               
                lblError.Text = ("UserName/Pasword Error");
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            LB = null;
        }
    }
}

我需要从 WinForms 打印 SP @ErrorPrint

使用ExecuteNonQuery而不是ExecuteScalar并在执行查询后检索输出参数值:

cmd.Parameters["@ErrorPrint"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string message = cmd.Parameters["@ErrorPrint"].Value.ToString();
connection.Close();
return message;

避免使用sp_存储过程名称前缀,因为它表示系统存储过程。 请参阅文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM