我的更新查询做错了什么?

发布于 2024-10-06 17:48:00 字数 2548 浏览 3 评论 0原文

我正在尝试使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-10-13 17:48:00

下面一行有问题,

depositiedParam.SourceVersion = DataRowVersion.Original;

一定是

depositiedParam.SourceVersion = DataRowVersion.Current;

below line is having problem

depositiedParam.SourceVersion = DataRowVersion.Original;

it must be

depositiedParam.SourceVersion = DataRowVersion.Current;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文