英文:
How can I delete from database without the key?
问题
I have a cart
table that has cartId
as the primary key, userId
and itemId
. I want to delete every row that has the id of a given user.
Let's say this is my table:
cartId | userId | itemID |
---|---|---|
01 | 5 | 111 |
02 | 5 | 112 |
03 | 4 | 111 |
I want to delete the information regarding user 5, so my table will look like this:
cartId | userId | itemID |
---|---|---|
03 | 4 | 111 |
I have tried this SQL:
public static bool DeleteItemsFromCartByUserId(int userId)
{
string sql = string.Format("delete from [cart] where [userId] = {0}", userId);
return Dbf.UpdateTable(sql) == 1;
}
public static int UpdateTable(string sql)
{
OleDbConnection obj = Dbf.GenerateConnection();
OleDbCommand cmd = new OleDbCommand(sql, obj);
int value = cmd.ExecuteNonQuery();
obj.Close();
return value;
}
Every other delete SQL that I wrote works perfectly fine - it returns true and deletes the information, but this one returns false and doesn't delete for some reason.
How can I do it if I don't have the cartId
's?
英文:
I have a cart
table that has cartId
as the primary key, userId
and itemId
. I want to delete every row that has the id of a given user
Let's say this is my table:
cartId | userId | itemID |
---|---|---|
01 | 5 | 111 |
02 | 5 | 112 |
03 | 4 | 111 |
I want to delete the information regarding user 5, so my table will look like this:
cartId | userId | itemID |
---|---|---|
03 | 4 | 111 |
I have trid this SQL:
public static bool DeleteItemsFromCartByUserId(int userId)
{
string sql = string.Format("delete from [cart] where [userId] = {0}", userId);
return Dbf.UpdateTable(sql) == 1;
}
public static int UpdateTable(string sql)
{
OleDbConnection obj = Dbf.GenerateConnection();
OleDbCommand cmd = new OleDbCommand(sql, obj);
int value = cmd.ExecuteNonQuery();
obj.Close();
return value;
}
Every other delete sql that I wrote works perfectly fine - it returns true and deletes the information, but this one returns false and doesn't delete for some reason.
How can I do it if I don't have the cardId's?
答案1
得分: 2
OleDbCommand.ExecuteNonQuery
返回受命令影响的行数。在您的示例中,您正在删除两行,但只检查了一行是否受影响,因此返回值为 FALSE。
一个稍微更好的检查方式是:
return Dbf.UpdateTable(sql) > 0;
然而,您仍然缺少在C#中进行SQL查询的一些最佳实践,包括:
- 使用SQL参数而不是格式化/连接SQL字符串
- 处理连接和命令的释放
- 适当的错误处理
如果您将结果存储在变量中并在调试器中进行检查,而不是将函数调用和比较内联在return
语句中,您可能会更容易发现这些问题。
英文:
OleDbCommand.ExecuteNonQuery
returns the number of rows that were affected by the command. In your example, you are deleting two rows, but only checking if one row is affected, hence the FALSE return value.
A moderately better check would be:
return Dbf.UpdateTable(sql) > 0;
However you are still missing some best practices regarding SQL queries in C#, namely:
- Use of SQL parameters instead of formatting/concatenating SQL strings
- Disposing of connections and commands
- Proper error handling
You probably would have also seen this if you stored the result in a variable and checked it in the debugger rather than inlining the function call and the comparison in a return
statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论