将图片上传到数据库中

it2022-05-21  65

在前台加上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();                }            }        }

转载于:https://www.cnblogs.com/qzc900809/archive/2013/01/04/2843512.html

相关资源:JSP实现将图片上传到数据库

最新回复(0)