PC Review


Reply
Thread Tools Rate Thread

Deleting rows with C#

 
 
New Member
Join Date: Oct 2011
Posts: 2
 
      26th Oct 2011
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 = newDataSet();
myDataSet.Tables.Add(
"tbl6_MONK_LoggedIn");
SqlCommand myCommand = newSqlCommand();
myCommand.CommandType =
CommandType.Text;
myCommand.CommandText =
"SELECT * FROM tbl6_MONK_LoggedIn";

myCommand.Connection = myCon;
myCommand.CommandTimeout = 180;
SqlDataAdapter myAdapter = newSqlDataAdapter(myCommand);
SqlCommandBuilder cb = newSqlCommandBuilder(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[i];
myRow.Delete();
myRow.AcceptChanges();

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


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

 
Reply With Quote
 
 
 
 
New Member
Join Date: Oct 2011
Posts: 2
 
      2nd Nov 2011
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 = newSqlDataAdapter();
myAdapter.DeleteCommand = myCon.CreateCommand();
myAdapter.DeleteCommand.CommandText = sql;
myAdapter.DeleteCommand.ExecuteNonQuery();
myCon.Close();
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:27 PM.