使用MySQL在C#中填充数据表 [英] Filling a DataTable in C# using MySQL

查看:151
本文介绍了使用MySQL在C#中填充数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从MySQL数据库获取的结果填充数据表,但是该数据表尽管已初始化,但并未填充.我想使用此DataTable填充ListView.这是我为DataTable设置的内容:

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 DataTable to fill a ListView. Here's what I've got for the setting of the DataTable:

    public DataTable SelectCharacters(string loginName)
    {
        this.Initialise();
        string connection = "0.0.0.0";
        string query = "SELECT * FROM characters WHERE _SteamName = '" + loginName + "'";

        if (this.OpenConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            MySqlDataAdapter returnVal = new MySqlDataAdapter(query,connection);
            DataTable dt = new DataTable("CharacterInfo");
            returnVal.Fill(dt);
            this.CloseConnection();
            return dt;
        }
        else
        {
            this.CloseConnection();
            DataTable dt = new DataTable("CharacterInfo");
            return dt;            
        }
    }

为了填充ListView,我得到了:

And for the filling of the ListView, I've got:

 private void button1_Click(object sender, EventArgs e)
    {
        string searchCriteria = textBox1.Text;

        dt = characterDatabase.SelectCharacters(searchCriteria);
        MessageBox.Show(dt.ToString());

        listView1.View = View.Details;

        ListViewItem iItem;
        foreach (DataRow row in dt.Rows)
        {
            iItem = new ListViewItem();
            for (int i = 0; i < row.ItemArray.Length; i++)
            {
                if (i == 0)
                    iItem.Text = row.ItemArray[i].ToString();
                else
                    iItem.SubItems.Add(row.ItemArray[i].ToString());
            }
            listView1.Items.Add(iItem);
        }
    }

有什么我想念的吗?包含了MessageBox,所以我可以看看它是否已填充,没有运气.

Is there something I'm missing? The MessageBox was included so I could see if it has populated, to no luck.

感谢您提供的任何帮助.

Thanks for any help you can give.

推荐答案

好吧,我...无法弄清您在这里所做的工作,因此我将粘贴我的代码以填充datagridview:

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)连接应如下所示(如果localhost是您的服务器,否则为服务器计算机的IP地址):

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)查询还可以(它将返回一些信息),但是您不应该构建像这样的SQL语句.请改用参数.请参阅 SQL注入.

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)代码:

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

因此,每次我想在mysql数据库中显示表的某些内容时,我都会调用此方法,并将查询字符串和datagridview名称传递给该方法.就是这样.

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.

为您着想,将此示例与您的示例进行比较,看看您可以从这两个示例中使用什么.也许,listview并不是您的最佳选择,只是说...

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.

这篇关于使用MySQL在C#中填充数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆