求助 报错 OracleConnection does not support parallel transactions.

wyhj123 2017-10-28 12:29:26
这段代码 哪里有错吗? 为啥会报这种错误 求大神解答
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filepath = folderBrowserDialog1.SelectedPath;
DirectoryInfo dir = new DirectoryInfo(filepath);
FileInfo[] fis = dir.GetFiles();

using (OracleConnection conn = new OracleConnection(OracleHelper.ConnectionStrings))
{
try
{
conn.Open();
OracleCommand cmd = null;
OracleTransaction transaction = null;

int j = 0;
for (int i = 0; i < fis.Length; i++)
{
string filename = fis[i].Name;
if (j == 0)
{
cmd = conn.CreateCommand();
cmd.Connection.BeginTransaction();
cmd.Transaction = transaction;
}
try
{
//cmd.CommandText = "update SZJT_ZJ_ZJSJ_B set wjnr=:WJNR where jh=" + "'"+filename.Split('钻')[0]+"'";
cmd.CommandText = "select wjnr from SZJT_ZJ_ZJSJ_B where jh=" + "'" + filename.Split('钻')[0] + "' for update";
OracleDataReader reader = cmd.ExecuteReader();

using (reader)
{
reader.Read();
OracleLob tempLob = reader.GetOracleLob(0);

FileStream fs = new FileStream(filepath+"\\"+filename, FileMode.Open);
tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
int length = 10485760;

byte[] Buffer = new byte[length];
//int i = 0;
while ((i = fs.Read(Buffer,0,length)) > 0)
{
tempLob.Write(Buffer,0,i);
}
fs.Close();
tempLob.EndBatch();
cmd.Parameters.Clear();
}

FileStream fs1 = null;
StreamWriter sw1 = null;
if (File.Exists(filepath + "\\" + "log.txt"))
{
fs1 = new FileStream(filepath + "\\" + "log.txt", FileMode.Append, FileAccess.Write);
}
else
{
fs1 = new FileStream(filepath + "\\" + "log.txt", FileMode.Create, FileAccess.Write);
}
sw1 = new StreamWriter(fs1);
sw1.WriteLine(filename);
sw1.Close();
fs1.Close();

j++;
if (j == 20 || j == fis.Length % 20)
{
transaction.Commit();
cmd.Dispose();
j = 0;
}
}
catch(Exception ex1)
{
this.textBox1.Text = this.textBox1.Text + filename + Environment.NewLine;
}
}


}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
}
...全文
305 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kampoo 2017-10-29
  • 打赏
  • 举报
回复
问题在这里,transaction未赋值:
conn.Open();
                        OracleCommand cmd = null;
                        OracleTransaction transaction = null;
                        
                        int j = 0;
                        for (int i = 0; i < fis.Length; i++)
                        {
                            string filename = fis[i].Name;
                            if (j == 0)
                            {
                                cmd = conn.CreateCommand();

                                transation = cmd.Connection.BeginTransaction();
                                // cmd.Connection.BeginTransaction();

                               // 你的代码transaction = null !
                                cmd.Transaction = transaction;
                            }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧