SQL delete command?
Try removing the single quotes. Also why are you concatenating your SQL string with a connection object (.. word='@word'" + conn
)???
Try like this:
try
{
using (var sc = new SqlConnection(ConnectionString))
using (var cmd = sc.CreateCommand())
{
sc.Open();
cmd.CommandText = "DELETE FROM excludes WHERE word = @word";
cmd.Parameters.AddWithValue("@word", word);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
Box.Text = "SQL error" + e;
}
...
Notice also that because the connection is wrapped in a using block you don't need to Close it in a finally
statement. The Dispose method will automatically call the .Close method which will return the connection to the ADO.NET connection pool so that it can be reused.
Another remark is that this IncludeWord
method does far to many things. It sends SQL queries to delete records, it updates some textboxes on the GUI and it binds some lists => methods like this should be split in separate so that each method has its own specific responsibility. Otherwise this code is simply a nightmare in terms of maintenance. I would very strongly recommend you to write methods that do only a single specific task, otherwise the code quickly becomes a complete mess.
SQL Server - Override DELETE command?
Yes, there is. You can set a INSTEAD OF DELETE trigger, which replaces the standard actions for DELETE commands with your code (in this case it would be an empty code, so it does nothing when someone tries to delete records from that table).
https://technet.microsoft.com/en-us/library/ms191208(v=sql.105).aspx
C# sql delete command
query += "BEGIN TRANSACTION";
query += "DELETE FROM VehicleRentals FROM VehicleRentals INNER JOIN Vehicles N..."
turns into
"BEGIN TRANSACTIONDELETE FROM..."
You need to include the spaces between each line:
query += "BEGIN TRANSACTION";
query += " DELETE FROM VehicleRentals FROM VehicleRentals INNER JOIN Vehicles N..."
And as others have stated, you should use parameters to avoid sql injection.
SQL delete command deletes all entries in the table
In your query [OrderID]=OrderID
means column OrderID equals column OrderID, so every row with not null OrderID is deleted. If you want ot pass parameter to query, you should put @ before it's name.
Try:
string Query = @"DELETE FROM [dbo].[Orders] WHERE [OrderID] = @OrderID;";
Related Topics
Why Must C# Operator Overloads Be Static
Extract Content from Div Tag C# Regex
Where Is Request.Isajaxrequest() in ASP.NET Core MVC
Ruby Equivalent of C# 'Using' Statement
How to Call R Functions from C# and Retrieve The Result in C#
How to Call a .Net Assembly from C/C++
Razor View Page as Email Template
How to Parse an HTML String for Image Tags to Get at The Src Information
How to Display a List of Images, from a Folder on Hard Drive, on ASP.NET Website
Dynamic Form Generation in Asp.Net
Calling a C# Function by a HTML Button
Generate PDF from ASP.NET from Raw HTML/CSS Content
Code with Undefined Behavior in C#
Gridview with Fixed Header and Full Page Width Grid
How to Programmatically Apply a CSS Class to an ASP.NET Control