c# - 将不同图片框中的多个图像保存到database.c中的不同字段#
问题描述
我试图将两个图片框中的两个图像保存到数据库,但图像没有添加到相关列,只有第一个图像保存到所有列,我需要解决这个问题(代码)。
try
{
if (isNIDValid == true & isNameValid == true & isFNameValid == true & isGFNameValid == true & isEmailValid == true & isPhoneValid == true)
{
byte[] img = null;
FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
string sql = "insert into owner(NID, owner_name, F_Name, G_Name,Email,Phone_No, Gender,NID_Photo, Owner_Photo)values('" + txtnid.Text + "', '" + txtname.Text + "', '" + txtfathername.Text + "','" + txtgrandfathername.Text + "','" + txtEmail.Text + "','" + txtPhone.Text + "', '" + Gender + "', @img,@img)";
Connectivity.openConnection();
da.InsertCommand = new SqlCommand(sql, Connectivity.cn);
da.InsertCommand.Parameters.Add(new SqlParameter("@img", img));
int x = da.InsertCommand.ExecuteNonQuery();
MessageBox.Show(x.ToString() + "Record Saved");
showData("select * from owner");
}
else
lblSaveError.Text = "Please Re-enter the correct data.";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
解决方案
在您的查询中,您有 2 列:
,NID_Photo, Owner_Photo
但您只插入带有一个参数的照片:
,@img,@img
请改用两个参数,例如, @img, @owner
.
然后,以与您相同的方式填写所有者照片@img
:
da.InsertCommand.Parameters.Add(new SqlParameter("@owner", owner));
推荐阅读
- excel-formula - 当日期已填充到另一个单元格中时,有没有办法在一个单元格中返回用户名?
- html - 使用滚动和标题将 div 扩展到视口底部
- excel - 如何压缩宏
- excel - 如何让我的宏在每周二下午 1:20 自动运行?
- javascript - Nodejs中的本地化日历?
- amazon-web-services - S3 生命周期策略删除没有特定标记值的所有对象
- wordpress - 我的网站加载缓慢,元素看起来很乱
- ios - 背景图像的位置不正确 - 快速
- android - 构建失败并出现 androidX 错误,但我没有在项目中使用 androidX
- java - 将数组从 Java 返回到 Python