来自DB行的多附加文件

时间:2014-09-03 06:13:42

标签: c# sql-server email attachment

我怎么不能从DB行附加到拆分的字符串文件名(我需要“mode = 2”和“case 2:”)。 在我的日志文件错误中:

  

附加错误的文件:无法找到文件   'C:\的Inetpub \ wwwroot的\ PLATFORM_700_NTFSRV \ PLATFORM_700_NTFSRV_LAB \附件\的text.txt,text2.txt'。

这里是我的示例代码和我在DB中的行 db中的行:

|FILE_TO_ATTACH     |
|text1.txt,text2.txt|

 public DataTable GetAttachmentFiles(int mode , string fileIDList)
    {
        try
        {
              DataTable DTB = new DataTable();
            if (mode == 1)
            {

                SqlCommand TheCommand = GetCommand("application_MessageAttachFiles", CommandType.StoredProcedure,
                    GetConnection("APP"));
                TheCommand.Parameters.Add("FILEIDLIST", SqlDbType.VarChar, 8000);
                TheCommand.Parameters["FILEIDLIST"].Value = fileIDList;


                SqlDataAdapter SDA = new SqlDataAdapter();
                SDA.SelectCommand = TheCommand;
                SDA.Fill(DTB);
            }
            else if(mode == 2)
            {
                try
                {
                    DTB.Columns.Add("FILENAME");
                    string[] fileList = fileIDList.Split(',');

                    for (int c = 0; c < fileList.Length; c++)
                    {
                        DataRow DR = DTB.NewRow();
                        DR["FILENAME"] = fileList[c];


                        DTB.Rows.Add(DR);
                    }
                }
                catch (Exception ex)
                {
                    RecordLine("ERROR Reading GetAttachmentFiles:  " + ex.Message);
                }

            }

            return DTB;
        }
        catch (Exception eX)
        {
            RecordLine("ERROR GetAttachmentFiles :  " + eX.Message);
            return null;
        }
    }

    public Attachment AttachmentFile(int mode, string fileNameString, int fileID, DataRow DRA)
    {
        // mode.ToString(ConfigurationSettings.AppSettings["MODE"]);


        try
        {

            switch (mode)
            {
                case 1: /*from Database*/

                    if (DRA != null)
                    {
                        Attachment messageAttachment;


                        int fileDataSize = int.Parse(DRA["FileSize"].ToString());
                        string fileType = DRA["FileType"].ToString();
                        string fileName = DRA["FileName"].ToString();
                        byte[] fileBuffer = (DRA["FileData"]) as byte[];


                        MemoryStream ms = new MemoryStream(fileBuffer);

                        RecordLine("DEBUG 2 - " + fileName + " " + fileType + "  " + fileDataSize.ToString() + " " + ms.Length.ToString() + " buffer size:" + fileBuffer.Length.ToString());
                        messageAttachment = new Attachment(ms, fileName, fileType);
                        return messageAttachment;


                    }

                    break;

                case 2: /*from Local Machin */
                {


                    Attachment messageAttachment;
                    try 
                    {

                        fileNameString = String.Format("{0}\\{1}", ConfigurationSettings.AppSettings["SOURCE_FILE"],
                            fileNameString);

                        messageAttachment = new Attachment(fileNameString);
                        RecordLine("DEBUG 2.1 - " + messageAttachment.Name);
                        return messageAttachment;
                    }
                    catch (Exception ex)
                    {
                        RecordLine("FILE TO ATTACH ERR : " + ex.Message);
                    }
                }

                    break;


                default:
                    return null;
                    break;
            }


            return null;




        }
        catch (Exception eX)
        {
            RecordLine("ERROR AttachmentFile :  " + eX.Message);
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

我添加了这段代码并且工作正常:

 foreach (DataRow DRA in DTBA.Rows)
      {
          message.Attachments.Add(AttachmentFile(2, DRA["FILENAME"].ToString().Trim(), 0, null));
          RecordLine("DEBUG 3.1 - " + message.Attachments.Count.ToString());
      }