Reputation: 120
I was wonder how I'd update a specific well in a database (the balance cell), when all I know is the users account number.
I don't know where about in the database this will be. I just know the account number exists as I've logged in through it.
I need to use LINQ for this, and I've tried querying the database to find the balance. Although, I want to update the balance to a specific value, not find the current value.
In SQL, I'd do something like:
UPDATE UserData
SET Balance = some value in this case an int being passed in
WHERE AccNo = the accountNumber variable stored in a global class
But the problem is the value is stored in a dataset as the client is working offline with the data. His dataset is updated in the SQL database once he logs out. I was going to do this by retrieving everything from the database and going an update/merge statement.
Any idea how to resolve this?
Upvotes: 0
Views: 2299
Reputation: 460168
Linq-To-DataSet
is a subset of Linq-To-Objects
. You can use it to query a DataSet
/DataTable
, but you can't use it to update the rows.
So you could search the relevant DataRows
with LINQ and then use a loop to update them.
DataTable tblUserData = dataSet.Tables["UserData"];
var rowsWithAccount = from row in tblUserData.AsEnumerable()
where row.Field<string>("AccountNumber") == accountNumber // replace `<string>` with the right type
select row;
foreach(DataRow row in rowsWithAccount)
row.SetField("Balance", newBalance);
Once the value is updated you can use a SqlDataAdapter
to update the database:
Updating Data Sources with DataAdapters
Upvotes: 2