图片上传到数据库中去

来源:互联网 发布:美国文献数据库 编辑:程序博客网 时间:2024/05/22 07:05
string strPath = this.FileUpload3.PostedFile.ToString();//图片路径
        FileStream fs = new System.IO.FileStream(strPath ,FileMode.Open,FileAccess.Read );
        BinaryReader br = new BinaryReader(fs);
        byte[] photo = br.ReadBytes((int)fs.Length);
        br.Close();
        fs.Close();
        //sava in
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mysql"].ConnectionString))
        {
            string sql = "insert into Image values (4 ,@Image) ";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.Parameters.Add("@Image",SqlDbType.Binary,photo.Length);
            comm.Parameters ["@Image"].Value=photo;
            conn.Open();
            if(comm.ExecuteNonQuery()>0)
            {
                Response.Write("上传成功");


            }
            conn.Close();
          
        }
0 0
原创粉丝点击