C#&放大器; SQL服务器:插入/更新调试时,只有真正执行 [英] C# & SQL Server : inserting/update only truly executes when debugging

查看:96
本文介绍了C#&放大器; SQL服务器:插入/更新调试时,只有真正执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有加,再后来在SQL Server更新一张小桌子一个相当简单的API。它会插入(或更新的,它表现出同样的古怪行为),当我踏进我的数据访问代码的调试器,而不是当我跨过这相同的代码。当然,我错过了一些东西明显...

  INT结果= 0; 
常量字符串的sql =INSERT INTO MyProgress(ImportJobId,SITECODE,ProcessedAmount,TotalToProcess,的ErrorCount,CreatedDateTime,LastModifiedDateTime)+
VALUES(@importJobId,@siteCode,@currentProgress,@totalPieces,0, GETDATE(),GETDATE());

使用(VAR连接= GetSqlConnection())使用
(VAR SQLCMD = GetSqlCommand(连接,SQL))
{
sqlCmd.Parameters.AddWithValue(@ importJobId,importJobId);
sqlCmd.Parameters.AddWithValue(@ SITECODE,SITECODE);
sqlCmd.Parameters.AddWithValue(@ currentProgress,currentProgress);
sqlCmd.Parameters.AddWithValue(@ totalPieces,totalPieces);

//的SqlParameter outputParameter =新的SqlParameter(@ ID中,SqlDbType.Int,4);
//outputParameter.Direction = ParameterDirection.InputOutput;
//sqlCmd.Parameters.Add(outputParameter);

sqlCmd.BeginExecuteNonQuery();
//结果=(INT)(outputParameter.Value ?? -1);
}

返回结果;

有关好奇, GetSqlConnection()的外观像这样的:

  SqlConnection的连接=新的SqlConnection(_ConnectionString); 
connection.Open();
返回连接;


解决方案

BeginExecuteNonQuery()是一个异步方法。执行该命令前,将立即返回。等待它完成,使用的 EndExecuteNonQuery 方法。




当你调用BeginExecuteNonQuery执行Transact-SQL语句,必须调用EndExecuteNonQuery才能完成操作。如果执行命令的进程尚未完成,这种方法阻止,直到操作完成。




看来你正在寻找同步方法。如果是这样的情况下,只需使用



  sqlCmd.ExecuteNonQuery(); 

这一个在执行命令后才会回来。



如果你想异步执行的命令,它更容易使用的 ExecuteNonQueryAsync 方法。

 等待sqlCmd.ExecuteNonQueryAsync(); 

请注意,你必须让你的方法异步使用等待。您还需要.NET 4.5+使用它。


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 my data access code in the debugger, but not when I step over that same code. Surely, I've missed something obvious...

int result = 0;
const string sql = "INSERT into MyProgress (ImportJobId, SiteCode, ProcessedAmount, TotalToProcess, ErrorCount, CreatedDateTime, LastModifiedDateTime) " +
                   "VALUES (@importJobId, @siteCode, @currentProgress, @totalPieces, 0, getdate(), getdate())";

using (var connection = GetSqlConnection())
using (var sqlCmd = GetSqlCommand(connection, sql))
{
    sqlCmd.Parameters.AddWithValue("@importJobId", importJobId);
    sqlCmd.Parameters.AddWithValue("@siteCode", siteCode);
    sqlCmd.Parameters.AddWithValue("@currentProgress", currentProgress);
    sqlCmd.Parameters.AddWithValue("@totalPieces", totalPieces);

    //SqlParameter outputParameter = new SqlParameter("@ID", SqlDbType.Int, 4);
    //outputParameter.Direction = ParameterDirection.InputOutput;
    //sqlCmd.Parameters.Add(outputParameter);

    sqlCmd.BeginExecuteNonQuery();
    //result = (int) (outputParameter.Value ?? -1);
}

return result;

For the curious, GetSqlConnection() looks like this:

SqlConnection connection = new SqlConnection(_ConnectionString);
connection.Open();
return connection;

解决方案

BeginExecuteNonQuery() is an asynchronous method. It will return immediately, before the command is executed. To wait for it to complete, use EndExecuteNonQuery method.

When you call BeginExecuteNonQuery to execute a Transact-SQL statement, you must call EndExecuteNonQuery in order to complete the operation. If the process of executing the command has not yet finished, this method blocks until the operation is complete.

It seems you're looking for synchronous method. If that's the case, simply use

sqlCmd.ExecuteNonQuery();

This one will return only after the command is executed.

If you want to execute the command asynchronously, it's much easier to use the ExecuteNonQueryAsync method.

await sqlCmd.ExecuteNonQueryAsync();

Note that you will have to make your method async to use await. You also need .NET 4.5+ to use it.

这篇关于C#&放大器; SQL服务器:插入/更新调试时,只有真正执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆