我有以下错误:
当分配给命令的连接处于挂起的本地事务中时,ExecuteNonQuery 要求该命令具有事务。该命令的 Transaction 属性尚未初始化。
此错误出现在函数 executaComando
我的功能保存:
public void save(string[] arr)//, int lista)
{
SqlConnection conexao = new SqlConnection(WebConfigurationManager.ConnectionStrings["strConexao"].ToString());
conexao.Open();
SqlTransaction trx = conexao.BeginTransaction();
try
{
//Truncate cliente_recurso
BUProjetosDAL dal = new BUProjetosDAL();
dal.excluirClientesRecurso(conexao);
dtsRecursoClienteTableAdapters.RECURSO_CLIENTETabelaTableAdapter tabela = new dtsRecursoClienteTableAdapters.RECURSO_CLIENTETabelaTableAdapter();
for (int j = 0; j <= 10; j++) { // <- here
tabela.Insert(arr);
}
trx.Commit();
}
catch (SqlException ex)
{
try
{
trx.Rollback();
}
catch (Exception exRollback)
{
Response.Write(exRollback.Message);
}
}
}
函数 excluirClientesRecurso
public void excluirClientesRecurso(SqlConnection conexao)
{
executaComando("TRUNCATE TABLE dbo.RECURSO_CLIENTE", conexao);
}
函数 executaComando (----错误出现在这里----)
public void executaComando(string query, SqlConnection conexao)
{
try
{
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = conexao;
cmd.ExecuteNonQuery();
conexao.Close();
}
catch (Exception ex)
{
throw ex;
}
}