cmd.ExecuteNonQuery() 的问题

发布于 2024-09-08 18:25:40 字数 1696 浏览 14 评论 0原文

我正在使用 ADO.NET、C# 将值从 Web 表单插入到数据库中。我使用的数据库是Oracle数据库。值没有被插入,程序在 cmd.ExecuteNonquery() 处受到攻击,

这是我的代码,如果我犯了任何错误,请告诉我。我正在使用一些静态方法,这会有什么问题吗?

public Boolean AddDivCo(Int32 UserNo,String ID, String Role, String DivName )
{
    Boolean ret = false;


    OracleCommand cmd = new OracleCommand();
    OracleConnection conn = new OracleConnection();
    int i = 0;

    try
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_RIS"].ConnectionString;
        conn.Open();

        cmd.Connection = conn;
        String mySQL = "INSERT INTO R4CAD_ADMIN (AdminUserNo, AdminID, AdminRole, AdminDivName)VALUES(:AdminUserNo,:AdminID,:AdminRole,:DivName)";

        OracleParameter p1 = new OracleParameter("AdminUserNo", OracleType.Number);
        p1.Value = UserNo;
        cmd.Parameters.Add(p1);

        OracleParameter p2 = new OracleParameter("AdminID", OracleType.VarChar);
        p2.Value = ID;
        cmd.Parameters.Add(p2);

        OracleParameter p3 = new OracleParameter("AdminRole", OracleType.VarChar);
        p3.Value = Role;
        cmd.Parameters.Add(p3);

        OracleParameter p4 = new OracleParameter("DivName", OracleType.VarChar);
        p4.Value = DivName;
        cmd.Parameters.Add(p4);

        cmd.CommandText = mySQL;

        i = cmd.ExecuteNonQuery();

        if (i != 0)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message.ToString());
    }
    finally
    {
        cmd.Dispose();
        //cmd = null;
        //conn = null;
        conn.Close();
    }
    return ret;
}

I am inserting values in to Database from a Webform using ADO.NET, C#. DB I am using is Oracle Database. Values are not being inserted and the program gets struck at the cmd.ExecuteNonquery()

Here is my Code below, Please let me know If I am doing any mistake.. I am using some Static Methods will that be any problem ?..

public Boolean AddDivCo(Int32 UserNo,String ID, String Role, String DivName )
{
    Boolean ret = false;


    OracleCommand cmd = new OracleCommand();
    OracleConnection conn = new OracleConnection();
    int i = 0;

    try
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_RIS"].ConnectionString;
        conn.Open();

        cmd.Connection = conn;
        String mySQL = "INSERT INTO R4CAD_ADMIN (AdminUserNo, AdminID, AdminRole, AdminDivName)VALUES(:AdminUserNo,:AdminID,:AdminRole,:DivName)";

        OracleParameter p1 = new OracleParameter("AdminUserNo", OracleType.Number);
        p1.Value = UserNo;
        cmd.Parameters.Add(p1);

        OracleParameter p2 = new OracleParameter("AdminID", OracleType.VarChar);
        p2.Value = ID;
        cmd.Parameters.Add(p2);

        OracleParameter p3 = new OracleParameter("AdminRole", OracleType.VarChar);
        p3.Value = Role;
        cmd.Parameters.Add(p3);

        OracleParameter p4 = new OracleParameter("DivName", OracleType.VarChar);
        p4.Value = DivName;
        cmd.Parameters.Add(p4);

        cmd.CommandText = mySQL;

        i = cmd.ExecuteNonQuery();

        if (i != 0)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message.ToString());
    }
    finally
    {
        cmd.Dispose();
        //cmd = null;
        //conn = null;
        conn.Close();
    }
    return ret;
}

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

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

发布评论

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

评论(1

行至春深 2024-09-15 18:25:40

该表上是否定义了主键?如果是这样,那么我的猜测是您有另一个会话已经使用此键插入了记录,但尚未通过提交或回滚终止事务。我没有看到提交作为您代码的一部分 - 我假设您在其他地方这样做?

再次执行上面的代码,当它挂起时,从另一个会话运行以下查询:

SELECT
      (SELECT username FROM v$session WHERE sid=a.sid) blocker,
       a.sid,
      ' is blocking ',
       (SELECT username FROM v$session WHERE sid=b.sid) blockee,
           b.sid
  FROM v$lock a JOIN v$lock b ON (a.id1 = b.id1 AND a.id2 = b.id2)
 WHERE a.block = 1
   AND b.request > 0;

这应该告诉您是否被另一个会话阻止以及该会话的 SID 是什么。

Is there a primary key defined on this table? If so, then my guess is that you have another session that already has inserted a record with this key, but has not yet terminated the transaction with a commit or rollback. I don't see a commit as part of your code - I assume you're doing that somewhere else?

Execute your code above once more, and while it's hung run the following query from another session:

SELECT
      (SELECT username FROM v$session WHERE sid=a.sid) blocker,
       a.sid,
      ' is blocking ',
       (SELECT username FROM v$session WHERE sid=b.sid) blockee,
           b.sid
  FROM v$lock a JOIN v$lock b ON (a.id1 = b.id1 AND a.id2 = b.id2)
 WHERE a.block = 1
   AND b.request > 0;

This should tell you if you're being blocked by another session and what the SID is of that session.

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