private void button2_Click(object sender, EventArgs e)
        {
           sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
            //定义删除SQL语句
            string sqlStr = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
            SqlCommand sqlcmd = new SqlCommand(sqlStr, sqlcon);//实例化SqlCommand对象
            if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
            {
                sqlcon.Open();//打开数据库连接
            }
            int records = Convert.ToInt32(sqlcmd.ExecuteNonQuery());//执行删除命令
            if (records > 0)//判断删除是否成功
                label2.Text="删除成功!";
            else
                label2.Text="删除失败!";
            sqlcmd.Dispose();//释放SqlCommand对象资源
            sqlcon.Close();//关闭数据库连接
            Form1_Load(sender, e);//刷新窗体
        }
        private void button1_Click(object sender, EventArgs e)
        {
            sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
            //定义删除SQL语句
            string sqlStr1 = "insert into IlluminationInfo(Roomnumber,lampnumber,consumption,cost,lamppower)values(" + Convert.ToInt32(textBox1.Text) + "," + Convert.ToInt32(textBox2.Text) + "," + Convert.ToInt32(textBox3.Text) + "," + Convert.ToInt32(textBox4.Text) + "," + Convert.ToInt32(textBox5.Text) + ")";
            SqlCommand sqlcmd1 = new SqlCommand(sqlStr1, sqlcon);//实例化SqlCommand对象
            //string sqlStr2 = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
            //SqlCommand sqlcmd2 = new SqlCommand(sqlStr2, sqlcon);//实例化SqlCommand对象
            if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
            {
                sqlcon.Open();//打开数据库连接
            }
            int records1 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
            if (records1 > 0)//判断删除是否成功
                label2.Text = "添加成功!";
            else
                label2.Text = "添加失败!";
            //if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
            //{
            //    sqlcon.Open();//打开数据库连接
            //}
            //int records2 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
           // if (records2 > 0)//判断删除是否成功
              //  label2.Text = "tj成功!";
            //else
               // label2.Text = "tj失败!";
            sqlcmd1.Dispose();//释放SqlCommand对象资源
            sqlcon.Close();//关闭数据库连接
            Form1_Load(sender, e);//刷新窗体
        }
这是我的程序片段
   我在界面上设了一个button2删除按钮  和button添加按钮 为什么不能同时执行呢?
请各位大侠帮帮我

解决方案 »

  1.   

    不是同时执行  是依次执行   是一个添加功能   和  删除功能。就是两个按钮,运行了多次,貌似只有在button1下可以执行  
    现在的新问题是我为了验证它是不是可以在button2下是否能执行  在Form窗体上删除了button1   结果还是不行   
    最后我把button1又从toolbox里面放回Form1,改好名字以后 运行成功,但是没办法连接库,就是往数据库里面添加  删除不了啦   
    请各位大侠帮忙  
    谢谢:)
      

  2.   

    你的逻辑上有问题,因为你点BUTTON1时,完成后数据库存已关闭,在实例化SqlCommand对象时sqlcon是关闭的,判断连接是否关闭应放前面。private void button2_Click(object sender, EventArgs e)
      {
      sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
      //定义删除SQL语句
      string sqlStr = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
    if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
      {
      sqlcon.Open();//打开数据库连接
      }
      SqlCommand sqlcmd = new SqlCommand(sqlStr, sqlcon);//实例化SqlCommand对象
      int records = Convert.ToInt32(sqlcmd.ExecuteNonQuery());//执行删除命令
      if (records > 0)//判断删除是否成功
      label2.Text="删除成功!";
      else
      label2.Text="删除失败!";
      sqlcmd.Dispose();//释放SqlCommand对象资源
      sqlcon.Close();//关闭数据库连接
      Form1_Load(sender, e);//刷新窗体
      }
      private void button1_Click(object sender, EventArgs e)
      {
      sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
      //定义删除SQL语句
      string sqlStr1 = "insert into IlluminationInfo(Roomnumber,lampnumber,consumption,cost,lamppower)values(" + Convert.ToInt32(textBox1.Text) + "," + Convert.ToInt32(textBox2.Text) + "," + Convert.ToInt32(textBox3.Text) + "," + Convert.ToInt32(textBox4.Text) + "," + Convert.ToInt32(textBox5.Text) + ")";
    if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
      {
      sqlcon.Open();//打开数据库连接
      }  SqlCommand sqlcmd1 = new SqlCommand(sqlStr1, sqlcon);//实例化SqlCommand对象
      int records1 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
      if (records1 > 0)//判断删除是否成功
      label2.Text = "添加成功!";
      else
      label2.Text = "添加失败!";
      sqlcmd1.Dispose();//释放SqlCommand对象资源
      sqlcon.Close();//关闭数据库连接
      Form1_Load(sender, e);//刷新窗体
      }
      

  3.   

    添加删除不了了  你还是应该去断点跟踪下代码  查看sqlcon.State的状态     
    我想问下 你每次运行的时候第一次点添加时成功吗  是不是删除不成功?   那你第一次运行时先点删除看是否成功  再点添加看是否也成功  
    还有你把你的SQL语句去数据库中运行一次 看有没有问题 能不能得到你要的结果
      

  4.   

    我猜你删除按钮(也就是Button2)没注册事件.在Button2属性那里点那个闪电,找到click,把事件注册上