To insert a record in SQL Server CE, you call ..InsertOnSubmit(), passing the class instance that represents the record being inserted, followed by .SubmitChanges();
Do I need a similar construct for updating a record? I've got this:
SQLCEDataContext sqlcedc = new SQLCEDataContext(SQLCEDataContext.DBConnectionString);
var invitations = (from SQLCEDataDefinition invitation in
sqlcedc.SQLCEDataDefinitions
where invitation.SenderID == senderID
select invitation).SingleOrDefault();
invitations.SenderDeviceID = senderDeviceID;
sqlcedc.SubmitChanges();
..but wonder if I need the analogue of InsertOnSubmit() - but there is no UpdateOnSubmit() that I can see. Do I need to user InsertOnSubmit() even though this is an Update operation,not an Insert operation?
windows-phone-8 sql-server-ce update linq
No you don't have an UpdateOnSubmit() and you can't use the InsertOnSubmit() to update your record.
You need to fetch the data first, update your property and then use the SubmitChanges()
Try like this:
SQLCEDataContext sqlcedc = new SQLCEDataContext(SQLCEDataContext.DBConnectionString);
var invitations = (from SQLCEDataDefinition invitation in
sqlcedc.SQLCEDataDefinitions
where invitation.SenderID == senderID
select invitation).SingleOrDefault();
invitations.SenderDeviceID = senderDeviceID;
sqlcedc.SubmitChanges();
Related
I am using the following code to update the UserSession column of the Activities. Following code return the records if the ExpiryTimeStamp is less then current date.
Then it Update the UserSession column to 0 for the returned recods in the table.
.Now I wants that if there are 100 records are returned then these should update at one time instead of using the FoREach. Is it posible in Linq
CacheDataDataContext db = new CacheDataDataContext();
var data = (from p in db.Activities
where p.ExpiryTimeStamp < DateTime.Now
select p).ToList();
data.ForEach(ta => ta.UserSession = "0");
db.SubmitChanges();
In short, no: Linq-2-sql does not do batch updates out of the box.
(I am not sure your foreach will work like you wrote - i do not think so - but this is similar and will work)
foreach (var x in data)
{x.UserSession = "0";}
db.SubmitChanges()
BUT, even if you do it like this, Linq-2-sql will send an update statement for each record to the database. So with your example of 100 records returned you will get 100 individual updates send to the database.
I am implementing the asp.net MVC web application, where i am using the Linq to Sql to manipulate the data in database. but in my one of action, i want to insert multiple table entries which are depends upon each other by referring previous insertion Id's. So i just wnat to know how to handle the transaction, like begin transaction, commit,rollback and all like in ADO.net. how to manage this. what if one of insertion get crashed in the middle of manipulation?
Note:- I am not using the Stored procedures here. I am using Lambda expressions and methods. Also these are use in different manager classes.
Example:
For Create Subject - used method in SubjectManager class to insert subject infor, that returns subject Id. within this subjectid i am inserting the let say its chapters with another method in manager class as ChapterManager. which again returns the ChapterId, on base of this chapeter Id , inserting the Topics of chapter. that again uses Topic manager same like above.in each manger class i am creating dataContext object for the same. and I am controlling all this within a single action in my controller. but worrying about the transaction management. how I can use here ?
The DataContext already includes an embedded transaction object. For example, let's say you are placing a new order for a customer. You can set up your model so that the following code updates both the Customer AND Order table with a single SubmitChanges. As long as a foreign key relationship exists between the two tables, the embedded transaction object handles both the Customer update and the Order insert in the same transaction. Using a TransactionScope object to encase a single DataContext is redundant:
using (DataContext dc = new DataContext())
{
Order order = new Order();
order.ProductID = 283564;
order.Quantity = 7;
order.OrderDate = DateTime.Now;
Customer customer = dc.Customers.Single(c => c.CustomerID == 6);
customer.LastUpdate = order.OrderDate;
customer.Orders.Add(order);
dc.SubmitChanges();
}
using(TransactionScope scope = new TransactionScope())
{
using(DataContext ctx = new MyDataContext())
{
ctx.Subject.Add(subject);
Chapter chapter = new Chapter();
chapter.SubjectId = subject.Id;
ctx.SubmitChanges();
ctx.Chapter.Add(chapter);
ctx.SubmitChanges();
scope.Complete() // if it all worked out
}
}
From the System.Transactions namespace I believe.
I want to get the next identity ID and then log it somewhere. Only after this do I want to call SubmitChanges().
Wrap the DataContext in a database transaction and call SubmitChanges to write changes to the database within that transaction. This way you can get the auto generated ID while being able to keep the operation transactional:
using (var con = new SqlConnection(conStr))
{
con.Open();
using (var tran = con.BeginTransaction())
{
using (var db = new YourDataContext(con))
{
// Setting the transaction is needed in .NET 3.5.
// It's a bug in L2S and was fixed in .NET 4.0.
db.Transaction = tran;
var entity = new MyEntity();
db.MyEntities.InsertOnSubmit(entity);
db.SubmitChanges();
var id = entity.Id;
// Do something useful with this id
}
tran.Commit();
}
}
Wrap the whole thing in a transaction, do a SELECT IDENT_CURRENT('table_name') then submit your changes, the commit the transaction. If you lock the table that should prevent someone else from inserting a record after your SELECT IDENT_CURRENT and before you insert which should give you the correct identity value.
you need to add the lastModified date column in DB and get it,if not and increment the identity column
Other wise better do SubmitChanges() and get the nextID
SQL Server 2008 Ent
ASP.NET MVC 2.0
Linq-to-SQL
I am building a gaming site, that tracks when a particular player (toon) had downed a particular monster (boss). Table looks something like:
int ToonId
int BossId
datetime LastKillTime
I use a 3d party service that gives me back latest information (toon,boss,time).
Now I want to update my database with that new information.
Brute force approach is to do line-by-line upsert. But It looks ugly (code-wise), and probably slow too.
I think better solution would be to insert new data (using temp table?) and then run MERGE statement.
Is it good idea? I know temp tables are "better-to-avoid". Should I create a permanent "temp" table just for this operation?
Or should I just read entire current set (100 rows at most), do merge and put it back from within application?
Any pointers/suggestions are always appreciated.
An ORM is the wrong tool for performing batch operations, and Linq-to-SQL is no exception. In this case I think you have picked the right solution: Store all entries in a temporary table quickly, then do the UPSERT using merge.
The fastest way to store the data to the temporary table is to use SqlBulkCopy to store all data to a table of your choice.
If you're using Linq-to-SQL, upserts aren't that ugly..
foreach (var line in linesFromService) {
var kill = db.Kills.FirstOrDefault(t=>t.ToonId==line.ToonId && t.BossId==line.BossId);
if (kill == null) {
kill = new Kills() { ToonId = line.ToonId, BossId = line.BossId };
db.Kills.InsertOnSubmit(kill);
}
kill.LastKillTime = line.LastKillTime;
}
db.SubmitChanges();
Not a work of art, but nicer than in SQL. Also, with only 100 rows, I wouldn't be too concerned about performance.
Looks like a straight-forward insert.
private ToonModel _db = new ToonModel();
Toon t = new Toon();
t.ToonId = 1;
t.BossId = 2;
t.LastKillTime = DateTime.Now();
_db.Toons.InsertOnSubmit(t);
_db.SubmitChanges();
To update without querying the records first, you can do the following. It will still hit the db once to check if record exists but will not pull the record:
var blob = new Blob { Id = "some id", Value = "some value" }; // Id is primary key (PK)
if (dbContext.Blobs.Contains(blob)) // if blob exists by PK then update
{
// This will update all columns that are not set in 'original' object. For
// this to work, Blob has to have UpdateCheck=Never for all properties except
// for primary keys. This will update the record without querying it first.
dbContext.Blobs.Attach(blob, original: new Blob { Id = blob.Id });
}
else // insert
{
dbContext.Blobs.InsertOnSubmit(blob);
}
dbContext.Blobs.SubmitChanges();
See here for an extension method for this.
I need to update my exist data in mysql database.
I write like this code;
String _id = lbID.Text;
dsrm_usersTableAdapters.rm_usersTableAdapter _t = new dsrm_usersTableAdapters.rm_usersTableAdapter();
dsrm_users _mds = new dsrm_users();
_mds.EnforceConstraints = false;
dsrm_users.rm_usersDataTable _m = _mds.rm_users;
_t.FillBy4(_m, _id);
if(_m.Rows.Count >0 )
{
DataRow _row = _m.Rows[0];
_row.BeginEdit();
_row["username"] = txtUserName.Text;
_row.EndEdit();
_row.AcceptChanges();
_t.Update(_m);
}
But nothing change my exists data. What is the Problem?
I think the problem is that you call DataRow.AcceptChanges() before calling DbDataAdapter.Update(). AcceptChanges will set the status of the datarow to "orignal" (or "not changed" - I don't remeber now). Try to move the call to AcceptChanges to after the Update.
Update requires a valid UpdateCommand when passed DataRow collection with modified rows
Yes I move the AccesptChange() after update bu now its give this error
Update requires a valid UpdateCommand when passed DataRow collection with modified rows
But now problem is, I use MySQL and I can not Wrie UpdateCommand , VS2008 does not accept the SQL command. Automaticly delete all SQL command. I dont understand the problem. So do you now another way without using SQL command (UpdateCommand) ?