Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 即使记录在表中可用,也无法删除该记录,[除else条件外,所有条件均正常运行]_C#_Sql Server_Winforms - Fatal编程技术网

C# 即使记录在表中可用,也无法删除该记录,[除else条件外,所有条件均正常运行]

C# 即使记录在表中可用,也无法删除该记录,[除else条件外,所有条件均正常运行],c#,sql-server,winforms,C#,Sql Server,Winforms,这是我的密码, 否则条件将不执行 SqlCommand cmd = new SqlCommand(@"DELETE FROM Demo_Table WHERE (ID = '"+textBox1.Text+"')",con); cmd.ExecuteNonQuery(); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); count = Convert.ToI

这是我的密码, 否则条件将不执行

SqlCommand cmd = new SqlCommand(@"DELETE FROM Demo_Table WHERE (ID = '"+textBox1.Text+"')",con);

cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);

count = Convert.ToInt32(dt.Rows.Count.ToString());

if ((textBox1.Text == string.Empty) && (textBox2.Text == string.Empty))
{
    MessageBox.Show("Provide ID and PASSWORD");
    textBox1.Clear();
    textBox2.Clear();
    textBox1.Focus();
}
else if (textBox1.Text == string.Empty)
{
    MessageBox.Show("select ID to delete record");
    textBox2.Clear();
    textBox1.Focus();
}
else if (count == 0)
{
    MessageBox.Show("wrong ID");
}
else
{
    MessageBox.Show("Record Deleted");
}

请帮助………

ID是一个整数或数字数据类型,用dB表示,但where子句要求它是字母数字

试试这个:

SqlCommand cmd = new SqlCommand(@"DELETE FROM Demo_Table WHERE ID = 1",con); 

专业提示:始终使用参数化命令:

ID是一个整数或数字数据类型,以dB为单位,但where子句要求它是字母数字

试试这个:

SqlCommand cmd = new SqlCommand(@"DELETE FROM Demo_Table WHERE ID = 1",con); 

专业提示:始终使用参数化命令:

好的,看看这个示例。注释用代码表示。基本上,在检查了
文本框之后,我移动了
SQL
命令。另外,由于可能的
SQL
注入,添加了参数。最后一件事是使用数据适配器
Fill
方法,这就是为什么会出现错误

SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
这两行试图用删除的结果填充
DataTable
。由于
DataTable
不能以这种方式填充,因此
count
变量将始终为0

而不是使用
Adapter.Fill
使用将返回受影响行数的
ExecuteNonQuery

//first check for user input
if ((textBox1.Text == string.Empty) && (textBox2.Text == string.Empty))
{
    MessageBox.Show("Provide ID and PASSWORD");
    textBox1.Clear();
    textBox2.Clear();
    textBox1.Focus();
    //exit
    return;
}
//then another check
else if (textBox1.Text == string.Empty)
{
    MessageBox.Show("select ID to delete record");
    textBox2.Clear();
    textBox1.Focus();
    //exit from method
    return;
}

//since code went to this line, execute deletion
//no need to use adapter because this command won't fill anything

SqlCommand cmd = new SqlCommand(@"DELETE FROM Demo_Table WHERE (ID = @ID)",con);
//Please use parameters because string concatenation is BAD BAD BAD!
cmd.Parameters.AddWithValue("@ID", int.Parse(textBox1.Text));

//execute query. Count will get number of effected rows (you want it to be 1)
count = cmd.ExecuteNonQuery();

if (count == 0)
{
    MessageBox.Show("wrong ID");
}
else
{
    MessageBox.Show("Record Deleted");
}

对于SQL注入,请看

好的,请看这个示例。注释用代码表示。基本上,在检查了
文本框之后,我移动了
SQL
命令。另外,由于可能的
SQL
注入,添加了参数。最后一件事是使用数据适配器
Fill
方法,这就是为什么会出现错误

SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
这两行试图用删除的结果填充
DataTable
。由于
DataTable
不能以这种方式填充,因此
count
变量将始终为0

而不是使用
Adapter.Fill
使用将返回受影响行数的
ExecuteNonQuery

//first check for user input
if ((textBox1.Text == string.Empty) && (textBox2.Text == string.Empty))
{
    MessageBox.Show("Provide ID and PASSWORD");
    textBox1.Clear();
    textBox2.Clear();
    textBox1.Focus();
    //exit
    return;
}
//then another check
else if (textBox1.Text == string.Empty)
{
    MessageBox.Show("select ID to delete record");
    textBox2.Clear();
    textBox1.Focus();
    //exit from method
    return;
}

//since code went to this line, execute deletion
//no need to use adapter because this command won't fill anything

SqlCommand cmd = new SqlCommand(@"DELETE FROM Demo_Table WHERE (ID = @ID)",con);
//Please use parameters because string concatenation is BAD BAD BAD!
cmd.Parameters.AddWithValue("@ID", int.Parse(textBox1.Text));

//execute query. Count will get number of effected rows (you want it to be 1)
count = cmd.ExecuteNonQuery();

if (count == 0)
{
    MessageBox.Show("wrong ID");
}
else
{
    MessageBox.Show("Record Deleted");
}

对于SQL注入,请看一下

我认为计数应该是查询中受影响的行数,而不是网格中的行数

count = cmd.ExecuteNonQuery();
//count = Convert.ToInt32(dt.Rows.Count.ToString()); 

我认为计数应该是查询中受影响的行数,而不是网格中的行数

count = cmd.ExecuteNonQuery();
//count = Convert.ToInt32(dt.Rows.Count.ToString()); 

你试过调试器了吗?你的信息框显示了吗?上面写了什么?如果有,还有什么?“错误的ID”?所有条件都正常运行,除其他部分外,我试图删除现有记录,但它显示错误的ID您是否尝试过调试器?你的信息框显示了吗?上面写了什么?如果有,还有什么?“错误的I'd”?所有条件都正常运行,除了其他部分,我试图删除现有记录,但它表示错误的idi尝试了相同的代码,但在第sda行中仍然出现错误。Fill(dt)I声明必须声明标量变量“@ID”@KalpeshPatil,您根本不应该使用
SqlDataAdapter
。复制那大块代码而不是你的代码,这应该可以做到。谢谢,但仍然有错误,它显示错误的ID,即使它存在于database@KalpeshPatil请说得更具体些;我们都在尽力帮忙。您收到的信息显示“错误的ID”。您在文本框中输入的是哪个ID?您确定具有该ID的记录存在吗。是您的ID号还是文本还是…?是的,ID存在于DB中,它是数字,但是当我点击delete按钮删除现有ID时,我得到了错误“error ID”,我尝试了相同的代码,但在sda行中仍然得到了错误。Fill(dt)我声明必须声明标量变量“@ID”@KalpeshPatil,您根本不应该使用
SqlDataAdapter
。复制那大块代码而不是你的代码,这应该可以做到。谢谢,但仍然有错误,它显示错误的ID,即使它存在于database@KalpeshPatil请说得更具体些;我们都在尽力帮忙。您收到的信息显示“错误的ID”。您在文本框中输入的是哪个ID?您确定具有该ID的记录存在吗。是您的ID号或文本还是…?是的,该ID存在于DB中,它是数字,但当我点击删除按钮删除该现有ID时,我得到错误“错误ID”