Upload images to SQL Server 2005 using ASP.Net MVC?

前端 未结 4 1045
闹比i
闹比i 2021-01-14 06:53

I know there is a way to upload images to the database as image type or varbinary type, however, I searched around the entire week, I am unable to find anything that can hel

4条回答
  •  盖世英雄少女心
    2021-01-14 07:37

    If you're okay storing the image as a VARCHAR, here is some code to do so.

        String b64;
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        {
            this.pic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Byte[] bytes = ms.ToArray();
            b64 = Convert.ToBase64String(bytes);
        }
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("UPDATE [settings] SET [value] = @val WHERE [id] = 2", conn))
            {
                conn.Open();
                cmd.Parameters.Add(new SqlParameter("@val", b64));
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    

提交回复
热议问题