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. :)
LVL 1
Adam DIT Solutions DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam DIT Solutions DeveloperAuthor Commented:
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. :)
0
Klavs RDeveloper, owner of AlbusBitCommented:
Can you show the code to DBManager class? Because it does not look like a framework class.
0
Adam DIT Solutions DeveloperAuthor Commented:
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. :)
0
Dirk StraussSenior Full Stack DeveloperCommented:
Did you by any chance use this class? Database manager example on CodeProject. If so, it has built-in methods for accessing the database:
System.Data.DbManager DbMgr = new System.Data.DbManager("System.Data.OleDb", connstring);
DataTable dtUsers = DbMgr.Retrieve("Select * From tblUsers");
String TopUser = DbMgr.Scalar("Select Top 1 UserName From tblUsers").ToString();
DbMgr.Execute("Delete * From tblUsers");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.