山本

导航

将图片上传到数据库中

在前台加上input 和button就可以了

 protected void btnAdd_Click(object sender, EventArgs e)
        {
            string connstr = "data source=连接的是计算机名;initial catalog=数据库名;User Id=sa;password=123";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //获得图象并把图象转换为byte[]
                    HttpPostedFile upPhoto = UpPhoto.PostedFile;
                    int upPhotoLength = upPhoto.ContentLength;
                    byte[] PhotoArray = new Byte[upPhotoLength];
                    Stream PhotoStream = upPhoto.InputStream;
                    PhotoStream.Read(PhotoArray, 0, upPhotoLength);
                    cmd.CommandText = "insert into 数据表名 (字段名) values(@FImage)";
                    cmd.Parameters.Add("@FImage", SqlDbType.Image);//参数替换
                    cmd.Parameters["@FImage"].Value = PhotoArray;
                    cmd.ExecuteNonQuery();
                }
            }
        }

posted on 2013-01-04 00:05  高级菜鸟  阅读(241)  评论(0编辑  收藏  举报