Line 48: </Columns> Line 49: </asp:GridView> Line 50: <asp:SqlDataSource ID="SqlDataSource1" runat="server" Line 51: ConnectionString="<%$ ConnectionStrings:ConnectionString %>" Line 52: SelectCommand="SELECT [ID], [ImageName], [Image]
using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; using System.Data; public partial class fileupload : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnUpload_Click(object sender, EventArgs e) { string strImageName = txtName.Text.ToString(); if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") { byte[] imageSize = new byte [FileUpload1.PostedFile.ContentLength]; HttpPostedFile uploadedImage = FileUpload1.PostedFile; uploadedImage.InputStream.Read (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength); // Create SQL Connection SqlConnection con = new SqlConnection("server=USER\\SQLEXPRESS;user id=;password;database=images"); con.ConnectionString = ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString; // Create SQL Command SqlCommand cmd = new SqlCommand(); cmd.CommandText = "INSERT INTO Images(ImageName,Image)" + " VALUES (@ImageName,@Image)"; cmd.CommandType = CommandType.Text; cmd.Connection = con; SqlParameter ImageName = new SqlParameter ("@ImageName", SqlDbType.VarChar, 50); ImageName.Value = strImageName.ToString(); cmd.Parameters.Add(ImageName); SqlParameter UploadedImage = new SqlParameter ("@Image", SqlDbType.Image, imageSize.Length); UploadedImage.Value = imageSize; cmd.Parameters.Add(UploadedImage); con.Open(); int result = cmd.ExecuteNonQuery(); con.Close(); if (result > 0) lblMessage.Text = "File Uploaded"; GridView1.DataBind(); } } } <%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.Configuration; using System.Data.SqlClient; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { SqlConnection con = new SqlConnection("server=USER\\SQLEXPRESS;user id=;password;database=images"); con.ConnectionString = ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString; // Create SQL Command SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Select ImageName,Image from Images" + " where ID =@ID"; cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = con; SqlParameter ImageID = new SqlParameter ("@ID", System.Data.SqlDbType.Int); ImageID.Value = context.Request.QueryString["ID"]; cmd.Parameters.Add(ImageID); con.Open(); SqlDataReader dReader = cmd.ExecuteReader(); dReader.Read(); context.Response.BinaryWrite((byte[])dReader["Image"]); dReader.Close(); con.Close(); } public bool IsReusable { get { return false; } } }
<connectionstrings> <add name="ConnectionString"> connectionString='your connection string' /> </connectionstrings>
["ConnectionString"]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)