山本

导航

图片上传到数据库

 protected void btnAdd_Click(object sender, EventArgs e)
        {
            string connstr = "data source=连接到计算机;initial catalog=student;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 boke (FIamge) values(@FImage)";
                    cmd.Parameters.Add("@FImage", SqlDbType.Image);
                    cmd.Parameters["@FImage"].Value = PhotoArray;
                    cmd.ExecuteNonQuery();
                }
            }
        }

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