Link to home
Start Free TrialLog in
Avatar of Adam D
Adam D

asked on

C# delete from SQL

Hello.

I have some code that I am refactoring but need to keep as much the same as possible.

=====================
DBManager dbMgr = new DBManager();
======================
The above line give me direct access to the tables.  So:

dbMgr.TableName   <<< will give me access to methods like "Select," "Remove," and "RemoveRange" for example.

".Remove" works fine if I ONLY have one record to remove.

My question is how do I remove a resultset that has multiple rows (say 5) without having to loop through and make 5 calls to the database (very inefficient).

How can I, using the DBManager above, send a SQL command that says "DELETE FROM TableName WHERE name = 'Fred'"

Thanks. :)
Avatar of Adam D
Adam D

ASKER

I have also tried this:


=============================
   using (DBManager ctx = new DBManager())
            {
                try
                {
                    var temp = ctx.TableName.Where(x => x.RequisitionID == ReqID);
                    ctx.TableName.RemoveRange(temp);
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
=================================
But I get the exception:

"The object cannot be deleted because it was not found in the ObjectStateManager."

Thanks. :)
Can you show the code to DBManager class? Because it does not look like a framework class.
Avatar of Adam D

ASKER

Sure, nothing exciting, it just initializes the connection.  As I said I get back a list of tables to use (TableName - is obviously not the name of the table :)) and then use the properties/methods connected to them.

This is the constructor:
==================================
 public DBManager()
            : base(CurrentConnectionString)
        {
            Database.SetInitializer<DBManager>(null);
            this.Configuration.ProxyCreationEnabled = false;
        }
==================================

Thanks. :)
ASKER CERTIFIED SOLUTION
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial