1

我有以下错误:

当分配给命令的连接处于挂起的本地事务中时,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;
    }
}
4

3 回答 3

2

好吧,正如错误所说。您打开了一个事务,但没有将其分配给命令。

一个简单的解决方案是将其简单地传递给executaComando

public void excluirClientesRecurso(SqlConnection conexao,
                                   SqlTransaction transaction)
{
    executaComando("TRUNCATE TABLE dbo.RECURSO_CLIENTE", conexao);
}

public void executaComando(string query, SqlConnection conexao,
                           SqlTransaction transaction)
{
    SqlCommand cmd = new SqlCommand(query);
    cmd.Connection = conexao;
    cmd.Transaction = transaction
    cmd.ExecuteNonQuery();
    conexao.Close();
}

// Usage
dal.excluirClientesRecurso(conexao, trx);

请注意,我删除了 try-catch ,executaComando因为它没有做任何好事。事实上,它做了坏事:它弄乱了异常的堆栈跟踪。

于 2013-10-25T12:44:22.550 回答
1

是的,它确实。所以传入交易。这基本上是你说服它“我知道这里有交易”的方式。所以:

dal.excluirClientesRecurso(conexao, trx);
// ...
public void excluirClientesRecurso(SqlConnection conexao,
     SqlTransaction transaction = null)
{
    executaComando("TRUNCATE TABLE dbo.RECURSO_CLIENTE",
        conexao, transaction);
}

public void executaComando(string query, SqlConnection conexao,
     SqlTransaction trasaction = null)
{
    try
    {
        SqlCommand cmd = new SqlCommand(query);
        cmd.Trasnaction = transaction;
        // ...
于 2013-10-25T12:44:47.320 回答
1

试试这个代码

    public bool updateusertable(string UserName,string Password,string Datetime)
            {
                bool bResult = false;
               SqlTransaction tx;
                try
                {
                    tx=Sqlconnect.Begintransaction;
                    SqlCommand Ocmd = new SqlCommand();
                   Sqlconnect= Cconnect.OpenSqlConnection();
                    Ocmd.Connection = Sqlconnect;
                    Ocmd.CommandType = CommandType.StoredProcedure;
                    Ocmd.CommandText = "SP_User_login_Update";
                    Ocmd.Parameters.Add("@UserName", SqlDbType.VarChar, 100).Value = UserName;
                    Ocmd.Parameters.Add("@Password", SqlDbType.VarChar, 100).Value = Password;
                    Ocmd.Parameters.Add("@lastlogin", SqlDbType.VarChar, 100).Value = Datetime;
                    int i = Ocmd.ExecuteNonQuery();
                    if (i <= 1)
                    {
                        bResult = true;
                    }else
                    {
                        tx.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message.ToString();
                    tx.Rollback();
                }
                finally
                {
                    tx.Commit();
                }
                return bResult;
            }
于 2014-07-01T16:33:11.457 回答