【问题标题】:Populate DataGrid from Mysql Database从 Mysql 数据库填充 DataGrid
【发布时间】:2016-07-07 18:46:33
【问题描述】:

首先,我是 C# 和 Visual Studio 的新手。 我遵循了有关如何从 MySql 表中填充 DataGridView 的教程。 我多次检查复制错误,但没有发现任何错误。 代码是这样的:

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;
    }
}
    }
    }

我似乎找不到问题,也没有显示错误。

【问题讨论】:

    标签: c# mysql datagridview datagrid


    【解决方案1】:

    由于您是 Visual Studio 新手,我的建议是您必须学习的第一件事是调试。它将为您节省很多时间。在这种情况下,在您的 form_load 中放置一个断点并逐步运行代码。您可以通过这种方式查看正在发生的事情,并检查所有变量。我这么说是因为我在您的代码中看不到任何错误,所以您可能甚至没有连接到数据库。调试你的代码你可能会明白为什么

    【讨论】:

    • 哦,你猜怎么着。 Debug.WriteLine 也不起作用。无论我把它放在哪里都是行不通的。再次没有错误,没有任何调试。我猜 C 编程语言是其中的一种……
    【解决方案2】:

    好的,我想通了! 我所要做的就是从 Form1() 函数内部调用 Form1_Load。 目前一切似乎都运行良好。 谢谢,C# 充满了该死的惊喜....

    【讨论】:

    • 嗯...您的项目是“控制台应用程序”类型的吗?如果是,显然不会调用 Form_Load,因为它是一个 Windows 窗体事件处理程序。
    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多