从SQL Server删除带有两个变量的记录 [英] delete record from sql server with two variables
问题描述
大家好!
我有一张桌子
item1
item2
item3
item4
item5
从C Sharp,我必须通过传递两个变量来删除一条记录
我使它看起来像是这个问题,并且,我仍然使用逗号,但有错误吗,您能告诉我如何处理它吗?
(从[数据库]中删除.[表] WHERE item1 ="+ Int1 +"和"item2 =" + Int2))
hi to all!
i have a table with
item1
item2
item3
item4
item5
from c sharp i have to delete one record by passing two variables
i make it like this problem is of and, i use comma still there is error can u tell me how to handle it
("DELETE FROM [database].[table] WHERE item1=" + Int1+ " and "item2="+Int2)
推荐答案
尝试以下操作:
Try this:
string delquery="Delete * From [database].[table] Where item1 = ? And item2 = ?";
OleDbCommand cmd = new OleDbCommand(delquery, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("item1", Int1);
cmd.Parameters.AddWithValue("item2", Int2);
conn.Open();
cmd.ExecuteNonQuery();
请记住,始终使用参数化查询或存储过程来避免sql注入攻击.
希望对您有所帮助:)
对于更多查询,请在此处进行注释!
remember always use parametrized query or stored procedures to avoid sql-injection attacks.
hope it helps :)
for further queries comment here!
如果项目是字符串数据类型,则需要在查询中的单引号内添加值,如下所示:
If items are string datatype then you need to add values within single quotes in query like this:
DELETE FROM [database].[table] WHERE item1=''" + Int1+ "'' and item2=''"+Int2+"''"
试试吧.
Try it.
使用以下代码:
(从[数据库]中删除.[表]其中item1 =""+ Int1 +"和item2 =""+ Int2 +"))
并且我建议您使用主键"删除记录
Use following code:
("DELETE FROM [database].[table] WHERE item1=''" + Int1+ "'' and item2=''"+Int2+"'')
and i like to recommend you that delete records using "primary key"
这篇关于从SQL Server删除带有两个变量的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!