【问题标题】:Show messages according to the MySQL database data? C#根据 MySQL 数据库数据显示消息? C#
【发布时间】:2014-10-06 03:49:52
【问题描述】:

我想从 MySQL 数据库中获取值,这需要根据值显示消息。但它不会发生,并且总是显示 int privilege 为 0。如果我没有指定该默认值,代码上将显示错误。

如何解决此问题并根据int privilege 值显示消息?

private void button_login_Click(object sender, RoutedEventArgs e)
{

    string username = usernameInput.Text;
    string password = passwordInput.Password;
    int privilege = 0;

    try
    {                
        //This is command class which will handle the query and connection object.
        string Query = "SELECT`tbl_user_login`.`u_id`,`tbl_user_login`.`u_username`,
                 `tbl_user_login`.`u_password`,`tbl_user_login`.`u_privilege` 
                 FROM `bcasdb`.`tbl_user_login`WHERE `tbl_user_login`.`u_username` = '"
                 + username + "' AND `tbl_user_login`.`u_password` ='" + password 
                 + "' AND `tbl_user_login`.`u_privilege` = @privi;";
        MySqlConnection conn = 
                     new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        cmd.Parameters.AddWithValue("@privi", privilege);
        MySqlDataReader MyReader;
        conn.Open();
        MyReader = cmd.ExecuteReader();
          // Here our query will be executed and data saved into the database.

        if (MyReader.HasRows && this.Frame != null)
        {
            while (MyReader.Read())
            {

                if (privilege == 1)
                {
                    DisplayMsgBox("click ok to open the admin page ", "OK");
                }
                if (privilege == 2)
                {
                    DisplayMsgBox("click ok to open the staff page ", "OK");
                }
                else
                {
                    DisplayMsgBox("privilege 0", "ok");
                }   
            }
        }                
        else
        {
            DisplayMsgBox("sucess else", "ok");
        }

        conn.Close();
    }
    catch (Exception )
    {
        DisplayMsgBox("sucess catch", "ok");
    }
}

【问题讨论】:

    标签: c# mysql windows windows-store-apps int


    【解决方案1】:

    看起来您正在尝试从tbl_user_login 表中检查u_privilege 列的值,而不是根据privilege 创建where 条件。您需要删除此 where 条件

    AND `tbl_user_login`.`u_privilege` = @privi
    

    并删除参数分配

    cmd.Parameters.AddWithValue("@privi", privilege);
    

    您可以通过在while (MyReader.Read()) 块中使用MySqlDataReader.GetInt32 语法来获取tbl_user_login.u_privilege 的值

    MyReader.GetInt32(3)
    

    请注意,使用 3 是因为 MyReader.GetInt32 需要从零开始的索引参数,而 tbl_user_login.u_privilege 是查询的第四列。该值应分配给privilege 变量,如下所示

    privilege = MyReader.GetInt32(3)
    

    附带说明,您应该参数化您的查询以避免SQL injection。这是实现上述更改后的完整代码

    int privilege = 0;
    
    try
    {                
        //This is command class which will handle the query and connection object.
        string Query = "SELECT`tbl_user_login`.`u_id`,`tbl_user_login`.`u_username`,
                 `tbl_user_login`.`u_password`,`tbl_user_login`.`u_privilege` 
                 FROM `bcasdb`.`tbl_user_login`WHERE `tbl_user_login`.`u_username` = 
                 @username AND `tbl_user_login`.`u_password` = @password;";
        MySqlConnection conn = 
                     new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        cmd.Parameters.AddWithValue("@username", username);
        cmd.Parameters.AddWithValue("@password", password);
        MySqlDataReader MyReader;
        conn.Open();
        MyReader = cmd.ExecuteReader();
          // Here our query will be executed and data saved into the database.
    
        if (MyReader.HasRows && this.Frame != null)
        {
            while (MyReader.Read())
            {
                privilege = MyReader.GetInt32(3)
    
                if (privilege == 1)
                {
                    DisplayMsgBox("click ok to open the admin page ", "OK");
                }
                if (privilege == 2)
                {
                    DisplayMsgBox("click ok to open the staff page ", "OK");
                }
                else
                {
                    DisplayMsgBox("privilege 0", "ok");
                }   
            }
        }                
        else
        {
            DisplayMsgBox("sucess else", "ok");
        }
    
        conn.Close();
    }
    catch (Exception )
    {
        DisplayMsgBox("sucess catch", "ok");
    }
    

    【讨论】:

    • 是的,它的工作,我想知道你使用了那个特权 = MyReader.GetInt32(3) 是什么意思 (3)
    • MyReader.GetInt32表示将查询结果的第n列的值取整为整数,假设该列是整数,如果使用从零开始的索引,则3表示查询结果的第四列,即 0 表示第一列,1 表示第二列,依此类推。
    • 我没办法给你声望:(
    【解决方案2】:

    如果我没有错,特权将作为字符串类型返回。尝试将其作为字符串输入,然后将其转换为整数?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多