将图像上载到SQL数据库

时间:2013-08-02 10:16:01

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


        }

我收到以下错误

  

DbType System.Drawing.Bitmap没有映射到已知的   SqlCeType。

但我不知道为什么我没有成功。任何建议都将受到高度赞赏。

2 个答案:

答案 0 :(得分:4)

尝试将图像转换为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类型

答案 1 :(得分:0)

切出这个链接..

http://www.codeproject.com/Articles/25956/Sending-Receiving-PictureBox-Image-in-C-To-From-Mi

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

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

相关问题