【问题标题】:Getting GetDecimal to work让 GetDecimal 工作
【发布时间】:2013-06-02 11:38:54
【问题描述】:
MySqlConnection con = new MySqlConnection("host=*;user=*;password=*;database=*;");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM members WHERE username = '" + textBox2.Text + "' AND password = '" + textBox3.Text + "';");
cmd.Connection = con;
DataTable dt = new DataTable();
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();

if (reader.Read() != false)
{
    if (reader.IsDBNull(0) == true)
    {
        cmd.Connection.Close();
        reader.Dispose();
        cmd.Dispose();
        MessageBox.Show("Oops!There was a problem!");
    }
    else
    {
        cmd.Connection.Close();
        reader.Dispose();
        cmd.Dispose();
        this.Hide();
        Main main = new Main();
        main.Show();
        MySqlCommand cmmd = new MySqlCommand("SELECT Pain FROM members WHERE username='" + textBox2.Text + "';");
        cmmd.Connection = con;
        con.Open();
        MySqlDataReader read = cmmd.ExecuteReader();
        if (read.Read())
        {
            if (read.GetDecimal(0) == 1)
            {
                MessageBox.Show("NO");
                cmmd.Connection.Close();
                read.Dispose();
                cmmd.Dispose();
            }
            else
            {
                MessageBox.Show("YES");
                cmmd.Connection.Close();
                read.Dispose();
                cmmd.Dispose();
            }
    }
}
else
{
    MessageBox.Show("You Login Information is incorrect!");
}

我希望 C# 读取 PAIN 列,如果它等于 0 则显示消息 NO,如果等于 1 则显示消息 YES。pain 列是 INT 类型并且是第 8 列

【问题讨论】:

  • 痛苦的列是一个 INT 类型 - 那么也许使用GetInt32() ?
  • 另外,这会报错吗?
  • 不,现在很好,非常感谢 :)

标签: c# mysql if-statement decimal datareader


【解决方案1】:

尝试如下。

using (var con = new MySqlConnection("host=*;user=*;password=*;database=*;"))
using (var cmd = con.CreateCommand())
{
    cmd.CommandText = "SELECT username, Pain FROM members WHERE username = @UserName AND password = @Password";
    cmd.Parameters.AddWithValue("@UserName", textBox2.Text);
    cmd.Parameters.AddWithValue("@Password", textBox3.Text);

    con.Open();
    using (var reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            var username = reader.GetString(0);

            if (!reader.IsDBNull(1))
            {
                var pain = reader.GetInt32(1);
                if (pain == 1)
                {
                    MessageBox.Show("NO");
                }
                else
                {
                    MessageBox.Show("YES");
                }
            }
        }
        else
        {
            MessageBox.Show("You Login Information is incorrect!");
        }

    }

}

【讨论】:

  • 我收到以下错误:'int.TryParse(string, out int)' 的最佳重载方法匹配有一些无效参数 参数 1:无法从 'object' 转换为 'string' 常量值'1' 不能转换为 'bool'
  • @VlasiuRobert 你的痛苦列允许空值吗?
  • 整数? pain = (int?)cmmd.ExecuteScalar();Specified cast is not valid..你的意思是列是否为 Null?
  • @VlasiuRobert 我添加了一些最佳实践的示例代码,不要使用内联参数,如果您使用using blocks,则不需要手动处理/关闭连接等,也不需要的两次调用数据库为此
猜你喜欢
  • 2013-07-22
  • 2012-11-06
  • 2014-03-11
  • 2010-09-27
  • 2016-04-29
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多