C# 将图像上载到SQL数据库

C# 将图像上载到SQL数据库,c#,sql,sql-server-ce,C#,Sql,Sql Server Ce,我一直在寻找解决方案,但不知道哪里做错了。但这是我第一次这么做 我有一班学生 class Students { public Image photo { get; set; } public bool AddStudent(Students _student) { Settings mySettings = new Settings(); SqlCeConnection conn = new SqlCeConnection(mySe

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

我有一班学生

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;
        }
    }  
现在我像这样从表单中传递属性值

  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

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

请查看此链接

您真的必须将图像存储在数据库中吗

如果您只需将图像存储在文件夹中(将其上载到应用程序),然后将该图像的路径存储在数据库的“Varchar”字段中,就可以轻松完成此任务。
我在网站上做了更多的工作,但我认为基本原理应该是一样的。…

尝试将图像转换为
字节[]
数组,然后保存

 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;
}

注意:数据库中的数据类型必须是
image
type

您需要将图像转换为字节[],然后根据我的经验,它可以存储在图像数据类型中,这是一段很长的时间。。。在将图像放入BLOB字段之前,您应该在DB中使用BLOB字段,并将图像流式传输到字节数组。您不明白,我的问题不同。我首先将照片保存在类实例中,然后将其上载到db。在这个例子中,我没有像openfiledialog.filename这样的功能。当然可以将图像上传到MS数据库,但是要确保图像质量低(缩略图),因为这样会降低数据库的速度。将图像存储在服务器上,并将图像路径作为字符串放入数据库以加载图像。评论不错。谢谢,我会记住的。但是如果我使用SQLCompactCE,会有什么不同吗?我现在遇到异常,转换不受支持。[Type to convert from(如果已知)=nvarchar,Type to convert to(如果已知)=binary]是的,我相信这是我现在应该做的事情。它是一个基于桌面的独立应用程序,使用本地数据库。因此,我最好只存储该图像的位置。或者可以将该图像复制到我定义的文件夹中,这样即使用户从该位置删除该图像,我们也会有一个copyHi。谢谢您的帮助,但它根本不起作用。我在cmd.executenonQuery()上收到此错误消息。错误是“不支持转换。[要转换的类型(如果已知)=nvarchar,要转换的类型(如果已知)=image]”以DB为单位的照片的数据类型是什么?