c# 代码如下,请问为什么只执行cmd1, 而不执行cmd2, 和cmd3呢?try
                {
                    using (SqlConnection con = new SqlConnection("Server=" + strServerName + ";database=" + strDataName + ";Uid=" + strUser + ";Pwd=" + strPwd))
                    {
                       
                        
                       SqlCommand cmd1 = new SqlCommand(sql[0], con);
                       textBox1.Text=Convert.ToString( cmd1.ExecuteNonQuery());
                        progressBar1.Value = 1;
                        cnt += 1;                        SqlCommand cmd2 = new SqlCommand(sql[1], con);
                       cmd2.ExecuteNonQuery();
                        progressBar1.Value = 2;
                        cnt += 1;                        SqlCommand cmd3 = new SqlCommand(sql[2], con);
                       cmd3.ExecuteNonQuery();
                        progressBar1.Value = 3;
                        cnt += 1;                     }
                catch (Exception ex)
                {                    MessageBox.Show(ex.Message);
                }        }

解决方案 »

  1.   

    分别写在自己的连接块里cmd1执行后连接就完了
      

  2.   

    不知道是LZ说的不清楚还是就是这么简单,如果是这么理解的话, 
    楼主只需要定义一个变量,每个 CMD之后+1 当变量值>1的时候就不执行,
    int count=0
    if(count==0) 
    {
      excute cmdone 
      count+=
    }
    cmdtwo 
    cmdthree
      

  3.   


    放在自己的连接快里也试过了,可是不知道为什么cmd2,和cmd3就是不执行。还试过了把cmd2,和cmd3放到其他事件里,还是不执行,是因为和cmd1太相似了难道? 总也搞不定
      

  4.   

    你这cmd1也不会执行,连接都没有打开con.Open()
      

  5.   


    跟踪调试了,不报错,到cmd2,cmd3时cmdtext也是对的,可是就是不执行
      

  6.   

     
    放到两个连接块里,像这样,cmd1执行, cmd3就不执行,这是为啥阿
    #region try catch语句
                try
                    {
                        using (SqlConnection con1 = new SqlConnection("Server=" + strServerName + ";database=" + strDataName + ";Uid=" + strUser + ";Pwd=" + strPwd))
                        {
                                                                          
                           con1.Open();                       SqlCommand cmd1 = new SqlCommand(sqlCnt[0], con1);
                           textBox1.Text=Convert.ToString( cmd1.ExecuteNonQuery());
                            progressBar1.Value = 1;
                            cnt += 1;
                            con1.Close();
                          
                            
                        }
                    }
                    catch (Exception ex)
                    {                    MessageBox.Show(ex.Message);
                    }
                #endregion
                            #region try catch语句
                try
                {
                    using (SqlConnection con3 = new SqlConnection("Server=" + strServerName + ";database=" + strDataName + ";Uid=" + strUser + ";Pwd=" + strPwd))
                    {
                        
                        con3.Open();                   
                        SqlCommand cmd3 = new SqlCommand(sqlCnt[2], con3);
                        textBox3.Text = Convert.ToString(cmd3.ExecuteNonQuery());
                        progressBar1.Value = 3;
                        cnt += 1;                    con3.Close();
                        if (cnt > 0)
                        {
                            MessageBox.Show("提取数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("提取数据失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }                }
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.Message);
                }
                #endregion
      

  7.   

    没这么写过,用一个cmd就行了啊?根本不需要cmd2,cmd3
      

  8.   


    可是想执行第一个cmd计数,输出进度,然后再执行第二个cmd2
      

  9.   

    试试catch (System.Data.SqlClient.SqlException exp)
      

  10.   

    SqlCommand cmd = new SqlCommand(sql[0], con);
      cmd.ExecuteNonQuery();
    cmd.CommandText=sql[1];
    cmd.ExecuteNonQuery();
    cmd.CommandText=sql[2];
    cmd.ExecuteNonQuery();