How to upload image from C# application to SQL Server 2005

Possible Duplicate:
Upload image to server using C#/.NET and storing filename in DB

How to upload image from a C# application to SQL Server 2005

  • Prevent a Stored Procedure from being executed twice at the same time
  • left/right join
  • Passing SQL Server connection around (node to c#)
  • Need to capture database name when error occurs with sp_msforeachdb
  • How to string replace at SQL Server data rows
  • Making a backup of an SQL Server database
  • sp_xml_preparedocument fails to return in openquery statement? (The metadata could not be determined)
  • Dynamic Grouping
  • Dynamically Joining a table based on value in recordset
  • Concatenate row record base on group by in SQL Server 2008
  • What's the purpose of distinct in this query?
  • Get SQL xml attribute value using variable
  • One Solution collect form web for “How to upload image from C# application to SQL Server 2005”

    using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    const string SQL = "INSERT INTO [BinaryTable] ([FileName], [DateTimeUploaded], [MIME], [BinaryData]) VALUES (@FileName, @DateTimeUploaded, @MIME, @BinaryData)";
    SqlCommand cmd = new SqlCommand(SQL, Conn);
    cmd.Parameters.AddWithValue("@FileName", FileName.Text.Trim());
    cmd.Parameters.AddWithValue("@MIME", FileToUpload.PostedFile.ContentType);
    byte[] imageBytes = new byte[FileToUpload.PostedFile.InputStream.Length + 1];
    FileToUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
    cmd.Parameters.AddWithValue("@BinaryData", imageBytes);
    cmd.Parameters.AddWithValue("@DateTimeUploaded", DateTime.Now);
    lit_Status.Text = "<br />File successfully uploaded - thank you.<br />";

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.