C# 执行 SQL 查询和 return 或显示响应

C# execute SQL query and return or show response

我正在尝试在我的 C# 应用程序中 运行 一个 SELECT 查询,但我实际上卡住了。我如何 select 来自 MySql 数据库的东西?

我当前的代码如下所示:

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 Helpful
{
    class database_connector
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        // Constructor
        public database_connector()
        {
            Initialize();
        }

        //Initialize values
        private void Initialize()
        {
            server = "xxx";
            database = "xxx";
            uid = "xxx";
            password = "xxx";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);
        }

        //open connection to database
        private bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server. Contact administrator.");
                        break;
                    case 1045:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
                return false;
            }
        }

        //Close connection
        private bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        //Select statement
        public void user_check(string username, string password)
        {
            string query = "SELECT * FROM swear_tool WHERE username =" + username;

            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                dataReader.Close();
                this.CloseConnection();
            }
        }
    }
}

但是我怎么才能知道这是否真的有效呢?因为如果我尝试在消息框中显示结果,它会 return,无法转换为字符串。

if (this.OpenConnection() == true)
{
    MySqlCommand cmd = new MySqlCommand(query, connection);
    MySqlDataReader dataReader = cmd.ExecuteReader();
    MessageBox.Show(dataReader);
    dataReader.Close();
    this.CloseConnection();
}

你需要知道你想要从数据库中得到什么字段。 使用像这样的 SQL SELECT Id, Name FROM swear_tool WHERE username =" + username 和检索 Name 字段的代码。
索引 0 = ID
索引 1 = 名称 <-- GetString(1)

if (dataReader.HasRows) {
    while (dataReader.Read()) {
        Console.WriteLine("{0}", dataReader.GetString(1) );
    }
} else {
    Console.WriteLine("No rows.");
}