删除不工作的查询

本文关键字:查询 工作 删除 | 更新日期: 2023-09-27 18:03:42

我一直试图从表中删除行,但我写的代码不工作,我不确定是SQL语法还是代码本身,visual studio没有抛出任何错误,它只是启动程序没有任何麻烦,在我点击按钮删除后,程序冻结,我甚至无法关闭它。如果我在这样做之后检查数据库,它保持不变。

如果我没有很好地解释,我也很抱歉,英语不是我的母语,所以对我来说有点困难,提前谢谢你。

      private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("DOING STUFF");
        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 serie ='1' tipodoc ='FSS' AND and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120) ";                    
                SqlCommand command = new SqlCommand(queryString, connection);
                //command.Connection.Open();
                command.ExecuteNonQuery();
            }
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                queryString = "SELECT * FROM wgcdoccab WHERE serie ='1' tipodoc ='FSS' AND and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120) ";
                using (SqlCommand command = new SqlCommand(queryString, connection))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        check = true;
                    }
                    else
                    {
                        check = false;
                        MessageBox.Show("STILL DOING STUFF");
                    }
                    command.Connection.Close();
                }
            }
        }
        while (check);

删除不工作的查询

我不确定你认为SQL命令是如何工作的,但它们肯定不是异步的,所以你既不需要循环,也不需要第二个using块,它检查你刚刚删除的行是否还在那里。只有删除完所有行后,删除过程才会结束。

你发布的代码也不能是真正的代码,因为你应该得到一个SQL异常,由于无效的语法在你的WHERE子句。

所以你的代码可以缩短为以下(也修复WHERE子句):

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("DOING STUFF");
        string connectionString = @"Data Source=.'wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            string queryString = "DELETE FROM wgcdoccab WHERE serie ='1' tipodoc ='FSS' AND and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120) ";                    
            SqlCommand command = new SqlCommand(queryString, connection);
            command.ExecuteNonQuery();
        }
}

内联查询对DELETESELECT都不正确。更正后的查询如下:

删除查询
DELETE FROM wgcdoccab WHERE serie ='1' AND tipodoc ='FSS' AND contribuinte ='999999990' 
 and  datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120) 

Select查询

SELECT * FROM wgcdoccab WHERE serie ='1' AND tipodoc ='FSS' AND    contribuinte ='999999990' 
and  datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120)