C# SqlCommand.ExecuteOnQuery返回1,但未在数据库中插入行

C# SqlCommand.ExecuteOnQuery返回1,但未在数据库中插入行,c#,sql,sql-server,asp.net-mvc,.net-core,C#,Sql,Sql Server,Asp.net Mvc,.net Core,我今天面临这个问题。我用.NETCore制作了一个API,并创建了一个将记录插入SQLServer数据库的函数 SqlCommand.ExecuteNonQuery返回1,但当我直接从SQLServerManagementStudio进行选择时,返回0行。准确地说,SELECT只返回使用SQL Server Management Studio手动插入的行 如果我尝试从SQLServerManagementStudio插入另一行,我可以看到主键(IDautoincrement)正在增加,即使我没有

我今天面临这个问题。我用.NETCore制作了一个API,并创建了一个将记录插入SQLServer数据库的函数

SqlCommand.ExecuteNonQuery
返回1,但当我直接从SQLServerManagementStudio进行
选择时,返回0行。准确地说,
SELECT
只返回使用SQL Server Management Studio手动插入的行

如果我尝试从SQLServerManagementStudio插入另一行,我可以看到主键(
ID
autoincrement)正在增加,即使我没有该行

我的代码:

public async Task<bool> InsertAuditTrail(AuditTrailModel auditTrail)
{
        bool result = false;
        string commandText = String.Concat(@"INSERT INTO [dbo].[AuditTrail] ", 
                                                   "([NomeApplicazione],[N_Applicazione],[N_Record],[NomeTabella],[NomeCampo],[Utente],[Data],[Insert],[Delete],[Update],[DatoPrecedente],[DatoAggiornato]) ",
                                            "VALUES (@NomeApplicazione, @N_Applicazione, @N_Record, @NomeTabella, @NomeCampo, @Utente, @Data, @Insert, @Delete, @Update, @DatoPrecedente, @DatoAggiornato)");
                  

        using (var connection = new SqlConnection(connStr))
        {
            await connection.OpenAsync(); 
            using (var transaction = connection.BeginTransaction())
            {
                using (var command = new SqlCommand(commandText, connection, transaction))
                {
                    try
                    {
                        command.CommandType = CommandType.Text;
                        command.Parameters.AddWithValue("@NomeApplicazione", auditTrail.NomeApplicazione);
                        command.Parameters.AddWithValue("@N_Applicazione", auditTrail.N_Applicazione);
                        command.Parameters.AddWithValue("@N_Record", auditTrail.N_Record);
                        command.Parameters.AddWithValue("@NomeTabella", auditTrail.NomeTabella);
                        command.Parameters.AddWithValue("@NomeCampo", auditTrail.NomeCampo);
                        command.Parameters.AddWithValue("@Utente", auditTrail.Utente);
                        command.Parameters.AddWithValue("@Data", auditTrail.Data);
                        command.Parameters.AddWithValue("@Insert", auditTrail.Insert);
                        command.Parameters.AddWithValue("@Delete", auditTrail.Delete);
                        command.Parameters.AddWithValue("@Update", auditTrail.Update);
                        command.Parameters.AddWithValue("@DatoPrecedente", auditTrail.DatoPrecedente);
                        command.Parameters.AddWithValue("@DatoAggiornato", auditTrail.DatoAggiornato);

                        result = command.ExecuteNonQuery() > 0 ? true : false;
                    }
                    catch (Exception Ex)
                    {
                        await connection.CloseAsync();
                        errorMsg = Ex.Message.ToString();                                               
                    }
                }
            }                          
        }    

        return result;
}
public异步任务InsertAuditTrail(AuditTrailModel auditTrail)
{
布尔结果=假;
string commandText=string.Concat(@“插入[dbo].[AuditTrail]”,
“([NomeApplicatione]、[N_Applicatione]、[N_Record]、[NomeTabella]、[NomeCampo]、[Utente]、[Data]、[Insert]、[Delete]、[Update]、[DatoPresente]、[DatoAggiornato])”,
“值(@nomeapplicatione、@N_applicatione、@N_Record、@NomeTabella、@NomeCampo、@utete、@Data、@Insert、@Delete、@Update、@DatoPrecedente、@DatoAggiornato)”;
使用(var连接=新的SqlConnection(connStr))
{
等待连接。OpenAsync();
使用(var transaction=connection.BeginTransaction())
{
使用(var命令=新的SqlCommand(commandText、connection、transaction))
{
尝试
{
command.CommandType=CommandType.Text;
command.Parameters.AddWithValue(“@nomeApplicatione”,auditTrail.nomeApplicatione);
command.Parameters.AddWithValue(“@N_applicatione”,auditTrail.N_applicatione);
command.Parameters.AddWithValue(“@N_记录”,auditTrail.N_记录);
command.Parameters.AddWithValue(“@NomeTabella”,auditTrail.NomeTabella);
command.Parameters.AddWithValue(“@NomeCampo”,auditTrail.NomeCampo);
command.Parameters.AddWithValue(“@utete”,auditTrail.utete);
command.Parameters.AddWithValue(“@Data”,auditTrail.Data);
command.Parameters.AddWithValue(“@Insert”,auditTrail.Insert);
command.Parameters.AddWithValue(“@Delete”,auditTrail.Delete);
command.Parameters.AddWithValue(“@Update”,auditTrail.Update);
command.Parameters.AddWithValue(“@DatoPrecedente”,auditTrail.DatoPrecedente);
command.Parameters.AddWithValue(“@DatoAggiornato”,auditTrail.DatoAggiornato);
结果=command.ExecuteOnQuery()>0?真:假;
}
捕获(例外情况除外)
{
等待连接。CloseAsync();
errorMsg=Ex.Message.ToString();
}
}
}                          
}    
返回结果;
}

您能否对照SSMS中使用的连接字符串检查程序中使用的连接字符串。再看看这个QA,你在哪里提交事务?@Selvin transaction.COMMIT解决了这个问题!!感谢you@vez25,无需使用单个
INSERT
语句进行明确的事务处理。默认情况下,每条语句都是一个自动提交事务;只需要对多个语句进行显式事务处理。不要使用