public static void DeleteThreads(int threadID)
{
StringBuilder sb = new StringBuilder();
sb.Append("DELETE FROM dbo.Threads");
sb.Append(" WHERE ThreadsID=@ThreadsID");
string myConnectionString = AllQuestionsPresented.connectionString;
using (SqlConnection myConnection = new SqlConnection(myConnectionString))
{
myConnection.Open();
SqlCommand sqlCommand = new SqlCommand(sb.ToString(), myConnection);
sqlCommand.Parameters.Add("@ThreadsID", SqlDbType.Int);
sqlCommand.Parameters["@ThreadsID"].Value = threadID;
sqlCommand.ExecuteNonQuery();
}
}
It gives me this Error:
The DELETE statement conflicted with the REFERENCE constraint "FK_Comments_Threads". The conflict occurred in database "model", table "dbo.Comments", column 'ThreadsID'.
The statement has been terminated.
Should this fix that error:
enter code here public static void DeleteComments(int threadID)
{
StringBuilder sb = new StringBuilder();
sb.Append("DELETE FROM dbo.Comments");
sb.Append(" WHERE ThreadsID=@ThreadsID");
string myConnectionString = AllQuestionsPresented.connectionString;
using (SqlConnection myConnection = new SqlConnection(myConnectionString))
{
myConnection.Open();
SqlCommand sqlCommand = new SqlCommand(sb.ToString(), myConnection);
sqlCommand.Parameters.Add("@ThreadsID", SqlDbType.Int);
sqlCommand.Parameters["@ThreadsID"].Value = threadID;
sqlCommand.ExecuteNonQuery();
}
}
You don’t have a space, and it looks like you’re missing the
FROMkeyword.Try:
Also, for the record: Using inline SQL is generally bad due to tightly coupling your application to your database. You should use a separate data access layer if this application is for any serious purpose.