mysql datareader找不到行c# [英] mysql datareader not finding rows c#

查看:68
本文介绍了mysql datareader找不到行c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 c# 和 mysql 的问题.我想做一个非常简单的登录表单,它连接到我的本地数据库.我已连接到工作(已测试),但读取从选择返回的数据时遇到问题.

I got a question about c# and mysql. I would like to make a very simpel login form that is connected to my local db. I got the connection to work (tested it) but i have a problem with reading my data that is returned from a select.

我试图将一个 ID 放入一个字符串中,以便我可以显示它(这只是为了测试).现在我在谷歌上搜索了很多,几乎每个人都有这样的事情.当我执行它不会出错,但我的 sqldatareader 什么也没找到.首先,如果我问它是否有任何行而没有.

I'm trying to put an ID into a string so I can display it(this is just for testing). Now I have searched a lot on google and almost everyone has something like this. When I execute it doesn't give error but my sqldatareader finds nothing. In the first if I ask if it has got any rows and there are none.

我做错了什么?我的用户名/密码确实存在于我的数据库中.

What am I doing wrong? My username/password do exist in my db.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace eindwerk
 {
  public partial class LoginForm : Form
   {
    string myConnection = "Server=localhost;Database=mydb;Uid=root;Pwd=root;";
    MySqlCommand cmd;
    MySqlConnection connection;

    public LoginForm()
    {
        InitializeComponent();
        connection  = new MySqlConnection(myConnection);

        connection.Open();
    }

    private void loginForm_Load(object sender, EventArgs e)
    {
        this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                       (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {

        try
        {
            cmd = connection.CreateCommand();
            cmd.CommandText = "SELECT idlogin FROM login WHERE (username='@username') AND (password='@password') LIMIT 1;";
            cmd.Parameters.AddWithValue("@username", txtbxLoginUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtbxLoginPassword.Text);


            MySqlDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    label1.Text = rdr.GetInt32("idlogin").ToString();
                }
            }
            else
            {
                lblLoginError.Visible = true;
            }


            rdr.Close();

        }
        catch {
            lblLoginError.Text = "Nope";
            lblLoginError.Visible = true;
        }


    }
}

}

推荐答案

您多次调用 Read().单次调用While(Reader.Read())并通过if(rdr.HasRows()){}检查结果,检查结果是返回还是什么都没有在回复中.

You are calling Read() Multiple time. Call the While(Reader.Read()) single time and check the result by if(rdr.HasRows()){} for check result is return or nothing is come in the response.

这篇关于mysql datareader找不到行c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆