【发布时间】:2013-06-24 09:49:45
【问题描述】:
我有两个完全相同的存储过程,唯一的区别是一个提交插入/更新,而另一个在回滚模式下运行。
我正在努力实现的目标。
我希望用户填写 3 个变量然后单击一个按钮,然后该按钮应设置并执行 ROLLBACK 版本的存储过程。然后将向用户显示一个确定/取消对话框消息框。如果数据看起来没问题,那么用户将从DialogResult 中选择“确定”,否则他们选择“取消”。如果他们确实选择了 OK,那么这是 COMMIT 版本的存储过程将执行的时间。
我的问题。
目前在代码中,我复制了我为存储过程的提交版本所做的工作。即一旦更改疯狂,数据集就会刷新并更新网格视图。由于存储过程的ROLLBACK 版本实际上不会进行任何更改,因此gridview 永远不会向用户显示如果他们单击确定,数据将是什么样子。
在 SSMS 中,如果我执行回滚存储过程,它将在 ROLLBACK TRAN 部分之前显示一条选择语句,这实际上向我展示了数据的样子。这是我想要更新数据集的SELECT 语句,以便用户可以在单击确定(提交)之前检查更改
我的问题
是否无论如何在回滚存储过程中使用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# sql gridview stored-procedures