向表中添加记录的问题 [英] Problem with adding a records to the table

查看:85
本文介绍了向表中添加记录的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



喜欢话题:当我试图在我的表中添加一些记录时,程序会抛出异常System.InvalidOperationException。 />




这是我与DateBase的连接/断开连接:

Hi!

Like in topic: When im trying to add some record to my table, the program is throwing an exception "System.InvalidOperationException".
.

This is my Connection/Disconnection to DateBase:

if (connection.State == ConnectionState.Closed)
                try
                {
                    connection.Open();
                    MessageBox.Show("Połączono z bazą danych!", "Połączono!");
                    LoadData();
                    LoadTree();
                }
                catch(MySql.Data.MySqlClient.MySqlException)
                {
                    MessageBox.Show("Nie połączono z bazą danych!", "Błąd!");
                }
            else
            {
                try
                {
                    connection.Close();
                    MessageBox.Show("Rozłączono z bazą danych!", "Rozłączono!");
                }
                catch(MySql.Data.MySqlClient.MySqlException)
                {
                    MessageBox.Show("Nie udało się!", "Błąd!");
                }
            }





按钮(添加值):



Button (Adding the value):

MySqlCommand cmd = connection.CreateCommand();
                        cmd.CommandText = "INSERT INTO test(nazwa,ilosc) VALUES(@nazwa,@ilosc)";
                        cmd.Parameters.AddWithValue("@nazwa", textBox1.Text);
                        cmd.Parameters.AddWithValue("@ilosc", int.Parse(textBox2.Text));
                        cmd.ExecuteNonQuery();
                        LoadData();
                        LoadTree();





LoadData():



LoadData():

try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT * FROM test";
                MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch(MySql.Data.MySqlClient.MySqlException)
            {
                ShowMessageBoxError();
            }





LoadTree():



LoadTree():

treeView1.Nodes.Clear();
            MySqlDataReader dr = cmd.ExecuteReader();
            
            while(dr.Read())
            {
                TreeNode node = new TreeNode(dr["nazwa"].ToString());
                node.Nodes.Add(dr["ilosc"].ToString());
                treeView1.Nodes.Add(node);
            } 





当我标记LoadTree();在评论的连接(第一个代码)中,问题正在消失。所以我的treeView出现了一些问题,但我无法修复它:/



When im marking "LoadTree();" in connection(first code) to a comment, the problem is vanishing. So my treeView is making some problem, but i cant to fix it :/

推荐答案

尝试在LoadData()之后检查连接状态。

类似于 -

Try checking the connection state after LoadData() too.
Something like-
if (connection.State == ConnectionState.Closed)
    try
    {
       connection.Open();
       MessageBox.Show("Połączono z bazą danych!", "Połączono!");
       LoadData();
       if (connection.State == ConnectionState.Closed)
          connection.Open();
       LoadTree();
    }





如果没有帮助,请告诉我:))



Let me know, if it doesn't help :)


这篇关于向表中添加记录的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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