private void Button1_Click(object sender, System.EventArgs e)
{
byte[] fileData = null;
if (File2 != null)
{
// Make sure the file has data.
if ((File2.PostedFile != null) && (File2.PostedFile.ContentLength > 0))
{
// Get the filename.
string fn = System.IO.Path.GetFileName(File2.PostedFile.FileNa me);
try
{
// Access the file stream and begin the upload. Store the file in a memory byte array.
Stream MyStream = File2.PostedFile.InputStream;
long iLength = MyStream.Length;
fileData = new byte[(int)MyStream.Length];
MyStream.Read(fileData, 0, (int)MyStream.Length);
MyStream.Close();
}
catch (Exception excep)
{
//return false;
}
}
}
SqlConnection MyConnection=new SqlConnection("user id=xx;password=12345;database=northwind;server=xyz ");
SqlCommand MyCommand = new SqlCommand("INSERT INTO MYTABLE (IMAGE_DATA) VALUES (@doc_content)", MyConnection);
SqlParameter param1 = new SqlParameter("@doc_content", SqlDbType.Image);
param1.Value = fileData;
MyCommand.Parameters.Add(param1);
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
private void Button2_Click(object sender, System.EventArgs e)
{
SqlConnection MyConnection=new SqlConnection("user id=xx;password=12345;database=northwind;server=xyz ");
SqlCommand MyCommand = new SqlCommand("SELECT IMAGE_DATA FROM MYTABLE WHERE ID=" + Convert.ToInt32(Selectid.Text), MyConnection);
MyConnection.Open();
SqlDataReader MyReader = MyCommand.ExecuteReader();
if (MyReader.Read())
{
byte[] m_MyImage = (byte[])MyReader["IMAGE_DATA"];
Response.BinaryWrite(m_MyImage);
}
}