• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 121
  • Last Modified:

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. :)
0
Adam D
Asked:
Adam D
  • 2
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now