请教一个问题 "无效操作。连接被关闭。"
用户代码未处理 System.InvalidOperationExceptionMessage="无效操作。连接被关闭。"
Source="System.Data.OracleClient"
StackTrace:
在 System.Data.OracleClient.OracleLob.AssertConnectionIsOpen()
在 System.Data.OracleClient.OracleLob.Write(Byte[] buffer, Int32 offset, Int32 count)
在 DAL.AdminDal.WriteClob(Int32 id, String content) 位置 E:\project\Cxlw\DAL\AdminDal.cs:行号 1229
在 admin_gonggao_fabu.btn_Click(Object sender, EventArgs e) 位置 e:\project\Cxlw\Web\admin\gonggao_fabu.aspx.cs:行号 63
在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: --------------------编程问答-------------------- --------------------编程问答-------------------- 贴代码咂, --------------------编程问答-------------------- 贴代码才晓得啊 --------------------编程问答-------------------- 这是网上参考的代码,红色处报错
public void WriteClob(int id, string content)
{
OracleConnection myCn = new OracleConnection("Data Source=YR;User Id=cxlw;Password=cxlw;");
OracleCommand myCmd = new OracleCommand();
try
{
myCn.Open();
}
catch (System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
OracleTransaction myTrans = myCn.BeginTransaction();
try
{
myCmd.Connection = myCn;
myCmd.Transaction = myTrans;
myCmd.CommandText = "select GG_CONTENT from T_LW_GG_INFO_NEW where id=" + id + " FOR UPDATE";
OracleDataReader reader = myCmd.ExecuteReader();
using (reader)
{
reader.Read();
OracleLob clob = reader.GetOracleLob(0);
Encoding UTF8 = Encoding.UTF8;
byte[] buffer = UTF8.GetBytes(content);
if (buffer.Length % 2 == 0)
clob.Write(buffer, 0, buffer.Length);
else
clob.Write(buffer, 0, (buffer.Length - 1));
myTrans.Commit();
}
}
catch (System.Data.OracleClient.OracleException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
--------------------编程问答-------------------- while(reader.Read())
{} --------------------编程问答-------------------- while(reader.Read())
{}
Reader必须用上述Read()方法,否则取不到数! --------------------编程问答-------------------- 报什么错误? --------------------编程问答-------------------- 报的错是:用户代码未处理 System.InvalidOperationException
Message="无效操作。连接被关闭。"
在clob.Write(buffer, 0, buffer.Length)或 clob.Write(buffer, 0, (buffer.Length - 1));
时出错 --------------------编程问答-------------------- 原因应该在于查询出的GG_CONTENT内容为空,你可以用reader.IsDBNull(0)来判断。这种情况下OracleLob的连接是Null,可以用clob.Connnection属性判断。所以OracleLob连接是关闭的.
解决办法:因为clob.Write会重写该栏位所以使查询出的GG_CONTENT不为空即可。
补充:.NET技术 , ASP.NET