Click here to Skip to main content
15,918,125 members
Home / Discussions / C#
   

C#

 
QuestionStream / MemoryStream Pin
Jacob D Dixon20-Jan-09 11:18
Jacob D Dixon20-Jan-09 11:18 
AnswerRe: Stream / MemoryStream Pin
Wendelius20-Jan-09 11:47
mentorWendelius20-Jan-09 11:47 
GeneralRe: Stream / MemoryStream Pin
Jacob D Dixon20-Jan-09 12:32
Jacob D Dixon20-Jan-09 12:32 
AnswerRe: Stream / MemoryStream Pin
Ennis Ray Lynch, Jr.20-Jan-09 12:46
Ennis Ray Lynch, Jr.20-Jan-09 12:46 
AnswerRe: Stream / MemoryStream Pin
Guffa20-Jan-09 12:56
Guffa20-Jan-09 12:56 
GeneralRe: Stream / MemoryStream Pin
Jacob D Dixon20-Jan-09 13:06
Jacob D Dixon20-Jan-09 13:06 
GeneralRe: Stream / MemoryStream Pin
Jacob D Dixon20-Jan-09 13:38
Jacob D Dixon20-Jan-09 13:38 
GeneralRe: Stream / MemoryStream Pin
Ben Fair21-Jan-09 2:36
Ben Fair21-Jan-09 2:36 
I think that overall the solution is good. It's definitely better to process the data in memory rather than writing it to disk only to delete it right afterwards. Especially since the only reason for writing to disk was to use the file as an Attachment. Writing to disk is slower and more 'risky' than working with the data in memory. One small improvement would be to put a try/catch/finally around the while loop and use the finally clause to close the database resources. Another would be to declare buffer as the correct type just one time outside the loop and use safe type conversion followed by testing it for null. Another thing to improve might be accessing the 'Filename' field without testing for DBNull or anything; but that may not be an issue for your data (you might have the field declared not null in the database, or might be defaulting it to '' if it's null in your query, etc.).

try
{
    byte[] buffer = null; <----- declare it here as null and of type byte[]
    while(myReader.Read())
    {
        buffer = myReader["Saved_File"] as byte[]; <----- safely convert the return value here
        if(buffer == null) <----- conversion failed, skip this item
            continue;
        mm.Attachments.Add(new Attachment(new MemoryStream(buffer), myReader["Filename"].ToString()));
    }
}
catch
{
     // <handle exception="" here="">
}
finally <----- close out database resources here
{
    if(myReader != null)
        myReader.Close();
    if(conn != null)
        conn.Close();
}</handle>


Keep It Simple Stupid! (KISS)

QuestionSolutions and Projects Pin
mobius11100120-Jan-09 9:24
mobius11100120-Jan-09 9:24 
AnswerRe: Solutions and Projects Pin
Wes Aday20-Jan-09 9:33
professionalWes Aday20-Jan-09 9:33 
AnswerRe: Solutions and Projects Pin
mobius11100120-Jan-09 9:44
mobius11100120-Jan-09 9:44 
GeneralRe: Solutions and Projects Pin
Wes Aday20-Jan-09 10:27
professionalWes Aday20-Jan-09 10:27 
GeneralRe: Solutions and Projects Pin
#realJSOP20-Jan-09 10:46
professional#realJSOP20-Jan-09 10:46 
GeneralRe: Solutions and Projects Pin
led mike20-Jan-09 11:42
led mike20-Jan-09 11:42 
GeneralRe: Solutions and Projects Pin
#realJSOP20-Jan-09 23:27
professional#realJSOP20-Jan-09 23:27 
GeneralRe: Solutions and Projects Pin
PIEBALDconsult21-Jan-09 8:05
mvePIEBALDconsult21-Jan-09 8:05 
GeneralRe: Solutions and Projects Pin
mobius11100121-Jan-09 2:18
mobius11100121-Jan-09 2:18 
GeneralRe: Solutions and Projects Pin
Wes Aday21-Jan-09 2:54
professionalWes Aday21-Jan-09 2:54 
QuestionRe: Solutions and Projects Pin
mobius11100121-Jan-09 3:01
mobius11100121-Jan-09 3:01 
AnswerRe: Solutions and Projects Pin
Wes Aday21-Jan-09 3:56
professionalWes Aday21-Jan-09 3:56 
AnswerRe: Solutions and Projects Pin
led mike20-Jan-09 11:42
led mike20-Jan-09 11:42 
AnswerRe: Solutions and Projects Pin
PIEBALDconsult21-Jan-09 8:09
mvePIEBALDconsult21-Jan-09 8:09 
QuestionDeploying an application with sql database Pin
R.Binu Port Blair20-Jan-09 9:20
R.Binu Port Blair20-Jan-09 9:20 
AnswerRe: Deploying an application with sql database Pin
Wendelius20-Jan-09 10:00
mentorWendelius20-Jan-09 10:00 
Questionhow to change mouse cursor (change image) outside form? Pin
ping_jacob20-Jan-09 8:58
ping_jacob20-Jan-09 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.