将图像上传到 SQL 数据库 [英] Upload image to SQL Database

查看:39
本文介绍了将图像上传到 SQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案,但不知道我哪里做错了.但我是第一次这样做.

I have been looking at solutions but don't know where I am doing it wrong. But I am doing it for the first time.

我有一班学生

class Students
{

    public Image photo { get; set; }


    public bool AddStudent(Students _student)
    {
        Settings mySettings = new Settings();

        SqlCeConnection conn = new SqlCeConnection(mySettings.StudentsConnectionString);
        SqlCeCommand cmd = new SqlCeCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = conn;
        cmd.CommandText = "insert into students (firstname, lastname, dob, allergic, allergydetails, memo, address, photo) " +
                            "Values (" +
                            "'" + @FirstName + "'," +
                            "'" + @LastName + "'," +
                            "'" + @Dob + "'," +
                            "'" + @isAllergic + "'," +
                            "'" + @AllergyDetails + "'," +
                            "'" + @Memo + "'," +
                            "'" + @photo + "'," +
                            "'" + @Address + "')";

        cmd.Parameters.Add("@FirstName", _student.FirstName);
        cmd.Parameters.Add("@LastName", _student.LastName);
        cmd.Parameters.Add("@Dob", _student.Dob);
        cmd.Parameters.Add("@isAllergic", _student.isAllergic);
        cmd.Parameters.Add("@AllergyDetails", _student.AllergyDetails);
        cmd.Parameters.Add("@Memo", _student.Memo);


        cmd.Parameters.Add("@photo", _student.photo);

        cmd.Parameters.Add("@Address", _student.Address);

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
            return false;
        }
        finally
        {
            if (conn.State == System.Data.ConnectionState.Open) conn.Close();
            cmd = null;
        }
    }  

现在我像这样从表单中传递属性值.

Now I pass the property values from my form like this.

  private void btnAdd_Click(object sender, EventArgs e)
        {

            Students myStudent = new Students();

            myStudent.FirstName = txtFirstName.Text.Trim();
            myStudent.LastName = txtLastName.Text.Trim();
            myStudent.Dob = dtPicker1.Value;
            myStudent.Memo = txtMemo.Text.Trim();
            myStudent.Address = txtAddress.Text.Trim();

            myStudent.photo = Image.FromFile(openFileDialog1.FileName);

            // Insert New Record
            if (myStudent.AddStudent(myStudent))
                MessageBox.Show("Student Added Successfully");


        }

我收到以下错误

不存在从 DbType System.Drawing.Bitmap 到已知的映射SqlCeType.

No mapping exists from DbType System.Drawing.Bitmap to a known SqlCeType.

但我不知道为什么我没有成功.任何建议将不胜感激.

But I don't know why I am not succedding. Any Suggestion would be highly appreciated.

推荐答案

尝试将图像转换为 Byte[] 数组然后保存.

Try converting the image to Byte[] array and then save.

 private void btnAdd_Click(object sender, EventArgs e)
 {
    Students myStudent = new Students();
    ...
    ...
    // change the student photo to byte array e.g. 
    // public byte[] Photo {get;set;}
    myStudent.photo = imageToByteArray(Image.FromFile(openFileDialog1.FileName));
    ...
    ...
    // Insert New Record
    if (myStudent.AddStudent(myStudent))
    MessageBox.Show("Student Added Successfully");
 }

 // convert image to byte array
 public byte[] imageToByteArray(System.Drawing.Image imageIn)
 {
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
    return  ms.ToArray();
 }

//Byte array to photo
public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

注意:你在数据库中的DataType必须是image类型

Note: your DataType in database must be image type

这篇关于将图像上传到 SQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆