C# & SQL Server : inserting/update only truly executes when debugging

前端 未结 2 1367
执念已碎
执念已碎 2021-01-23 18:07

I have a rather simplistic API for adding, and then later updating a small table in SQL Server. It will insert (or update, which exhibits the same odd behavior) when I step into

2条回答
  •  广开言路
    2021-01-23 18:58

    Edited to correct mistakenly calling this an async method - see comments.


    You are calling the method BeginExecuteNonQuery which initiates a query, but you must call EndExecuteNonQuery to complete the operation (generally asynchronously). When you debug, this has a chance to complete as you manually step through. When you step over, the connection is disposed prior to completing.

    You will need to either update your code to use an Asynchronous Programming Model (APM) pattern or call the synchronous ExecuteNonQuery instead.

提交回复
热议问题