I am trying to update a set of queries using .NET's DataAdapter. Here's a simplified version of what I'm doing:
//get all transactions that need to be made
String sql = "SELECT r.ID, r.[Check], r.Cash, r.Coin, r.TenantID, t.TenantName, r.PropertyID, u.UnitNumber, r.ReceivedFrom, r.isDeposited FROM tblCashReceipts r " + //I don't actually think all this is needed, if nessecary I can go back and remove unnessecary selections
"LEFT JOIN tblTenant t " +
"ON t.ID = r.TenantID " +
"LEFT JOIN tblProperty p " +
"ON p.ID = r.PropertyID " +
"LEFT JOIN tblRentalUnit u " +
"ON t.UnitID = u.id " +
"WHERE p.CheckbookID = " + checkbookId;
//populate the data table
DataTable receipts = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString)) {
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
try {
adapter.Fill(receipts);
} catch (Exception ex) {
MessageBox.Show(ex.Message);
} finally {
conn.Close();
}
}
//update the row
foreach (DataRow row in receipts.Rows) {
//no longer removing, it will be left entact with the hidden tblCashReceipt row
row["isDeposited"] = true;
}
//now make the database reflect our changes to the tblCashReceiptes
using (SqlConnection conn = new SqlConnection(connectionString)) {
SqlDataAdapter receiptsAdapter = new SqlDataAdapter("SELECT ID FROM tblCashReceipts", connectionString);
//create delete command
conn.Open();
SqlCommand receiptsUpdateCommand = new SqlCommand("UPDATE tblCashReceipts SET isDeposited = @isDeposited WHERE ID = @ID", conn);
SqlParameter idParam = receiptsUpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 5, "ID");
idParam.SourceVersion = DataRowVersion.Original;
SqlParameter depositiedParam = recei开发者_如何学CptsUpdateCommand.Parameters.Add("@isDeposited", SqlDbType.Bit, 1, "isDeposited");
depositiedParam.SourceVersion = DataRowVersion.Original;
receiptsAdapter.UpdateCommand = receiptsUpdateCommand;
receiptsAdapter.Update(receipts);
}
However, I find that the receiptsAdapter.Update(receipts); doesn't actually result in the database being updated. What am I doing wrong?
A simplified way of writing this would be just to execute the sql command of: UPDATE tblCashReceipts SET isDeposited = 1 WHERE {my clause}
But I want to learn how to use ADO.NET stuff.
below line is having problem
depositiedParam.SourceVersion = DataRowVersion.Original;
it must be
depositiedParam.SourceVersion = DataRowVersion.Current;
精彩评论