string[] strTxt = TextBox1.Text.Split("\n".ToCharArray());
int i = dal.deleteUser(strTxt);    public int deleteUser(string[] strTxt)
    {
        string sql;
        SqlParameter[] p;
        for (int n = 0; n < strTxt.Length; n++)
        {
            sql = "delete from bx_users where username in( @strTxt" + n + ")";
            command = getCommand(sql, conString);
            p = new SqlParameter[] { new SqlParameter("strTxt" + n, strTxt[n]) };
           command.Parameters.AddRange(p);
        } 
        int i = command.ExecuteNonQuery();
        con.Close();
        return i;
     }我这个功能是从多行文本框读取用户名,然后在数据库批量删除用户。 请问为什么我这个方法只删除了最后一个用户?
请大家帮帮忙,如果能提供些批量数据处理的资料或实例最好不过了。

解决方案 »

  1.   

    SqlCommand 和 SqlParameter[]实例化里很多次···
      

  2.   

     int i = command.ExecuteNonQuery();
    放到循环里面
      

  3.   

    将int i = command.ExecuteNonQuery();放到循环里面string[] strTxt = TextBox1.Text.Split("\n".ToCharArray());
    int i = dal.deleteUser(strTxt);  public int deleteUser(string[] strTxt)
      {
      string sql;
      SqlParameter[] p;
      for (int n = 0; n < strTxt.Length; n++)
      {
      sql = "delete from bx_users where username in( @strTxt" + n + ")";
      command = getCommand(sql, conString);
      p = new SqlParameter[] { new SqlParameter("strTxt" + n, strTxt[n]) };
      command.Parameters.AddRange(p);
    int i = command.ExecuteNonQuery();
      }  
      
      con.Close();
      return i;
      }