나는 CSV로 SQL을 전송하는 코드가 있습니다 그 후C#을, 파일을 액세스 할 수 없습니다
public void CreateCSVFile(DataTable dt, string strFilePath, bool Is_Ordre, int TypeDonne)
{
//FileStream fs = new FileStream(strFilePath, FileMode.Create);
// FileStream fs = File.Create(strFilePath);
// Create the CSV file to which grid data will be exported.
//StreamWriter sw = new StreamWriter(fs);
//StreamWriter sw = new StreamWriter(strFilePath, false);
using (var sw = new StreamWriter(strFilePath, false))
{
// First we will write the headers.
//DataTable dt = m_dsProducts.Tables[0];
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
if (Is_Ordre) sw.Write(", TYP_DONNE");
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write("\'" + dr[i].ToString().Replace("'", " ").Replace(",", ".") + "\'");
}
else
{
sw.Write("\' \'");
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
if (Is_Ordre) sw.Write(", \'" + TypeDonne + "\'");
sw.Write(sw.NewLine);
}
sw.Flush();
sw.Close();
sw.Dispose();
}
//fs.Close();
}
내가 이메일을 통해 CSV 파일을 보내
Attachment data_OD = new Attachment(LeSource, MediaTypeNames.Application.Octet);
Attachment data_LV = new Attachment(LeSource_LV, MediaTypeNames.Application.Octet);
oEmail.SendingEmail(ClientID, data_OD, data_LV);
public void SendingEmail(string CodeClient, Attachment dataOD, Attachment dataLV)
{
try
{
Pers_Conf oConf = LeConf.Get_Config(CodeClient);
MailMessage omail = new MailMessage();
var oSmtp = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("[email protected]", "xxxxxx"),
EnableSsl = true
};
omail.From = new MailAddress("[email protected]");
omail.To.Add(oConf.EmailAddr);
omail.Subject = "xxxxxx_" + CodeClient;
omail.Body = CodeClient;
omail.Attachments.Add(dataOD);
omail.Attachments.Add(dataLV);
oSmtp.Send(omail);
}
catch (Exception excThrown)
{
throw excThrown;
}
을 StreamWriter
제대로 파일을 닫습니다하지 않습니다, 난 이미이 함께 시도 것 같다 :
Try
{
StreamWriter sw = new StreamWriter(strFilePath, false);
}
Catch
{
}
Finaly
{
sw.Flush();
sw.Close();
sw.Dispose();
}
만에를 finally
블록이 인식되지 않습니다. sw
.
아이디어가 있으십니까?
추신 :이 코드 줄은 파일이 다른 프로세스
using (var sw = new StreamWriter(strFilePath, false))
때문에 StreamWriter
제대로 가깝거나 이메일 보내기가이 파일을 사용하고 있기 때문에하지의 폐쇄되지 않은 의한되고 사용되었는지 확인하는 방법 예외를 throw ? 당신의 StreamWriter
이 범위를 벗어나 선언하기 때문이다
에 또한
using
문을 사용 하는가? 처음에는? – Steve프로그램이이 함수에 처음으로 액세스하거나 전체 프로세스 (빌드, 보내기)가 동일한 파일 이름으로 두 번 이상 반복됩니다. – Steve
두 번째로 프로그래머가이 함수를 처리 할 때 발생합니다 – user609511