
C#中我有一个界面,界面上有一个pictureBox控件,我想把这个控件上的图片存到数据库
因为我做的是一个项目,所以数据库操作是单独分开的,publicstaticvoidAddEmployee(Employeeemployee){stringstrField...
因为我做的是一个项目,所以数据库操作是单独分开的,
public static void AddEmployee(Employee employee)
{
string strField = "name, officeId, password, cardNumber,gender,nation,religion,homeTown,marriage,politics,eduction,DOB,workPhone,mobile,homePhone,homeAddress,mailingAddress,jobNumber,jobTitle,skillLevel,ID,socailId,entryDate,leaveDate,email,comment,photo";
string strData = string.Format("'{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}', '{7}', '{8}', '{9}', '{10}','{11}','{12}', '{13}','{14}', '{15}', '{16}', '{17}', '{18}','{19}', '{20}','{21}', '{22}', '{23}', '{24}','{25}','{26}'",
employee.name, employee.officeId, employee.password,employee.cardNumber,employee.gender,employee.nation,employee.religion,employee.homeTown,employee.marriage,employee.politics,employee.education,
employee.DOB,employee.workPhone,employee.mobile,employee.homePhone,employee.homeAddress,employee.mailingAddress,employee.jobNumber,employee.jobTitle,employee.skillLevel,
employee.ID,employee.socailId,employee.entryDate,employee.leaveDate,employee.email,employee.comment,employee.photo);
sqladd("Employee", strField, strData);
}我跟踪employee.photo貌似没有值
m_employee.leaveDate = dtpLeaveDate.Value.ToShortDateString().Trim();
m_employee.email = textEmail.Text.Trim();
m_employee.comment = textComment.Text.Trim();
m_employee.photo = 该怎么写??
private void btnGetPhoto_Click(object sender, EventArgs e)
{
//System.IO.FileStream fs;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "D:\\";
openFileDialog.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp";
openFileDialog.RestoreDirectory = true;//保存当前路径
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
pbPhoto.Load(fName);//实现图片更改
//图片保存到数据库
path = openFileDialog.FileName;//文件路径
}
}
private void saveToData()
{
//path 如果没有变动的话,值取不到,是否要给个默认值?
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] photo = new byte[(Int32)stream.Length];
stream.Read(photo, 0, photo.Length);
string name = textUserName.Text.ToString();
string strSql = string.Format("update Employee set photo = '{0}' where employeeId = '{1}'", photo, name);
SQLServer.SQLInquiry(strSql);
stream.Close();
}我出来照片到数据库 展开
public static void AddEmployee(Employee employee)
{
string strField = "name, officeId, password, cardNumber,gender,nation,religion,homeTown,marriage,politics,eduction,DOB,workPhone,mobile,homePhone,homeAddress,mailingAddress,jobNumber,jobTitle,skillLevel,ID,socailId,entryDate,leaveDate,email,comment,photo";
string strData = string.Format("'{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}', '{7}', '{8}', '{9}', '{10}','{11}','{12}', '{13}','{14}', '{15}', '{16}', '{17}', '{18}','{19}', '{20}','{21}', '{22}', '{23}', '{24}','{25}','{26}'",
employee.name, employee.officeId, employee.password,employee.cardNumber,employee.gender,employee.nation,employee.religion,employee.homeTown,employee.marriage,employee.politics,employee.education,
employee.DOB,employee.workPhone,employee.mobile,employee.homePhone,employee.homeAddress,employee.mailingAddress,employee.jobNumber,employee.jobTitle,employee.skillLevel,
employee.ID,employee.socailId,employee.entryDate,employee.leaveDate,employee.email,employee.comment,employee.photo);
sqladd("Employee", strField, strData);
}我跟踪employee.photo貌似没有值
m_employee.leaveDate = dtpLeaveDate.Value.ToShortDateString().Trim();
m_employee.email = textEmail.Text.Trim();
m_employee.comment = textComment.Text.Trim();
m_employee.photo = 该怎么写??
private void btnGetPhoto_Click(object sender, EventArgs e)
{
//System.IO.FileStream fs;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "D:\\";
openFileDialog.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp";
openFileDialog.RestoreDirectory = true;//保存当前路径
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
pbPhoto.Load(fName);//实现图片更改
//图片保存到数据库
path = openFileDialog.FileName;//文件路径
}
}
private void saveToData()
{
//path 如果没有变动的话,值取不到,是否要给个默认值?
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] photo = new byte[(Int32)stream.Length];
stream.Read(photo, 0, photo.Length);
string name = textUserName.Text.ToString();
string strSql = string.Format("update Employee set photo = '{0}' where employeeId = '{1}'", photo, name);
SQLServer.SQLInquiry(strSql);
stream.Close();
}我出来照片到数据库 展开
3个回答
展开全部
为什么不直接保存图片路径?一般很少在数据库直接存图片的
追问
这个要求存照片,图片丢了,路径就没办法找到
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询