如何在 OracleCommand INSERT INTO 之后检索 sys_guid() 的值
How can you retrieve the value of sys_guid() after an OracleCommand INSERT INTO
我想检索包含在事务中生成 sys_guid() 的 Oracle 数据库的特定列的值。
public string GetGUID(OracleConnection connection)
{
string guid = "";
connection.Open();
OracleCommand cmd = new OracleCommand();
OracleTransaction transaction;
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = transaction;
cmd.Connection = connection;
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO CUSTOMERS (NAME,DETAILS) VALUES (:nm, :dts)";
cmd.Parameters.Add("nm", OracleDbType.VarChar);
cmd.Parameters["nm"].Value = name;
cmd.Parameters.Add("dts", OracleDbType.VarChar);
cmd.Parameters["dts"].Value = details;
cmd.Prepare();
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
}
reader.Close();
transaction.Commit();
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine(e.Message);
}
return guid;
}
Table CUSTOMERS 有一个 ORDER 列,它使用 SYS_GUID() 创建了一个 guid,我如何检索该 guid?
尝试 returning .. into
clause 例如
...
using (OracleCommand cmd = connection.CreateCommand()) {
cmd.CommandText =
@"INSERT INTO CUSTOMERS (
NAME,
DETAILS)
VALUES (
:nm,
:dts)
RETURNING
""ORDER"" INTO :orderid";
cmd.Parameters.Add("nm", OracleDbType.VarChar);
cmd.Parameters["nm"].Value = name;
cmd.Parameters.Add("dts", OracleDbType.VarChar);
cmd.Parameters["dts"].Value = details;
cmd.Parameters.Add("orderid", OracleDbType.VarChar);
cmd.Parameters["orderid"].Direction = ParameterDirection.Output;
try {
cmd.ExecuteNonQuery(); // Just execute, nothing to read
transaction.Commit();
guid = Convert.ToString(cmd.Parameters["orderid"].Value);
}
catch (Exception e) { //TODO: put more specific exception type
transaction.Rollback();
Console.WriteLine(e.Message);
}
return guid;
}
...
我想检索包含在事务中生成 sys_guid() 的 Oracle 数据库的特定列的值。
public string GetGUID(OracleConnection connection)
{
string guid = "";
connection.Open();
OracleCommand cmd = new OracleCommand();
OracleTransaction transaction;
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = transaction;
cmd.Connection = connection;
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO CUSTOMERS (NAME,DETAILS) VALUES (:nm, :dts)";
cmd.Parameters.Add("nm", OracleDbType.VarChar);
cmd.Parameters["nm"].Value = name;
cmd.Parameters.Add("dts", OracleDbType.VarChar);
cmd.Parameters["dts"].Value = details;
cmd.Prepare();
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
}
reader.Close();
transaction.Commit();
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine(e.Message);
}
return guid;
}
Table CUSTOMERS 有一个 ORDER 列,它使用 SYS_GUID() 创建了一个 guid,我如何检索该 guid?
尝试 returning .. into
clause 例如
...
using (OracleCommand cmd = connection.CreateCommand()) {
cmd.CommandText =
@"INSERT INTO CUSTOMERS (
NAME,
DETAILS)
VALUES (
:nm,
:dts)
RETURNING
""ORDER"" INTO :orderid";
cmd.Parameters.Add("nm", OracleDbType.VarChar);
cmd.Parameters["nm"].Value = name;
cmd.Parameters.Add("dts", OracleDbType.VarChar);
cmd.Parameters["dts"].Value = details;
cmd.Parameters.Add("orderid", OracleDbType.VarChar);
cmd.Parameters["orderid"].Direction = ParameterDirection.Output;
try {
cmd.ExecuteNonQuery(); // Just execute, nothing to read
transaction.Commit();
guid = Convert.ToString(cmd.Parameters["orderid"].Value);
}
catch (Exception e) { //TODO: put more specific exception type
transaction.Rollback();
Console.WriteLine(e.Message);
}
return guid;
}
...