【问题标题】:Insert records to two tables using oracle使用oracle向两个表中插入记录
【发布时间】:2016-03-03 17:00:40
【问题描述】:

我将向 Oracle 中的两个表插入值。当我将记录插入第一个表时,我需要获取该表的 Id,它需要进入第二个表。

使用我的查询将值插入第一个表。

  public bool AddNewNews(NEWS nws)
    {
        bool status = false;

        try
        {

                DatabaseProviderFactory factory = new DatabaseProviderFactory();
                Database db = factory.Create("CharikaConString");

                con.Open();

                string query = @"INSERT INTO NEWS_TBL(NewsID,NAME) 
                                 VALUES(newsid.nextval,:NAME)";


                cmd = db.GetSqlStringCommand(query);

                db.AddInParameter(cmd, "NAME", DbType.String, nws.NAME);

                db.ExecuteNonQuery(cmd);
// In the below commented lines are,I tried to insert second table but not success.
                  // String query2 = "Select newsid.currval";

                 //string query2 = @"INSERT INTO DTL_TBL(DtlId,desc) VALUES (newsid.currval,nws.Desc)";
                // cmd = db.GetSqlStringCommand(query);

                 //db.AddInParameter(cmd, "desc", DbType.String, nws.Desc);

                // db.ExecuteNonQuery(cmd);

        }
        catch (Exception ex)
        {

            throw;
        }
        finally
        {
            con.Close();
        }
        return status;
    }

【问题讨论】:

标签: c# sql oracle


【解决方案1】:

您可以通过多种方式使用 oracle 序列,您可以在初步声明中获取该值

select newsid.nextval from dual

然后在后续插入中将其用作常规参数?

【讨论】:

  • 不确定是否有更好的方法,但我一直都是这样做的!
  • 那么如何将其插入到第二张表中?
  • 你会说:double newId = db.executescalar();在上面的 select 语句中,然后在需要的地方替换 newId
猜你喜欢
  • 2016-07-07
  • 2021-12-06
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2017-04-19
  • 1970-01-01
相关资源
最近更新 更多