简体   繁体   English

从Mysql数据库填充DataGrid

[英]Populate DataGrid from Mysql Database

First of all I'm new with C# and Visual Studio. 首先,我是C#和Visual Studio的新手。 I followed a tutorial on how to populate DataGridView from MySql table. 我遵循了有关如何从MySql表填充DataGridView的教程。 I checked multiple times for copying mistakes but I didn't find any. 我多次检查复制错误,但没有发现任何错误。 The code is this: 代码是这样的:

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 DataGridMain
{
    public partial class Form1 : Form
    {
        private string server;
        private string database;
        private string uid;
        private string password;
        private MySqlConnection connection;
        private MySqlDataAdapter mySqlDataAdapter;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {  
         server = "localhost";
         database = "elphoapp";
         uid = "username";
         password = "password";
         string connectionString;
         connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

    connection = new MySqlConnection(connectionString);

    if (this.OpenConnection() == true)
    {
        mySqlDataAdapter = new MySqlDataAdapter("select * from users", connection);
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        dataGridView1.DataSource = DS.Tables[0];
        this.CloseConnection();
    }


        }

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;
            default:
                MessageBox.Show(ex.Message);
                break;
        }
        return false;
    }
}
        private bool CloseConnection()
{
    try
    {
        connection.Close();
        return true;
    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.Message);
        return false;
    }
}
    }
    }

I can't seem to find a problem and no error is displaying. 我似乎找不到问题,也没有错误显示。

As you are new to Visual Studio, my advice is the first thing you must learn is debugging. 当您不熟悉Visual Studio时,我的建议是您必须学习的第一件事是调试。 It will save you a lot of times. 它将为您节省很多时间。 In this case, put a breakpoint in your form_load and run the code step by step. 在这种情况下,在form_load中放置一个断点并逐步运行代码。 You can that way see what is happening, and inspect all the variables. 您可以通过这种方式查看正在发生的情况,并检查所有变量。 I'm saying this because i can't see any error in your code, so probably you are not even connecting to the database. 我说这是因为我看不到您的代码中的任何错误,所以可能您甚至没有连接到数据库。 Debugging your code you may see why 调试代码,您可能会明白为什么

Ok i figured it out! 好吧,我想通了! All i had to do was to call Form1_Load from inside the Form1() function. 我要做的就是从Form1()函数内部调用Form1_Load。 Everything seems to be working fine at the moment. 目前一切似乎都正常。 Thanks, C# is full of damn surprises.... 谢谢,C#充满了该死的惊喜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM