【问题标题】:Print OUT parameter to c# winform将 OUT 参数打印到 c# winform
【发布时间】:2020-12-23 13:17:17
【问题描述】:

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

 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

【问题讨论】:

    标签: c# sql-server 3-tier


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多