Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在调试时防止执行存储过程_C#_Asp.net_Sql - Fatal编程技术网

C# 在调试时防止执行存储过程

C# 在调试时防止执行存储过程,c#,asp.net,sql,C#,Asp.net,Sql,每次使用调试器时,都会执行一个存储过程,即使在到达负责将数据保存到数据库的代码行之前停止调试器也是如此 我怎样才能防止这种情况发生?我不希望数据库中有不完整的行 POrder po = new POrder(); po.PODate = DateTime.Parse(txtPODate.Text); <-- Breakpoint here po.POVendorID = int.Parse(ddPOVendors.SelectedValue); <-- St

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

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

POrder po = new POrder();          
po.PODate = DateTime.Parse(txtPODate.Text);  <-- Breakpoint here

po.POVendorID = int.Parse(ddPOVendors.SelectedValue);  <-- Stop Debugging here 
POrder
class:

    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

您可以使用
预处理器指令
跳过给定条件下执行的代码部分(例如,在您的案例中调试期间)。看看这个


将调用包装到事务中?可能发生的情况是,您的浏览器与
ASP.NET开发服务器一起保持打开状态,即使您停止调试浏览器,也会完成它的请求。@Bob Vale谢谢,我怎么能这样做?停止运行调试程序是有区别的(通过点击F5或解除调试器的跟踪)实际上是终止您正在调试的会话。您必须结束服务器进程,以防止请求完成和其他代码执行。我不确定这是他要求的……他说当他调试时,他会遇到问题,并在调用
po.AddNewOrd之前停止调试程序er()
,但该行仍在执行。我想他希望在调试时执行该行,如果他允许它到达该行。@Kaf感谢这是非常有用的,它会解决问题,但我正在测试不同的用户行为,实际上在某个点上我希望使用该行并执行。
ALTER PROCEDURE [dbo].[uspAddNewPOrder] 
(
        @PODate datetime = null,
        @POVendorID int= null
)
AS
BEGIN
    INSERT INTO PurchaseOrders.[dbo].[tblPurchaseOrders]
    (
       PODate,
       POVendorID

    )
    VALUES
    (
       @PODate,
       @POVendorID
    )
END
#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
    ....      
}