不允许更改' ConnectionString'连接时的属性(state = Open)

时间:2017-10-16 10:34:51

标签: c# connection-string delete-row 3-tier

我正在创建3层架构的Windows应用程序,每次我删除被检查的datagrid中的多行时,都会显示此错误。代码如下:

IN DAL:

    public void delete_contact(MySqlCommand cmd)
            {
                conn.ConnectionString = connString;

                if (ConnectionState.Closed == conn.State)
                    conn.Open();

                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = delete_proc;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
            }

    DELETE BUTTON:
     private void button2_Click(object sender, EventArgs e)
            {
                addcon ac = new addcon();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    isCellChecked = Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value);
                    if (isCellChecked == true)
                    {
                        ac.snum = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                        ac.delete_proc();
                        MessageBox.Show("Deleted", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

如何修复错误?请帮忙。感谢

0 个答案:

没有答案