create table fupload(ImgName varchar(250),Img image)
using System.Data;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page { SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString); SqlCommand sqlcmd = new SqlCommand(); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { sqlcon.Open(); byte[] cnt = null; //Source directory to read files DirectoryInfo di = new DirectoryInfo("D:\\DNS\\samp"); //if you want only jpg files then filter it FileInfo[] ArrFi = di.GetFiles("*.jpg"); foreach (FileInfo fi in ArrFi) { cnt = FileToByteArray(fi.FullName); SqlCommand sqlcmd = new SqlCommand("insert into fupload(ImgName,Img) values (@Im, @Img)", sqlcon); sqlcmd.Parameters.Add("@Im", fi.Name); sqlcmd.Parameters.Add("@Img", cnt); sqlcmd.ExecuteNonQuery(); } } catch (Exception ex) { } finally { sqlcon.Close(); } } public byte[] FileToByteArray(string fname) { byte[] cnt = null; try { System.IO.FileStream fs = new System.IO.FileStream(fname, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); long tb = new System.IO.FileInfo(fname).Length; cnt = br.ReadBytes((Int32)tb); fs.Close(); fs.Dispose(); br.Close(); } catch (Exception ex) { } return cnt; }}