c# - 将图像上传到 SQL 数据库

标签 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(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");


        }

我收到以下错误

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

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

最佳答案

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

 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类型

关于c# - 将图像上传到 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18014465/

相关文章:

c# - 通过C#程序调用手机

sql - TSQL : OR Status of Previous month

c# - 如何在sql server中获取这一列的总和

c# - 适用于 Windows Phone 8.1 Silverlight 的数据库

c# - 带有 redis 背板的横向扩展信号服务器无法正常工作(vue 前端)

c# - 为外部集合中的每个项目断言内部集合中的元素数量

c# - 如何在c#中获取通用日期时间

asp.net-core - ASP.NET Core 现在是否支持 SQL Server CE?

sql-server-2008 - 如何部署SQL Server Compact Edition 4.0?

c# - 如何在 ASP.NET Core 中获取 Active Directory 当前用户显示名称?