如何将图像添加到类并保存在数据库中 [英] how To add image To a class and save in database

查看:53
本文介绍了如何将图像添加到类并保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义好的类,我想向该类添加图像,我该怎么做:

I have a class like defined i would like to add image to this class how can I do this:

public class CLASSFIVE
{
    public CLASSFIVE()
    {
        // Insert code required on object creation below this point.
    }

    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Contact { get; set; }
    public string GuardianPhone { get; set; }
    public string DOB { get; set; }
    public string Address { get; set; }
    public object State { get; set; }
    public string Gender { get; set; }
    public string Nationality { get; set; }
    public string Disability { get; set; }
    public object photo { get; set; }

    static string MYCONNEC = ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;

    public DataTable Select()
    {
        SqlConnection conn = new SqlConnection(MYCONNEC);
        
        DataTable dt = new DataTable();

        try
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_c5", conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);

            if (conn.State != ConnectionState.Open)
            { 
                conn.Open(); 
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return dt;
    }

    public bool Insert(CLASSFIVE c5)
    {
        bool isSuccess = false;

        SqlConnection conn = new SqlConnection(MYCONNEC);

        try
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO tbl_c5 (FirstName, MiddleName, LastName, Contact, GuardianPhone, DOB, Address,State, Gender, Nationality, Disability) VALUES (@FirstName, @MiddleName, @LastName, @Contact, @GuardianPhone, @DOB, @Address,@State, @Gender, @Nationality, @Disability)", conn);
            cmd.Parameters.AddWithValue("@FirstName", c5.FirstName);
            cmd.Parameters.AddWithValue("@MiddleName", c5.MiddleName);
            cmd.Parameters.AddWithValue("@LastName", c5.LastName);
            cmd.Parameters.AddWithValue("@Contact", c5.Contact);
            cmd.Parameters.AddWithValue("@GuardianPhone", c5.GuardianPhone);
            cmd.Parameters.AddWithValue("@DOB", c5.DOB);
            cmd.Parameters.AddWithValue("@Address", c5.Address);
            cmd.Parameters.AddWithValue("@State", c5.State);
            cmd.Parameters.AddWithValue("@Gender", c5.Gender);
            cmd.Parameters.AddWithValue("@Nationality", c5.Nationality);
            cmd.Parameters.AddWithValue("@Disability", c5.Disability);
            cmd.Parameters.AddWithValue("@photo", c5.Photo);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open(); 
            }

            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }

    public bool Update(CLASSFIVE c5)
    {
        bool isSuccess = false;

        SqlConnection conn = new SqlConnection(MYCONNEC);

        try
        {
            SqlCommand cmd = new SqlCommand("UPDATE tbl_c5 SET FirstName=@FirstName, MiddleName=@MiddleName, LastName=@LastName, Contact=@Contact, GuardianPhone=@GuardianPhone, DOB=@DOB, Address=@Address, Gender=@Gender, Nationality=@Nationality, Disability=@Disability WHERE ContactID=@ContactID", conn);
            cmd.Parameters.AddWithValue("@FirstName", c5.FirstName);
            cmd.Parameters.AddWithValue("@MiddleName", c5.MiddleName);
            cmd.Parameters.AddWithValue("@LastName", c5.LastName);
            cmd.Parameters.AddWithValue("@Contact", c5.Contact);
            cmd.Parameters.AddWithValue("@GuardianPhone", c5.GuardianPhone);
            cmd.Parameters.AddWithValue("@DOB", c5.DOB);
            cmd.Parameters.AddWithValue("@Address", c5.Address);
            cmd.Parameters.AddWithValue("@State", c5.State);
            cmd.Parameters.AddWithValue("@Gender", c5.Gender);
            cmd.Parameters.AddWithValue("@Nationality", c5.Nationality);
            cmd.Parameters.AddWithValue("@Disability", c5.Disability);
            cmd.Parameters.AddWithValue("@photo", c5.Photo);
            cmd.Parameters.AddWithValue("ContactID", c5.ContactID);
                
            if (conn.State != ConnectionState.Open)
            { 
                conn.Open(); 
            }

            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }

    public bool Delete(CLASSFIVE c5)
    {
        bool isSuccess = false;

        SqlConnection conn = new SqlConnection(MYCONNEC);

        try
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM tbl_c5 WHERE ContactID=@ContactID", conn);

            cmd.Parameters.AddWithValue("@ContactID", c5.ContactID);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open(); 
            }

            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }
}

我有一个这样的保存按钮,其中引用了上面的类,但是我想在该按钮上添加图像,请查看下面的代码,看看是否可以帮助我解决这个问题:

And I have a save button like this in which I have referenced the class above but I would like to add and image to this button please view the code below and see if you can help me out in solving this problem:

private void BTNSAVE_Click(object sender, RoutedEventArgs e)
{
    try
    {
        if (txtFirstName.Text == "" || txtMiddelName.Text == "" || txtLastName.Text == "" || txtGuardianPhone.Text == "" || txtNationality.Text == "" || txtstate.Text == "" || txtGender.Text == "" || txtDisability.Text == "")
        {
            MessageBox.Show("Required Field:FirstName,MiddleName,LastName, \n GuardianPhone,State/Province,Gender, \n Nationality,Disability \n Save Aborted; enter accurate values");
        }
        else
        {
            using (SqlConnection conn = new SqlConnection(MYCONNEC))
            {
                c5.FirstName = txtFirstName.Text;
                c5.MiddleName = txtMiddelName.Text;
                c5.LastName = txtLastName.Text;
                c5.Contact = txtContact.Text;
                c5.GuardianPhone = txtGuardianPhone.Text;
                c5.DOB = txtDOB.Text;
                c5.Address = txtAddress.Text;
                c5.State = txtstate.Text;
                c5.Gender = txtGender.Text;
                c5.Nationality = txtNationality.Text;
                c5.Disability = txtDisability.Text;

                bool success = c5.Insert(c5);

                if (success == true)
                {
                    LBLDP.Content = "Saved Successfully!!";
                    Refresh();
                    Clear();
                }
                else
                {
                    MessageBox.Show("Contact Not Saved, Try Again");
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "MGT Message", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

请在此应用程序的保存"按钮中添加 c5.photo ,有人可以帮我吗?

Please I would like To add a c5.photo to the save button in this application, can someone help me out please?

推荐答案

您不能直接将图像保存在类对象和数据库中.为此,您可以通过两种方式做到这一点:

You can not directly save the image in the class object and in the database. To achieve this you can do this in two ways:

  1. 您从请求中获取图像,将其保存到服务器,然后将该特定文件的路径保存在数据库中.这是实现此目标的最佳方法,因为它不会对您的数据库大小产生任何影响.

  1. You get the image from the request, save it to your server, and save the path of that particular file in the database. This is the best way to achieve this as it will not hit any impact on your database size.

如果仅要将文件保存在数据库中,则可以将数据库列数据类型用作BLOB,这允许您保存文件或冗长的对象.在课堂上,您将制作照片道具.作为字符串并保存图像或文件的base64(推荐),或将该属性设置为Stream并将文件流保存在此特定对象中,并以相同的方式推送到数据库.

If you want to save the file in the database only then you can use the database column datatype as BLOB which allows you to save the file or lengthy object. In your class, you make the photo prop. as the string and save the base64 of the image or file (recommended) or make the property as Stream and save the file stream in this particular object and same push to database.

这篇关于如何将图像添加到类并保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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