2

MS SQLデータベースに保存されている1つ以上のファイルを電子メールに追加するにはどうすればよいですか?ディレクトリまたはfileUploadコントロールから保存されたファイルを添付する方法はすでに知っています。

私のデータブルフィールドは次のとおりです。

col1:Email_ID-int

col2:file_name-varchar

col3:file_content-画像

したがって、私のSQLselectステートメントは非常に単純です。

Select file_name, file_content from Attachments where Email_ID = 333

後でメールに添付する方法がわかりません。

ありがとう!

4

2 に答える 2

3
  SqlCommand cmdSelect=new SqlCommand("Select file_name, file_content " + 
              " from Attachments where Email_ID=@ID",this.sqlConnection1);
        cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);
        cmdSelect.Parameters["@ID"].Value=333;

DataTable dt = new DataTable();
        this.sqlConnection1.Open();
        SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmdSelect;
            sda.Fill(dt);
     if(dt.Rows.Count > 0)
{
        byte[] barrImg=(byte[])dt.Rows[0]["file_content"];
        string strfn= "Your File Directory Path" + Convert.ToString(dt.Rows[0]["file_name"]);
        FileStream fs=new FileStream(strfn, 
                          FileMode.CreateNew, FileAccess.Write);
        fs.Write(barrImg,0,barrImg.Length);
        fs.Flush();
        fs.Close();
   //now you can attache you file to email here your file is generate at path stored in "strfn" variable
}

参照: http: //www.codeproject.com/KB/database/ImageSaveInDataBase.aspx

于 2011-07-22T05:48:08.417 に答える
2

SQLから画像を取得し、それをストリームに変換してから、メールへの添付ファイルを作成します。サンプル:Attachment(Stream、String)指定されたストリームと名前でAttachmentクラスの新しいインスタンスを初期化します。

于 2011-07-21T14:03:53.237 に答える