给你一段例子代码,上传图片进入数据库的,不懂可以继续提问:
private void UploadImage_Click(object sender, System.EventArgs e)
{
/// <summary>
/// ContentType holds the content type of the file that was added to the ImageToUpload
/// control, and HTML input control. </summary>
string ContentType = ImageToUpload.PostedFile.ContentType;
/// <summary>
/// Length holds the size of the file that was added to the ImageToUpload control </summary>
int Length = System.Convert.ToInt32(ImageToUpload.PostedFile.InputStream.Length);
/// <summary>
/// Content will hold the image. It is a byte array of size Length </summary>
byte[] Content = new byte[Length];
/// <summary>
/// The Read method is used to read the file from the ImageToUpload control </summary>
ImageToUpload.PostedFile.InputStream.Read(Content,0,Length);
/// <summary>
/// Open a connection to the SQL Server </summary>
SqlConnection Connection = new SqlConnection("server=localhost;uid=sa;pwd=;database=ImageUpload");
/// <summary>
/// The SqlCommand will be used to insert the image into the Images table </summary>
SqlCommand Command = new SqlCommand("INSERT Into Images(Description, ImageFile, ImageSize, ImageType) Values(@Description, @ImageFile, @ImageSize, @ImageType)", Connection);
/// <summary>
/// The Description parameter is used to add the image file description to the database
SqlParameter imageDescriptionParameter = new SqlParameter("@Description", SqlDbType.NVarChar);
imageDescriptionParameter.Value = imageDescription.Text;
Command.Parameters.Add(imageDescriptionParameter);
/// <summary>
/// The ImageFile parameter is used to add the image file to the database
SqlParameter imageFileParameter = new SqlParameter("@ImageFile", SqlDbType.Image);
imageFileParameter.Value = Content;
Command.Parameters.Add(imageFileParameter);
/// <summary>
/// The ImageSize parameter is used to add the image file size to the database
SqlParameter imageSizeParameter = new SqlParameter("@ImageSize", SqlDbType.Int);
imageSizeParameter.Value = Length;
Command.Parameters.Add(imageSizeParameter);
/// <summary>
/// The ImageType parameter is used to add the image file type to the database
SqlParameter imageTypeParameter = new SqlParameter("@ImageType", SqlDbType.NVarChar);
imageTypeParameter.Value = ContentType;
Command.Parameters.Add(imageTypeParameter);
/// <summary>
/// Open the connection in order to retrieve the record </summary>
Connection.Open();
/// <summary>
/// The SQL statement is executed. ExecuteNonQuery is used since no records
/// will be returned. </summary>
Command.ExecuteNonQuery();
/// <summary>
/// The connection is closed </summary>
Connection.Close();