小弟我用了StreamReader和StreamWriter,StreamReader用来读一个格式文本,再在数据库中查出来的数据用StreadReader写到一个文本文件里面,当我一台机器连接服务器操作是很正常,但是,有多台机器同事操作的时候,就报错,出现了IO异常,不能并行进行,下面是原代码,麻烦各位帮我看看,谢谢了。
private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(isPrint(ImageButton2.CommandName))
{
Response.Write("<script language='javascript'>alert('对不起,您选择的帐期帐单已打印,不能打印两次');</script>");
return;
}
int billing_id,ServID;
billing_id=Getbilling_id();
ServID=getServID(ImageButton2.CommandName);
string strFile="C:\\Inetpub\\ftproot\\custmer\\tmp\\";
string strFiles="C:\\Inetpub\\ftproot\\custmer\\xd.txt";
string strDFile="C:\\Inetpub\\ftproot\\custmer\\";
strFile=strFile+Request.UserHostAddress+".txt";
strDFile=strDFile+Request.UserHostAddress+".txt";
if(System.IO.File.Exists(strFile))
{
System.IO.File.Delete(strFile);
}
StreamWriter myStreamWriter = null;
StreamReader myStreamReader=null;
try
{
myStreamWriter =new StreamWriter(strFile, true,UnicodeEncoding.GetEncoding("GB2312"));
myStreamReader =new StreamReader(strFiles,UnicodeEncoding.GetEncoding("GB2312"));
myStreamWriter.Flush();
}
catch(Exception exc)
{
Response.Write("<script language='javascript'>alert('文件操作失败!');</script>");
return;
}
string result=null;
string temp=null;
string foot=null;
int rowCount=0;
string snStr="Provider=MSDAORA.1;Data source=JFZW;user id=jfzw;password=123456789";
OleDbConnection dbCon=new OleDbConnection(snStr);
dbCon.Open();
string sqlStr=null;
if(!cB.Checked)
{
sqlStr=@" SELECT A.TICKET_ID,sum(nvl(decode(B.SORT_ID,1,A.CHARGE),0))/100 as thf,sum(nvl(decode(B.SORT_ID,2,A.CHARGE),0))/100 as fjf,"
+" substr(F_GET_CALL_TICKET_CX(A.TICKET_ID),1,10) as ticket_type,substr(F_GET_CALL_TICKET_CX(A.TICKET_ID),11,10) as v_duration,"
+" substr(F_GET_CALL_TICKET_CX(A.TICKET_ID),21,30) as start_time,substr(F_GET_CALL_TICKET_CX(A.TICKET_ID),51,20) as called_nbr,"           
+" rtrim(substr(F_GET_CALL_TICKET_CX(A.TICKET_ID),71,10)) as duration,"
+" (sum(nvl(decode(B.SORT_ID,1,A.CHARGE),0))+ sum(nvl(decode(B.SORT_ID,2,A.CHARGE),0)))/100 as hj,"     
+" substr(F_GET_CALL_TICKET_CX(A.TICKET_ID),90,10) as calling_nbr"
+" FROM USAGE_CHARGE A,XDCX_ACCITEMTYPE B"  
+" WHERE ( A.TICKET_ID IS NOT NULL) and ( B.ACCTITEM_TYPE_ID = A.ACCT_ITEM_TYPE_ID ) and ( A.SERV_ID = '" + ServID +"' ) AND ( A.BILLING_CYCLE_ID = '" + billing_id +"' )"       
+" GROUP BY A.TICKET_ID  ORDER BY 6 ASC";
}
else
{
sqlStr=@"select A.mobile_ticket_id,A.acct_item_type_id  ,rtrim(substr(f_get_mobile_call_ticket(A.mobile_ticket_id),21,30)) as start_time,"
+" rtrim(substr(f_get_mobile_call_ticket(A.mobile_ticket_id),51,20)) as called_nbr,"
+" rtrim(substr(f_get_mobile_call_ticket(A.mobile_ticket_id),71,10)) as duration,"
+" sum(A.charge)/100 as hj,"
+" substr(f_get_mobile_call_ticket(A.mobile_ticket_id),90,10) as calling_nbr"
+" from mobile_usage_charge A,xdcx_accitemtype_xlt B"
+" where   ( A.serv_id =  '" + ServID +"' ) and ( A.billing_cycle_id =  '" + billing_id +"' ) and"
+" ( A.mobile_ticket_id is not null) and ( B.acctitem_type_id = A.acct_item_type_id ) and"
+" ( rtrim(substr(f_get_mobile_call_ticket(A.mobile_ticket_id),71,10))) >2"
+" group by A.mobile_ticket_id ,A.acct_item_type_id "
+" order by rtrim(substr(f_get_mobile_call_ticket(A.mobile_ticket_id),21,30))";
}
OleDbCommand myCommand=new OleDbCommand(sqlStr,dbCon);
OleDbDataReader myReader=myCommand.ExecuteReader();
temp=myStreamReader.ReadLine();
while(temp!=null)
{
rowCount++;
switch(rowCount)
{
case 3:
temp+=ImageButton2.CommandName;
result+=temp+Environment.NewLine;
break;
case 4:
temp+=DPTime.SelectedItem.Text;
result+=temp+Environment.NewLine;
break;
case 9:
foot+=temp+Environment.NewLine;
break;
case 10:
temp+=getCountF(ImageButton1.CommandName)+"元";
foot+=temp+Environment.NewLine;
break;
case 11:
temp+=DateTime.Now.ToString("D");
foot+=temp+Environment.NewLine;
break;
default:
result+=temp+Environment.NewLine;
break;
}
temp=myStreamReader.ReadLine();
}
myStreamWriter.Write(result);
myStreamWriter.Flush();
while(myReader.Read())
{
result=System.String.Format(" {0} {1} {2} {3}",formatStr(myReader["called_nbr"].ToString(),17),formatStr(myReader["start_time"].ToString(),19),formatStr(myReader["duration"].ToString(),4),formatStr(myReader["hj"].ToString(),7));
result+=Environment.NewLine;
myStreamWriter.Write(result);
myStreamWriter.Flush();
}
myStreamWriter.Write(foot);
myStreamWriter.Flush();
myStreamReader.Close();
myStreamWriter.Close();
if(System.IO.File.Exists(strDFile))
{
System.IO.File.Delete(strDFile);
}
File.Copy(strFile,strDFile);
File.Delete(strFile);
dbCon.Close();
OleDbConnection Conn;
string snStr1="Provider=MSDAORA.1;Data source=PASS;user id=cmp;password=123456789";
Conn= new OleDbConnection(snStr1);
Conn.Open();
OleDbTransaction myTrans = Conn.BeginTransaction();
OleDbCommand myCommand1 = new OleDbCommand("", Conn, myTrans);
string dhhm=ImageButton2.CommandName;
string zq=getstrzq(DPTime.SelectedItem.Text);
myCommand1.CommandText=@" insert into dbcall.print_num(SEVR_NBR,MONTH,PNUM) values('" + dhhm + "','" + zq + "',1)";
try
{
myCommand1.ExecuteNonQuery();
myTrans.Commit();
}
catch(System.Exception E)
{
myTrans.Rollback();
}
finally
{
Conn.Close();
}
Server.Transfer("hfcx_menu.aspx");
//ImageButton2.ImageUrl="../image/打印0.PNG";
//ImageButton2.Enabled=false;
}