我一直在尝试从表中删除行但是我编写的代码不起作用,我不确定它是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);
答案 0 :(得分:0)
DELETE
和SELECT
的内联查询不正确。更正的查询将如下所示。
删除查询
DELETE FROM wgcdoccab WHERE serie ='1' AND tipodoc ='FSS' AND contribuinte ='999999990'
and datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120)
选择查询
SELECT * FROM wgcdoccab WHERE serie ='1' AND tipodoc ='FSS' AND contribuinte ='999999990'
and datadoc = CONVERT(varchar(10),(dateadd(dd, -2, getdate())),120)
答案 1 :(得分:0)
我不确定您认为SQL命令如何工作,但它们肯定不是异步的,因此您既不需要循环也不需要第二个using
块来检查您刚刚删除的行是否仍然存在。只有在删除所有行后,删除过程才会结束。
您发布的代码也不是真正的代码,因为您的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();
}
}