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 に答える