gpt4 book ai didi

c# - 删除查询不起作用 SQL C#

转载 作者:行者123 更新时间:2023-11-29 22:12:00 26 4
gpt4 key购买 nike

我一直在尝试从表中删除行,但我编写的代码不起作用,我不确定这是 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 block 来检查您刚刚删除的行仍然在那里。只有删除所有行后,删除过程才会结束。

此外,您发布的代码不可能是真正的代码,因为您应该由于 WHERE 子句中的语法无效而收到 SQL 异常。

因此您的代码可以缩短为以下内容(也修复 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();
}
}

关于c# - 删除查询不起作用 SQL C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com