gpt4 book ai didi

c# - 如何将 CLOB 字段读入 C# 变量?

转载 作者:行者123 更新时间:2023-12-02 18:05:29 28 4
gpt4 key购买 nike

Possible Duplicate:
Why Do I get OutOfRange Exception in GetOrdinal Function of this CLOB field?

I need to read a CLOB field from an ORACLE table int a C# variable of type String. Does anyone know how to accomplish this task? 

这就是我所做的,但在计算字段的 GetOrdinal 时出现 IndexOutofRange。提前致谢。

 public void ReadFunction(string FName, out string fContent) 
{
OracleCommand command = _connection.CreateCommand();
OracleTransaction transaction = _connection.BeginTransaction();
command.Transaction = transaction;
command.CommandText = "SELECT TO_CLOB(TO_NCLOB(FUNCTION_SCRIPT)) FROM IS_FUNCTION where FNAME=:fName ";
command.Parameters.Add("FName", OracleType.NVarChar).Value = FName;
OracleDataReader odr = command.ExecuteReader();
int temp = odr.GetOrdinal("FUNCTION_SCRIPT");
OracleLob myLob = odr.GetOracleLob(temp);
fContent = (String)myLob.Value;
odr.close();
}

最佳答案

这是获取 Blob 的代码,然后您应该按照您需要的方式将其转换为字符串。我不知道你需要什么格式。

  // create and open connection
// change for your environment
string connStr = "User Id=pm; Password=pm; Data Source=orcllx; Pooling=false";
OracleConnection con = new OracleConnection(connStr);
try
{
con.Open();
}
catch (OracleException ex)
{
MessageBox.Show(ex.Message);
}

// statement to get a blob
string sql = "select ad_composite from print_media where product_id=3106 and
ad_id=13001";

// create command object
// InitialLOBFetchSize
// defaults to 0
OracleCommand cmd = new OracleCommand(sql, con);

cmd.InitialLOBFetchSize = 8192;

// create a datareader
OracleDataReader dr = cmd.ExecuteReader();

// read the single row result
try
{
dr.Read();
}
catch (OracleException ex)
{
MessageBox.Show(ex.Message);
}

// use typed accessor to retrieve the blob
OracleBlob blob = dr.GetOracleBlob(0);

// create a memory stream from the blob
MemoryStream ms = new MemoryStream(blob.Value);

// set the image property equal to a bitmap
// created from the memory stream
pictureBox1.Image = new Bitmap(ms);

关于c# - 如何将 CLOB 字段读入 C# 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12574508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com