0

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

我想要达到的目标。

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

我的问题。

目前在代码中我已经复制了我为存储过程的提交版本所做的事情。即,一旦更改疯狂,数据集就会刷新,gridview 也会更新。由于ROLLBACK存储过程的版本实际上不会进行任何更改,因此 gridview 永远不会向用户显示如果他们单击确定,数据将是什么样子。

在 SSMS 中,如果我执行回滚存储过程,它将在ROLLBACK TRAN部件之前显示一条选择语句,这实际上向我展示了数据的外观。我想用它SELECT来更新我的数据集,以便用户可以在单击“确定”(提交)之前检查更改

我的问题

无论如何使用SELECT回滚存储过程中的语句来更新我的数据集/网格视图,如果没有的话,是否有任何改变我SQLDataAdapter来更新网格视图与数据在回滚存储过程的事务中的外观,我想我可能需要使用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");
                    }
4

1 回答 1

0

这完成了工作......

 //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;
                        }
于 2013-06-24T12:15:24.697 回答