如何在没有密钥的情况下从数据库中删除?

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 05:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定