日期:2014-05-18  浏览次数:21049 次

各位大侠帮小弟看看
try
  {
   

  for (int i = 0; i < dt.Rows.Count-1; i++)
  {
   
  fid =Convert.ToInt32(dt.Rows[i]["fid"].ToString());
  fname = dt.Rows[i]["fname"].ToString();
  fsellprice =Convert.ToDouble(dt.Rows[i]["fsellprice"].ToString());
  fnumber =Convert.ToInt32(dt.Rows[i]["fnumber"].ToString());
  funitprice =Convert.ToDouble(dt.Rows[i]["funitprice"].ToString());
  fbarcode =Convert.ToInt32(dt.Rows[i]["fbarcode"].ToString() ); 

  sql = "insert into ftest(fid,fname,fsellprice,fnumber,funitprice,fbarcode) values(" + fid + ",'" + fname + "'," + fsellprice + "," + fnumber + "," + funitprice + "," + fbarcode + ")";
  MySqlCommand cmd = new MySqlCommand(sql, conn);
  int count = Convert.ToInt32(cmd.ExecuteNonQuery());
  //if (count == 1)
  //{
  // MessageBox.Show("保存成功", "提示");
  //}
  //else
  //{
  // MessageBox.Show("保存失败", "提示");
  //}
   
  }
   


  }
我想在执行完循环之后再提示保存成功或者失败,因为我定义的 count是在循环体里面的,在循环体外面无法使用。各位大侠有什么方法?

------解决方案--------------------
int count

放函数外面啊不久可以在其他地方使用啊

------解决方案--------------------
C# code
int count=0;
try
  {
    

  for (int i = 0; i < dt.Rows.Count-1; i++)
  {
    
  fid =Convert.ToInt32(dt.Rows[i]["fid"].ToString());
  fname = dt.Rows[i]["fname"].ToString();
  fsellprice =Convert.ToDouble(dt.Rows[i]["fsellprice"].ToString());
  fnumber =Convert.ToInt32(dt.Rows[i]["fnumber"].ToString());
  funitprice =Convert.ToDouble(dt.Rows[i]["funitprice"].ToString());
  fbarcode =Convert.ToInt32(dt.Rows[i]["fbarcode"].ToString() );  

  sql = "insert into ftest(fid,fname,fsellprice,fnumber,funitprice,fbarcode) values(" + fid + ",'" + fname + "'," + fsellprice + "," + fnumber + "," + funitprice + "," + fbarcode + ")";
  MySqlCommand cmd = new MySqlCommand(sql, conn);
   count = Convert.ToInt32(cmd.ExecuteNonQuery());
  
  }
    
//if (count == 1)
  //{
  // MessageBox.Show("保存成功", "提示");
  //}
  //else
  //{
  // MessageBox.Show("保存失败", "提示");
  //}
    

  }

------解决方案--------------------
count 放到循环体外面定义,不就OK 了嘛