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. :)
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. :)
Can you show the code to DBManager class? Because it does not look like a framework class.
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(CurrentConnectionStri ng)
{
Database.SetInitializer<DB Manager>(n ull);
this.Configuration.ProxyCr eationEnab led = false;
}
========================== ========
Thanks. :)
This is the constructor:
==========================
public DBManager()
: base(CurrentConnectionStri
{
Database.SetInitializer<DB
this.Configuration.ProxyCr
}
==========================
Thanks. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
==========================
using (DBManager ctx = new DBManager())
{
try
{
var temp = ctx.TableName.Where(x => x.RequisitionID == ReqID);
ctx.TableName.RemoveRange(
}
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. :)