我的更新查询做错了什么?
我正在尝试使用 .NET 的 DataAdapter 更新一组查询。这是我正在做的事情的简化版本:
//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 = receiptsUpdateCommand.Parameters.Add("@isDeposited", SqlDbType.Bit, 1, "isDeposited");
depositiedParam.SourceVersion = DataRowVersion.Original;
receiptsAdapter.UpdateCommand = receiptsUpdateCommand;
receiptsAdapter.Update(receipts);
}
但是,我发现receiptsAdapter.Update(receipts);实际上并不会导致数据库被更新。我做错了什么?
一种简化的编写方式就是执行以下 sql 命令: UPDATE tblCashReceipts SET isDeposited = 1 WHERE {my Clause}
但我想学习如何使用 ADO.NET 的东西。
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 = receiptsUpdateCommand.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面一行有问题,
一定是
below line is having problem
it must be