delete specific entries in table
In my table I have many rows with data. One row has seven columns with values.
Is it possible to delete from one row values of two columns and rest five leave without changes?
Can I to it with SQL delete?
cmd.CommandText = @"DELETE ImageData,"
+ " ContentType, "
+ " FROM Users "
+ " WHERE UserName = @UserName";
cmd.Parameters.Add(new SqlParameter("@UserName", username));
cmd.Parameters.Add(new SqlParameter("@ImageData", ImageData));
cmd.Parameters.Add(ne开发者_JAVA百科w SqlParameter("@ContentType", ContentType));
In my code I can't delete like this, is it wrong? Does anyone know how to delete them?
I assume it's not DELETE you need but UPDATE.
DELETEalways removes an entire row.UPDATEallows you to change individual columns in a row.
codefragment
@"UPDATE Users "
+ "SET ContentType = NULL, "
+ " ImageData = NULL "
+ "WHERE Username = @UserName";
You need to use an UPDATE statement for this. DELETE is only for deleting whole row(s) at a time.
You can't actually "delete" them as though it was cells in a spreadsheet though. Assuming the columns are nullable you can set them to NULL as below.
cmd.CommandText = @"UPDATE ImageData
SET ContentType = NULL, Users = NULL
WHERE UserName = @UserName";
cmd.Parameters.Add(new SqlParameter("@UserName", username));
you could update the row:
UPDATE Users
SET ImageData = NULL, ContentType = NULL
WHERE UserName = @UserName
加载中,请稍侯......
精彩评论