Showing posts with label Do you want fries with that?. Show all posts
Showing posts with label Do you want fries with that?. Show all posts

Monday, March 1, 2010

Waiting Game Photos -- With Soupcon of Source Code

While I am playing a waiting game in life, I have nothing for a topic on my blog today, so I will blog about nothing. I do have some pictures though. I have lots of pictures.

The first is of a tropical sunrise.


The second is of people trying to enjoy themselves on a cold beach.


So I sit here and write software code. What is the latest thing that I put into the beast? A file chooser to upload a blob to a database. Here is the source code snippet:


Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
long FileSize;
byte[] rawData;
MySqlCommand cmd = new MySqlCommand();

openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string filname = openFileDialog1.FileName;
using (myStream)
{
// Insert code to read the stream here.
FileSize = myStream.Length;
int fsint = (int)FileSize;
rawData = new byte[FileSize];
myStream.Read(rawData, 0, fsint);
myStream.Close();
string SQL = "insert into attachments (filename, filesize, blobdata, userPKID, messPKID) values (?Filname, ?Filsiz, ?File, 2, 1)";
cmd.Connection = conn;
cmd.CommandText = SQL;
cmd.Parameters.AddWithValue("?Filname", filname);
cmd.Parameters.AddWithValue("?Filsiz", FileSize);
cmd.Parameters.AddWithValue("?File", rawData);
cmd.ExecuteNonQuery();
MessageBox.Show("File Inserted into database successfully!",
"Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);



}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}

}