views:

27

answers:

1
public void RemoveTask(int index) {
  SQL = "DELETE FROM Task where (...) = " +index;

  dbConn.Open();

  dbCommand = new SqlCeCommand(SQL, dbConn);
  dbCommand.ExecuteNonQuery();

  dbConn.Close();
}

What i want to do is to delete the record based on the index which specified the row number but I don't know what function or variable should be used ( note the blank ), i try something like rowNum but it does not work.

any help will be appreaciated

A: 

It isn't entirely clear what you are trying to do. I think the following code is what you are after - it deletes a row based on the primary key where in this case the name of the primary key column is TaskId (but you can change that based on your table column names).

Note that it also uses parameterised SQL which gives better performance and security.

SQL = "DELETE FROM Task where TaskId = @taskid"; 

dbConn.Open(); 

dbCommand = new SqlCeCommand(SQL, dbConn); 

dbCommand.Parameters.Add("@taskid", SqlDbType.Int);
dbCommand.Parameters["@taskid"].Value = index;

dbCommand.ExecuteNonQuery(); 

dbConn.Close(); 
David Hall
emm, TaskId will then contain row number ie 1,2,3,4 depending on how many columns we have, but how do I create TaskId , I tried using identity but then I need to reset the seed each time I delete one record but the 'DBCC CHECKIDENT' does not seems to work as well
Oww, I am using sql server compact