0

每次使用调试器时,都会执行我的一个存储过程,即使我在到达负责将数据保存到数据库的代码行之前停止调试器也是如此。

我怎样才能防止这种情况发生?我不想在数据库中有不完整的行。

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
4

1 回答 1

1

您可以使用preprocessor directives跳过在给定条件下执行的代码部分(例如,在您的情况下调试期间)。看看这个MSDN 链接

#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
    ....      
}
于 2012-11-16T17:34:10.567 回答