每次使用调试器时,即使在到达负责将数据保存到数据库的代码行之前停止调试器,也会执行其中一个存储过程。
我怎么能防止这种情况发生?我不想在数据库中有不完整的行。
POrder po = new POrder();
po.PODate = DateTime.Parse(txtPODate.Text); <-- Breakpoint here
po.POVendorID = int.Parse(ddPOVendors.SelectedValue); <-- Stop Debugging here
我还没有到达po.AddNewOrder
我停止调试器,所以我还没有执行存储过程
po.AddNewOrder();
POrder
上课:
public DateTime PODate { get; set; }
public int POVendorID { get; set; }
public void AddNewOrder()
{
SqlConnection con = new SqlConnection(ss.GetConectionString());
SqlCommand cmd = new SqlCommand("uspAddNewPOrder", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parPODate = new SqlParameter("@PODate", SqlDbType.Date);
parPODate.Value = PODate;
cmd.Parameters.Add(parPODate);
SqlParameter parPOVendorID = new SqlParameter("@POVendorID", SqlDbType.Int);
parPOVendorID.Value = POVendorID;
cmd.Parameters.Add(parPOVendorID);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception exp)
{
throw new Exception(exp.Message.ToString());
}
finally
{
cmd.Dispose();
con.Close();
}
}
存储过程代码:
ALTER PROCEDURE [dbo].[uspAddNewPOrder]
(
@PODate datetime = null,
@POVendorID int= null
)
AS
BEGIN
INSERT INTO PurchaseOrders.[dbo].[tblPurchaseOrders]
(
PODate,
POVendorID
)
VALUES
(
@PODate,
@POVendorID
)
END
答案 0 :(得分:1)
您可以使用preprocessor directives
跳过在给定条件下执行的代码段(例如,在您的情况下在调试期间)。看看这个MSDN link
#define DEBUG
public class YourClass
{
POrder po = new POrder();
po.PODate = DateTime.Parse(txtPODate.Text);
Break point here ->
po.POVendorID = int.Parse(ddPOVendors.SelectedValue);
#if (!DEBUG)
po.AddNewOrder(); //this will not be executed during debug.
#endif
....
}