【问题标题】:Multi attach files from DB row来自数据库行的多个附加文件
【发布时间】:2014-09-03 06:13:42
【问题描述】:

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

FILE TO ATTACH ERR : 找不到文件 'C:\inetpub\wwwroot\PLATFORM_700_NTFSRV\PLATFORM_700_NTFSRV_LAB\Attachments\text.txt,text2.txt'。

这是我的示例代码和我在 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;
        }
    }

【问题讨论】:

    标签: c# sql-server email attachment


    【解决方案1】:

    我已经添加了这段代码,它可以工作:

     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());
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多