如何将图像添加到类并保存在数据库中[重复]
Posted
技术标签:
【中文标题】如何将图像添加到类并保存在数据库中[重复]【英文标题】:how To add image To a class and save in database [duplicate] 【发布时间】:2021-02-23 01:26:36 【问题描述】:我有一个定义的类我想将图像添加到这个类我该怎么做:
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;
我有一个像这样的保存按钮,其中我引用了上面的类,但我想向这个按钮添加图像,请查看下面的代码,看看你是否可以帮助我解决这个问题:
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
,有人可以帮我吗?
【问题讨论】:
【参考方案1】:您不能直接将图像保存在类对象和数据库中。为此,您可以通过两种方式做到这一点:
您从请求中获取图像,将其保存到您的服务器,并将该特定文件的路径保存在数据库中。这是实现这一目标的最佳方式,因为它不会对您的数据库大小产生任何影响。
如果您只想将文件保存在数据库中,则可以将数据库列数据类型用作 BLOB,这样您就可以保存文件或冗长的对象。在你的课堂上,你制作照片道具。作为字符串并将图像或文件的 base64 保存(推荐)或将属性设置为 Stream 并将文件流保存在此特定对象中并同样推送到数据库。
【讨论】:
以上是关于如何将图像添加到类并保存在数据库中[重复]的主要内容,如果未能解决你的问题,请参考以下文章
尝试将 AlamoFire JSON 响应映射到类并接收“致命错误:在展开可选时意外发现 nil”