How to Delete a row using DeleteCommand of DataAdapter?
-
Hii i m trying to delete a row using DeleteCommand of DataAdapter, but i can't. i m using this code.. using System; using System.Data; using System.Data.SqlClient; class ModifyDataTable { static void Main() { string constr = @"server=(local)\NetSDK;Integrated Security=true;"+ "database=address"; SqlConnection Conn = new SqlConnection(constr); try { string SQL = "SELECT * from Hello"; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(SQL,Conn); DataSet ds = new DataSet(); da.Fill(ds,"Hello"); DataTable dt = ds.Tables ["Hello"]; ("Select * From Hello",Conn); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } Console.WriteLine("\n" + dt.Rows .Count.ToString ()); da.DeleteCommand = new SqlCommand ("DELETE FROM Hello WHERE (Name='Amit')" ,Conn); da.Update (ds,"Hello"); Console.WriteLine ("\n" + dt.Rows.Count.ToString ()); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } } catch (Exception e) { Console.WriteLine ("Error Occured : " +e.Message ); Conn.Close (); } finally { Conn.Close(); Console.ReadLine (); } } } This isn't deleting anything neither in the dataset nor in the database
-
Hii i m trying to delete a row using DeleteCommand of DataAdapter, but i can't. i m using this code.. using System; using System.Data; using System.Data.SqlClient; class ModifyDataTable { static void Main() { string constr = @"server=(local)\NetSDK;Integrated Security=true;"+ "database=address"; SqlConnection Conn = new SqlConnection(constr); try { string SQL = "SELECT * from Hello"; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(SQL,Conn); DataSet ds = new DataSet(); da.Fill(ds,"Hello"); DataTable dt = ds.Tables ["Hello"]; ("Select * From Hello",Conn); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } Console.WriteLine("\n" + dt.Rows .Count.ToString ()); da.DeleteCommand = new SqlCommand ("DELETE FROM Hello WHERE (Name='Amit')" ,Conn); da.Update (ds,"Hello"); Console.WriteLine ("\n" + dt.Rows.Count.ToString ()); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } } catch (Exception e) { Console.WriteLine ("Error Occured : " +e.Message ); Conn.Close (); } finally { Conn.Close(); Console.ReadLine (); } } } This isn't deleting anything neither in the dataset nor in the database
You should use a
DataAdapter
when you want your updates and deletes to be managed for you depending on the state of theDataSet
. In your case, you are not altering the DataSet, so your delete command is never used. If you just want to execute a specific delete command:string sql = "DELETE FROM Hello WHERE (Name='Amit');
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();Charlie if(!curlies){ return; }