Create an Image field in the database - it needs to be image as the size limits are too small on the other datatypes.
At the same time, create a nvarchar(Max) field for the filename.
Handle the button click event of the Upload control.
In the handler, the upload control has two properties you are interested in: FileName (which is just the name, no path information is ever available) and FileBytes which contains the actual file content.
This method does more than you need, but it gives you the idea:
private string SaveUpload(FileUpload fl)
{
if (fl.HasFile)
{
try
{
int version = 0;
string filename = Path.GetFileName(fl.FileName);
byte[] filedata = fl.FileBytes;
string strCon = ConnectionStrings.Download;
using (SqlConnection con = new SqlConnection(strCon))
{
con.Open();
using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
{
ver.Parameters.AddWithValue("@FN", filename);
object o = ver.ExecuteScalar();
if (o != null && o != System.DBNull.Value)
{
version = (int) o + 1;
}
}
using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
"VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
{
ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
ins.Parameters.AddWithValue("@FN", filename);
ins.Parameters.AddWithValue("@DS", "");
ins.Parameters.AddWithValue("@DT", filedata);
ins.Parameters.AddWithValue("@VS", version);
ins.Parameters.AddWithValue("@UD", DateTime.Now);
ins.ExecuteNonQuery();
}
}
return string.Format("{0} uploaded, version = {1}", filename, version);
}
catch (Exception ex)
{
return "The file could not be uploaded. The following error occured: " + ex.Message;
}
}
return "Please select a file.";
}