Inserting Image into the database
How do I insert an image into the database?
In the ASPX page, have a asp:FileUpload control and a Submit button. On clicking the submit button, the following code inserts into the table - Image and column - ImageStream, of type varbinary
<asp:FileUpload ID="fup" runat="server" /> <asp:Button ID="btnSubmit" runat="server" Text="Hello" OnClick="btnSubmit_Click" />
protected void btnSubmit_Click(object sender, EventArgs e) { byte [] bytes = fup.FileBytes; SqlConnection conn = new SqlConnection("Data Source=localhost; Database=Pubs; Integrated Security=true;"); conn.Open(); string sql = "INSERT INTO IMAGE (ImageStream) VALUES (@bytes)"; SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddWithValue("@bytes", bytes); comm.ExecuteNonQuery(); }
Posted by Vijay on 04-Sep-2009 02:42 PM
Category : ASP.NET WebForms
Category : ASP.NET WebForms
Commented by
Ravi at 10-Oct-2009 06:17 PM
This is very helpful to me


