Database Reference
In-Depth Information
cmd.Parameters.Add("@imagefile", SqlDbType.NVarChar, 20);
cmd.Parameters.Add("@imagedata", SqlDbType.Image, 1000000);
cmd.Prepare();
}
The Executelnsertlmages method accepts an integer to use as a suffix for the image file name, calls
LoadlmageFile to get a byte array containing the image, assigns the file name and image to their
corresponding command parameters, and then executes the command to insert the image.
private void ExecuteInsertImages(int imageFileNumber)
{
string imageFileName = null;
byte[] imageImageData = null;
imageFileName = imageFilePrefix + imageFileNumber.ToString() + imageFileType;
imageImageData = LoadImageFile(imageFileName, imageFileLocation, maxImageSize);
cmd.Parameters["@ImageFile"].Value = imageFileName;
cmd.Parameters["@ImageData"].Value = imageImageData;
ExecuteCommand(cmd.CommandText);
}
The LoadlmageFile method reads the image file, displays the file name and number of bytes in the
file, and returns the image as a byte array.
private byte[] LoadImageFile(string fileName,string fileLocation,int maxImageSize)
{
byte[] imagebytes = null;
string fullpath = fileLocation + fileName;
txtLoadImages.AppendText("Loading File:");
txtLoadImages.AppendText(Environment.NewLine);
txtLoadImages.AppendText(fullpath);
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(maxImageSize);
txtLoadImages.AppendText(Environment.NewLine);
txtLoadImages.AppendText("Imagebytes has length " +
imagebytes.GetLength(0).ToString() + " bytes.");
return imagebytes;
}
Retrieving Images from a Database
Now that you've stored some image(s), you'll see how to retrieve and display them with a Windows
Forms application.
 
Search WWH ::




Custom Search