Filling a DataTable in C# using MySQL

前端 未结 5 1809
陌清茗
陌清茗 2021-01-07 14:33

I\'m attempting to fill a DataTable with results pulled from a MySQL database, however the DataTable, although it is initialised, doesn\'t populate. I wanted to use this Dat

相关标签:
5条回答
  • 2021-01-07 14:43

    Well, I ... can't figure out what you have done here so I'll paste you my code with which I'm filling datagridview:

    1) Connection should look something like this(if localhost is your server, else, IP adress of server machine):

    string connection = @"server=localhost;uid=root;password=*******;database=*******;port=3306;charset=utf8";
    

    2) Query is ok(it will return you something), but you shouldn't build SQL statements like that.. use parameters instead. See SQL injection.

    3) Code:

    void SelectAllFrom(string query, DataGridView dgv)
            {
                _dataTable.Clear();
    
                try
                {
                    _conn = new MySqlConnection(connection);
                    _conn.Open();
                    _cmd = new MySqlCommand
                    {
                        Connection = _conn,
                        CommandText = query
                    };
                    _cmd.ExecuteNonQuery();
    
                    _da = new MySqlDataAdapter(_cmd);
                    _da.Fill(_dataTable);
    
                    _cb = new MySqlCommandBuilder(_da);
    
                    dgv.DataSource = _dataTable;
                    dgv.DataMember = _dataTable.TableName;
                    dgv.AutoResizeColumns();
    
                    _conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (_conn != null) _conn.Close();
                }
            }
    

    So, every time I want to display some content of table in mysql database I call this method, pass query string and datagridview name to that method. and, that is it.

    For your sake, compare this example with your and see what can you use from both of it. Maybe, listview is not the best thing for you, just saying ...

    hope that this will help you a little bit.

    0 讨论(0)
  • 2021-01-07 14:51

    Debug your application and see if your sql statement/ connection string is correct and returns some value, also verify if your application is not throwing any exception.

    0 讨论(0)
  • 2021-01-07 14:52

    Check your connection string and instead of using

    MySqlCommand cmd = new MySqlCommand(query, connection);
    MySqlDataAdapter returnVal = new MySqlDataAdapter(query,connection);
    DataTable dt = new DataTable("CharacterInfo");
    returnVal.Fill(dt);
    this.CloseConnection();
    return dt;
    

    you can use this one

    MySqlCommand cmd = new MySqlCommand(query, connection);
    DataTable dt = new DataTable();
    dt.load(cmd.ExecuteReader());
    return dt;
    
    0 讨论(0)
  • 2021-01-07 14:54

    Here the following why the codes would not work.

    1. Connection
      The connection of your mySQL is invalid
    2. Query
      I guess do you want to search in the table, try this query

    string query = "SELECT * FROM characters WHERE _SteamName LIKE '" + loginName + "%'";

    notice the LIKE and % this could help to list all the data.
    for more details String Comparison Functions

    0 讨论(0)
  • 2021-01-07 15:02

    Your connection string is invalid.

    Set it as follows:

    connection = "Server=myServer;Database=myDataBase;Uid=myUser;Pwd=myPassword;";
    

    Refer: mysql connection strings

    0 讨论(0)
提交回复
热议问题