使用条件删除多行

时间:2015-07-21 10:21:52

标签: c# sql

你好试图根据条件删除表的几行,但是我需要一些帮助,基本上我想删除所有行,条件为真然后停止并保持其余不变。

编辑:关于我道歉的评论,我是一个相当新的编程,对不起,如果不能正确地对这个网站做新的事情。

private static void addEndPoint(AdaptiveAccountsPortType port,
            String endpointURL) {
        BindingProvider bp = (BindingProvider)port;
        bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);

        /*List hchain = bp.getBinding().getHandlerChain();
        if (hchain == null) {
          hchain = new ArrayList();
        }
        hchain.add(new HTTPUserAgentHandler());
        bp.getBinding().setHandlerChain(hchain);*/
    }

1 个答案:

答案 0 :(得分:1)

您需要做的是:

private void button1_Click(object sender, EventArgs e)
{
     bool check = true;
     do
     {
          string connectionString = @"Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd";
          string queryString = string.Empty;
          using (SqlConnection connection = new SqlConnection(connectionString))
          {
               connection.Open();
               queryString = "DELETE FROM wgcdoccab WHERE 'tipodoc' ='FSS' and 'FP' ";
               SqlCommand command = new SqlCommand(queryString, connection);
               command.Connection.Open();
               command.ExecuteNonQuery();
           }
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
                connection.Open();
                queryString = "SELECT * from wgcdoccab where 'tipodoc' !='FSS' and !='FP'  and contribuinte !='999999990' and  datadoc != CONVERT(varchar(10),(dateadd(dd, -1, getdate())),120)";

                using (SqlCommand command = new SqlCommand(queryString, connection))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        check = true;
                    }
                    else
                    {
                        check = false;
                    }
                }
           }          
      } 
      while (check);
}

通常我通过编辑代码完成的工作是:

  1. 添加using语句,以便从已建立的连接中释放资源。

  2. 您应该使用查询的exeqution中返回的类型。 ExecuteNonQuery()将返回受影响的行数,在我们的示例中,我们对删除查询后从select语句返回的行特别感兴趣。我们创建一个读者,并根据行数,在我们的情况下,我们只感兴趣,如果有行或没有,分支适当。如果我们没有从select中删除任何行(一切都被删除),我们就继续,如果我们什么都没有(reader.HasRows返回false),我们重复删除查询并再次检查。 就这么简单。

相关问题