Click here to Skip to main content
15,886,661 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ketnoi
{
    public partial class Form1 : Form
    {
      
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadData();
        }

        public void loadData()
        {

			var database = new Database();
            database.connect_db();
            
            string query = "SELECT * FROM kho_k22";
            MySqlCommand mySqlCommand = new MySqlCommand(query);
            mySqlCommand.Connection = database.mySqlConnection;
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = mySqlCommand;
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = dt;  

            dataGridViewMySql.DataSource = bindingSource;

            database.close_db();
        }
    }
}


What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace ketnoi
{
    internal class Database
    {
        static readonly string server = "127.0.0.1";
        static readonly string user = "root";
        static readonly string password = "Ngoctung123";
        static readonly string database = "quan_ly_kho_k22";
        public static string connection_string = " server = '" + server + "'; user = '" + user + "'; password = '" + password + "'; database = '" + database + "'  ";
        public MySqlConnection mySqlConnection = new MySqlConnection(connection_string);
        
        public bool connect_db()
        {
            try
            {   
                mySqlConnection.Open();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
 
		public bool close_db()
		{
			try
			{
				mySqlConnection.Close();
				return true;
			}
			catch (Exception ex)
			{
				return false;
			}
		}
    }
}
Posted
Updated 31-May-23 19:07pm
v2
Comments
Richard MacCutchan 1-Jun-23 4:07am    
What error?
Andre Oosthuizen 1-Jun-23 4:15am    
Is your Apache and MySql running before you run your app (Using localhost)? Use the XAMPP Control Panel to start both.

1 solution

We have no idea what error that generates - and no access to your system or network to try and find out.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

You swallow your exception, which contains detail on why the connection attempt failed:
try
{
    mySqlConnection.Open();
    return true;
}
catch(Exception ex)
{
    return false;
}
The error message and detail is normally very useful!
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
If it throws an exception you will see that, and can examine the Exception object to see what exactly it is complaining about. (Assuming it's a connection problem). If it isn't, then you need to look at the actual error you don't catch and look at what it is saying - probably something about there being no table called "kho_k22".

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900