Frage

Hi all i have written the following transaction to insert data but when i am getting an exception only the data which got the exception not inserting to db the remaining all are inserting

This is what i wrote

public bool addWhole(SqlTransaction osqlTrans)
{
   m_flag = false;
   osqlTrans = null;

    SqlConnection osqlCon = new SqlConnection(constr);

    if (osqlCon.State != ConnectionState.Open)
    {
        osqlCon.Open();
    }

    osqlTrans = osqlCon.BeginTransaction();

    try
    {
      if (this.addRisk(osqlTrans, osqlCon))
      {
       if (this.addEconomical(osqlTrans, osqlCon))
       {
           osqlTrans.Commit();
       }
      }
     }
    catch (Exception ex)
    {
        osqlTrans.Rollback();
    }
    finally
    {
        osqlCon.Close();
    }
    return m_flag;
}

public bool addRisk(SqlTransaction oRiskTrans, SqlConnection oRiskConn)
{
     con = new SqlConnection(constr);
    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    cmd = new SqlCommand("insert into tblEnrollmentData (EID,Eyear,Epercent) values('" + id + "','" + str + "','" + dbPercent + "')", con); //Even i tried adding transaction in command statement
            if (cmd.ExecuteNonQuery() > 0)
            {
                m_flag = true;
            }
   }

public bool addEconomical(SqlTransaction oRiskTrans, SqlConnection oRiskConn)
{
     con = new SqlConnection(constr);
    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    cmd = new SqlCommand("insert into tblEnrollmentData (EID,Eyear,Epercent) values('" + id + "','" + str + "','" + dbPercent + "')", con);//Even i tried adding transaction in command statement
            if (cmd.ExecuteNonQuery() > 0)
            {
                m_flag = true;
            }
   }

I tried to rollback the transaction by failing the second condition but my first statement is inserting to DB.. What should i do in order to overcome this

War es hilfreich?

Lösung

I'm guessing its one of these things

1) You are not using the same connection object for all the different commands
2) You are not assigning the transaction to the commands before you execute them
3) Both perhaps

Try using the same connection object and assigning the transaction to the command before you execute it for an example see this page on MSDN. http://msdn.microsoft.com/en-us/library/86773566.aspx

Andere Tipps

Transaction is not shared between connections and you always create a new connection. Use oRiskConn specified as 2nd parameter of your methods.

As you are creating a new connection in every function your code didn't work. Just remove the connection

  `con = new SqlConnection(constr);`

Replace it with the connection available in your function i.e

oRiskConn and don't initialize it as a new connection. If you did so again your transaction as per your requirement will not work. Also include oRiskTrans in your command object. Then it will works as per your requirement

I don't know what some of your parameters are but it looks like you want something like this:

class SomeClass
{
    // These need to be set to appropriate values
    int id, str;
    double dbPercent;
    string constr;

    public bool addWhole()
    {
        var m_flag = false;
        using (var osqlCon = new SqlConnection(constr))
        {
            if (osqlCon.State != ConnectionState.Open)
            {
                osqlCon.Open();
            }

            using (var osqlTrans = osqlCon.BeginTransaction())
            {
                try
                {
                    if (m_flag = this.addRisk(osqlTrans))
                    {
                        if (m_flag = this.addEconomical(osqlTrans))
                        {
                            osqlTrans.Commit();
                        }
                    }
                }
                catch (Exception)
                {
                    // Use $exception in watch window if you are debugging
                    osqlTrans.Rollback();
                }
            }
        }
        return m_flag;
    }

    public bool addRisk(SqlTransaction oRiskTrans)
    {
        var m_flag = false;
        using (var cmd = new SqlCommand("insert into tblEnrollmentData (EID,Eyear,Epercent) values('" + id + "','" + str + "','" + dbPercent + "')"))
        {
            cmd.Transaction = oRiskTrans;
            cmd.Connection = oRiskTrans.Connection;
            if (cmd.ExecuteNonQuery() > 0)
            {
                m_flag = true;
            }
        }
        return m_flag;
    }

    public bool addEconomical(SqlTransaction oRiskTrans)
    {
        var m_flag = false;
        using (var cmd = new SqlCommand("insert into tblEnrollmentData (EID,Eyear,Epercent) values('" + id + "','" + str + "','" + dbPercent + "')"))
        {
            cmd.Transaction = oRiskTrans;
            cmd.Connection = oRiskTrans.Connection;

            if (cmd.ExecuteNonQuery() > 0)
            {
                m_flag = true;
            }
        }
        return m_flag;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top