.net 和UserName=@UserName”,new{Datasource=pendingAttachment.Datasource,pendingAttachment.LastUpdated,pendingAttachment.PendingCount,pe
.net 和UserName=@UserName”,new{Datasource=pendingAttachment.Datasource,pendingAttachment.LastUpdated,pendingAttachment.PendingCount,pe,.net,sqlparameter,executescalar,.net,Sqlparameter,Executescalar,和UserName=@UserName”,new{Datasource=pendingAttachment.Datasource,pendingAttachment.LastUpdated,pendingAttachment.PendingCount,pendingAttachment.DeviceId,UserName=pendingAttachment.UserName});我这样做了,所以在执行foreach()时,您不会尝试使用已经声明的参数但是…这不是它所做的;它所做的是:从命令中删除
和UserName=@UserName”,new{Datasource=pendingAttachment.Datasource,pendingAttachment.LastUpdated,pendingAttachment.PendingCount,pendingAttachment.DeviceId,UserName=pendingAttachment.UserName});我这样做了,所以在执行foreach()时,您不会尝试使用已经声明的参数但是…这不是它所做的;它所做的是:从命令中删除所有参数,所以:零参数;还有-为什么它在没有执行循环中的某些内容的情况下循环附件?我清除了,这是一个新错误:+ex{“参数化查询”(@Datasource nvarchar(4000),@LastUpdated nvarchar(4000),@Pending'需要未提供的参数“@Datasource”。}System.Exception{System.Data.SqlClient.SqlException}
public BaseResponse Post([FromBody]List<PendingAttachment> pendingAttachmentRequest)
{
PendingAttachment pendingAttachment = new PendingAttachment();
List<SqlParameter> sqlParameters = new List<SqlParameter>()
{
new SqlParameter("@Datasource", pendingAttachment.DataSource),
new SqlParameter("@LastUpdated", pendingAttachment.LastUpdated),
new SqlParameter("@PendingCount", pendingAttachment.PendingCount),
new SqlParameter("@DeviceId", pendingAttachment.DeviceId),
new SqlParameter("@Username", pendingAttachment.UserName),
};
try
{
RequestHeaders headers = new RequestHeaders();
var query = "SELECT count(*) FROM PendingAttachments WHERE DeviceId = @DeviceId AND UserName = @UserName";
using (var onbaseConnection = MobileCompleteServer.Helpers.OnbaseAuth.Connect(headers))
{
var connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (SqlCommand comm = new SqlCommand(query, sqlConnection))
{
comm.CommandType = System.Data.CommandType.Text;
foreach(PendingAttachment attachment in pendingAttachmentRequest)
{
comm.Parameters.AddRange(sqlParameters.ToArray());
comm.Parameters.Clear();
}
int rowCount = (int)comm.ExecuteScalar(); //FAILS HERE
if (rowCount > 0)
{
using (SqlCommand sqlComm = new SqlCommand("sp_UpdatePendingAttachments", sqlConnection))
{
sqlComm.CommandType = System.Data.CommandType.StoredProcedure;
foreach (PendingAttachment attachment in pendingAttachmentRequest)
{
sqlComm.Parameters.AddRange(sqlParameters.ToArray());
sqlComm.Parameters.Clear();
}
sqlComm.ExecuteNonQuery();
}
}
else
{
using (SqlCommand sqlCommand = new SqlCommand("sp_InsertPendingAttachments", sqlConnection))
{
sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
foreach(PendingAttachment attachment in pendingAttachmentRequest)
{
sqlCommand.Parameters.AddRange(sqlParameters.ToArray());
sqlCommand.Parameters.Clear();
}
sqlCommand.ExecuteNonQuery();
}
}
}
}
return new BaseResponse();
}
}
catch (Exception ex)
{
return new BaseResponse
{
Exception = ErrorCodes.Get(ex, ErrorCodes.PendingAttachmentError),
ExceptionStackTrace = ex.ToString()
};
}
}
}