Upisi u bazu podataka
Code:
private void SaveToSqlDatabase(AtalaImage image)
{
SqlConnection myConnection = null;
try
{
// Save image to byte array.
byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));
// Create the SQL statement to add the image data.
myConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand myCommand = new SqlCommand
("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection);
SqlParameter myParameter = new SqlParameter("@Image", SqlDbType.Image, imagedata.Length);
myParameter.Value = imagedata;
myCommand.Parameters.Add(myParameter);
// Open the connection and execture the statement.
myConnection.Open();
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
}
}
Procitaj iz Baze podataka
Code:
private AtalaImage OpenFromSqlDatabase()
{
SqlConnection myConnection = null;
try
{
// Establish connection and SELECT statement.
myConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand myCommand = new SqlCommand
("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection);
myConnection.Open();
// Get the image from the database.
byte[] imagedata = (byte[])myCommand.ExecuteScalar();
if (imagedata != null)
{
AtalaImage image = AtalaImage.FromByteArray(imagedata);
return image;
}
else
{
MessageBox.Show("Image does not exist in database.");
return null;
}
}
finally
{
myConnection.Close();
}
}