Deleting rows with C#

Joined
Oct 26, 2011
Messages
2
Reaction score
0
Hi, I have problems deleting rows from a database table. I have read on the internet, and adjusted the code but apparently I am missing something, any help greatly appreciated. Below is the vital code that is used:


myCon.Open();
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add(
"tbl6_MONK_LoggedIn");
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType =
CommandType.Text;
myCommand.CommandText =
"SELECT * FROM tbl6_MONK_LoggedIn";

myCommand.Connection = myCon;
myCommand.CommandTimeout = 180;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder cb = new SqlCommandBuilder(myAdapter);


myAdapter.Fill(myDataSet, "tbl6_MONK_LoggedIn");


for ( int i = 0; i < myDataSet.Tables["tbl6_MONK_LoggedIn"].Rows.Count; i++)
{
DataRow myRow = myDataSet.Tables["tbl6_MONK_LoggedIn"].Rows;
myRow.Delete();
myRow.AcceptChanges();

myDataSet.Tables["tbl6_MONK_LoggedIn"].AcceptChanges();
}


myAdapter.Update(myDataSet, "tbl6_MONK_LoggedIn");
myCon.Close();

 
Thanks för all the replys.

Here is the code that fixed it:

myCon.Open();
string sql = "";
sql =
"DELETE FROM tbl6_MONK_LoggedIn";
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.DeleteCommand = myCon.CreateCommand();
myAdapter.DeleteCommand.CommandText = sql;
myAdapter.DeleteCommand.ExecuteNonQuery();
myCon.Close();
 
Back
Top