0

I Created two Stored Procedure one for reading Datarows and One for deleting, but I was wondering If I can delete records while reading them and also if its best practice ?

e.g.

while (rdr.Read())
{
    If(rdr[abc].ToString() != Null)
    {Maybe delete it ?}
}

My question is, Can I delete a row while reading it ? :)

fdgfdgs dfg
  • 619
  • 4
  • 11
  • 22

2 Answers2

1

Basically, sql server do not allow you to execute two store procedures simultaneously. I think you can refer to link How can I run stored procedures in parallel

Community
  • 1
  • 1
cat_minhv0
  • 1,326
  • 10
  • 18
1

This is what you can do with c#

DataTable table = GetTable(); // Get the data table.
    foreach (DataRow row in table.Rows) // Loop over the rows.
    {

        string getvalue= row[1].ToString();
        If( getvalue == your condition || getvalue == DBNull.Value)
            {
                table.Row.Remove(row[1]);
            }
}   }

Have you tried this

 SqlCommand cmd = new SqlCommand("select * from yourtable", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        { 
            string value=dr[0].ToString();
            if (value == "yourcondition")
            {              
                SqlCommand cmd2 = new SqlCommand("delete from yourtable where columnname ='"+value+"'", con2);
                con2.Open();
                cmd2.ExecuteNonQuery();
                con2.Close();
            }
        } dr.Close();
        con.Close();
Satinder singh
  • 9,340
  • 15
  • 53
  • 93