1

我正在尝试与运行非常旧版本的 Oracle (8) 的应用程序交谈。我需要先进行查询以获取最新的 id,然后使用此 id 插入。不幸的是,我在执行时遇到错误。我在网上搜索过,但找不到使用这种查询的人,所以任何帮助指针都将不胜感激。

private static OleDbConnection GetConn()
{
return new OleDbConnection()
{
    ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString
    };
}

public bool AddModel(Model model)
{
    OleDbConnection conn = GetConn();
    conn.Open();
    StringBuilder sb = new StringBuilder();
    sb.Append("DECLARE max_id INTEGER; ");
    sb.Append("BEGIN ");
    sb.Append("SELECT MAX(SORTID) into max_id FROM QRY.TABLE where status = 'A'; ");
    sb.Append("max_id := max_id + 1; ");
    sb.Append("INSERT INTO QRY.TABLE (COL1,COL2,COL3,COL4,COL5,COL6,COL7,SORTID) VALUES(? ,? ,? ,? ,? ,? ,? ,max_id);");
    sb.Append("END;");
    using (OleDbCommand command = new OleDbCommand(sb.ToString(), conn))
{
        command.Parameters.AddWithValue("COL1", model.ID);
    command.Parameters.AddWithValue("COL2", model.Description);
    command.Parameters.AddWithValue("COL3", model.Perc);
    command.Parameters.AddWithValue("COL4", model.Amount);
    command.Parameters.AddWithValue("COL5", model.Status);
    command.Parameters.AddWithValue("COL6", model.UpdatedUser);
    command.Parameters.Add("COL7", OleDbType.Date).Value = DateTime.Now;
    command.ExecuteNonQuery();
}
}

注意我不得不重新格式化这篇文章中的代码以删除对真实表的任何引用,因此它可能已经放入了一些类型。真正的代码会编译并运行。

4

1 回答 1

1

你可以修改

 sb.Append("INSERT INTO QRY.TABLE (COL1,COL2,COL3,COL4,COL5,COL6,COL7,SORTID) VALUES(@COL1,@COL2,@COL3,@COL4,@COL5,@COL6,@COL7,max_id);");

并修改这个

    command.Parameters.AddWithValue("@COL1", model.ID);
    command.Parameters.AddWithValue("@COL2", model.Description);
    command.Parameters.AddWithValue("@COL3", model.Perc);
    command.Parameters.AddWithValue("@COL4", model.Amount);
    command.Parameters.AddWithValue("@COL5", model.Status);
    command.Parameters.AddWithValue("@COL6", model.UpdatedUser);
    command.Parameters.Add("@COL7", OleDbType.Date).Value = DateTime.Now;
于 2012-09-27T15:52:49.817 回答