谁能告诉我是什么吗?我的交易有误

发布于 2024-12-15 03:38:45 字数 1759 浏览 0 评论 0原文

大家好,我已经编写了以下事务来插入数据,但是当我收到异常时,只有出现异常的数据没有插入到数据库,其余所有数据都在插入

这是我写的,

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;
            }
   }

我试图通过失败第二个条件来回滚事务但我的第一条语句是插入到DB..我应该做什么才能克服这个问题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌无敌 2024-12-22 03:38:45

我猜这是其中之一

1)您没有为所有不同的命令使用相同的连接对象
2) 在执行命令之前,您没有将事务分配给命令
3) 两者都可以

尝试使用相同的连接对象,并在执行命令之前将事务分配给命令,有关示例,请参阅 MSDN 上的此页面。 http://msdn.microsoft.com/en-us/library/86773566.aspx

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

遮了一弯 2024-12-22 03:38:45

连接之间不共享事务,并且您始终创建新连接。使用 oRiskConn 指定为方法的第二个参数。

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

爱本泡沫多脆弱 2024-12-22 03:38:45

当您在每个函数中创建新连接时,您的代码不起作用。只需删除连接,

  `con = new SqlConnection(constr);`

将其替换为函数中可用的连接,即

oRiskConn,并且不要将其初始化为新连接。如果您再次这样做,您的交易将无法按照您的要求进行。还要在命令对象中包含 oRiskTrans。然后它将按照您的要求工作

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

溺深海 2024-12-22 03:38:45

我不知道你的一些参数是什么,但看起来你想要这样的东西:

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文