The Personal Web Site Starter Kit has great sample code on how to upload image to data base. The link is shown below:
http://www.asp.net/default.aspx?tabindex=5&tabid=41
Or just go to www.asp.net > Downloads.
This template project's Photo.aspx page uses
<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" SelectMethod="GetPhotos" InsertMethod="AddPhoto" DeleteMethod="RemovePhoto" UpdateMethod="EditPhoto" >
In the PhotoManager, check out the AddPhoto method:
public static void AddPhoto(int AlbumID, string Caption, byte[] BytesOriginal) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) {
using (SqlCommand command = new SqlCommand("AddPhoto", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
command.Parameters.Add(new SqlParameter("@Caption", Caption));
command.Parameters.Add(new SqlParameter("@BytesOriginal", BytesOriginal));
command.Parameters.Add(new SqlParameter("@BytesFull", ResizeImageFile(BytesOriginal, 600)));
command.Parameters.Add(new SqlParameter("@BytesPoster", ResizeImageFile(BytesOriginal, 198)));
command.Parameters.Add(new SqlParameter("@BytesThumb", ResizeImageFile(BytesOriginal, 100)));
connection.Open();
command.ExecuteNonQuery();
}
}
}
Then in the SQL database, check out the AddPhoto store proceedure:
ALTER PROCEDURE AddPhoto
@AlbumID int,
@Caption nvarchar(50),
@BytesOriginal image,
@BytesFull image,
@BytesPoster image,
@BytesThumb image
AS
INSERT INTO [Photos] (
[AlbumID],
[BytesOriginal],
[Caption],
[BytesFull],
[BytesPoster],
[BytesThumb] )
VALUES (
@AlbumID,
@BytesOriginal,
@Caption,
@BytesFull,
@BytesPoster,
@BytesThumb )
RETURN
This is a better way to upload files to DB than the way I use before. By using a store proceedure, the processing is off loaded to the DB server from Web Server. Though, these two servers are the same PC so it did not make any improvement in this regard. But this pattern is more scalable.