在提交之前,在C#网格视图中输出回滚存储过程的结果

本文关键字:输出 存储过程 结果 视图 提交 网格 | 更新日期: 2023-09-27 18:19:35

我有两个完全相同的存储过程,唯一的区别是一个提交插入/更新,而另一个以回滚模式运行。

我正在努力实现的目标

我希望用户填写3个变量,然后单击一个按钮,这个按钮应该设置并执行ROLLBACK版本的存储过程。然后将向用户显示一个"确定/取消"对话框MessageBox。如果数据看起来正常,则用户将从DialogResult中选择OK,如果不正常,则选择Cancel。如果他们确实选择了OK,则此时将执行存储过程的COMMIT版本。

我的问题

目前,在代码中,我已经复制了我为存储过程的提交版本所做的操作。即,一旦改变被疯狂,数据集被刷新并且网格视图被更新。由于存储过程的ROLLBACK版本实际上不会做出任何更改,因此如果用户单击"确定",网格视图永远不会向用户显示数据的样子。

在SSMS中,如果我执行回滚存储过程,它将在ROLLBACK TRAN部分之前显示一条select语句,这基本上向我显示了数据的样子。这是我想要更新数据集的SELECT语句,这样用户就可以在单击OK(提交)之前检查更改

我的问题

是否有在回滚存储过程中使用SELECT语句来更新我的数据集/gridview,如果没有,是否有更改SQLDataAdapter来更新gridview以了解回滚存储过程的事务中数据的外观,我认为我可能需要使用ExecuteReader,但我不确定这将适合我当前的代码。

代码

//Only execute the updated if there is an ID, OldProfileClass and NewProfileClass specified
                    if (recordID.Text != "" && oldProfileClass.Text != "" && newProfileClass.Text != "")
                    {
                        int ID = Convert.ToInt32(recordID.Text);
                        int oldPC = Convert.ToInt32(oldProfileClass.Text);
                        int NewPC = Convert.ToInt32(newProfileClass.Text);
                        string connstrroll = @"Initial Catalog=mytestdb;Data Source=localhost;Integrated Security=SSPI;";
                        SqlConnection connroll = new SqlConnection(connstrroll);
                        connroll.Open();
                        var cmdroll = new SqlCommand("dbo.myrollbacksp", connroll);
                        cmdroll.CommandType = CommandType.StoredProcedure;
                        cmdroll.Parameters.AddWithValue("@meter_id", ID);
                        cmdroll.Parameters.AddWithValue("@new_profile_num", NewPC);
                        cmdroll.Parameters.AddWithValue("@old_profile_num", oldPC);
                        //execute the command 'cmd', the profile class will now be updated at db level
                        cmdroll.ExecuteNonQuery();
                        int numberOfRecordsroll = cmdroll.ExecuteNonQuery();

                        //Once the Profile Class change has been committed, show the results in the gridview
                        using (SqlDataAdapter aroll = new SqlDataAdapter("SELECT cust_ref, (region+meter_num_1+meter_num_2) as Number, meter_id, site_name, profile_num FROM dbo.Meter WHERE meter_id = @filter", conn))
                        {
                            int filter = ID;
                            aroll.SelectCommand.Parameters.AddWithValue("@filter", filter);
                            // Use DataAdapter to fill DataTable
                            DataTable t = new DataTable();
                            aroll.Fill(t);
                            // Render data onto the screen
                            gridSelectID.DataSource = t;
                        }
                        //close connections
                        cmdroll.Dispose();
                        connroll.Close();
                        connroll.Dispose();
                        //confirm update
                        MessageBox.Show("Number of records affected:" + numberOfRecordsroll + " Please check the data is correct before proceeding", "Please validate your changes", MessageBoxButtons.OKCancel);
                        if (DialogResult == DialogResult.OK)
                        {
                            // CODE TO FIRE THE COMMIT VERSION OF STORED PROC GOES HERE
                        }
                        else if (DialogResult == DialogResult.Cancel)
                        {
                            //DONT RUN THE COMMIT VERSION OF THE STORED PROC
                        }
                        //empty the values of the three text box's once the profile class is updated
                        recordID.Text = "";
                        oldProfileClass.Text = "";
                        newProfileClass.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Please provide details for all 3 boxes", "Warning");
                    }

在提交之前,在C#网格视图中输出回滚存储过程的结果

这完成了任务。。。

 //show the 'temporary' results in the gridview, taken from the rollback stored proc
                        using (SqlDataAdapter aroll = new SqlDataAdapter(cmdroll))
                        {
                            // Use DataAdapter to fill DataTable
                            DataTable troll = new DataTable();
                            aroll.Fill(troll);
                            // Render data onto the screen
                            gridSelectID.DataSource = troll;
                        }