首页 > 解决方案 > 将不同图片框中的多个图像保存到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);
        }

标签: c#

解决方案


在您的查询中,您有 2 列:

,NID_Photo, Owner_Photo

但您只插入带有一个参数的照片:

,@img,@img

请改用两个参数,例如, @img, @owner.

然后,以与您相同的方式填写所有者照片@img

da.InsertCommand.Parameters.Add(new SqlParameter("@owner", owner));

推荐阅读