1

我下载了使用 SQL Server 用 C# 构建的简单学校管理软件。我将其表导出到 MS Access 数据库中。SqlCommand我还对 OleDb的SqlDataAdapter连接等代码进行了更改。但在一种形式中,我无法将我的图像存储到访问中。

这是代码:

private void btn_Insert_Click(object sender, EventArgs e)
{
    if (txt_StudentFirstName.Text != "" && 
        txt_StudentMobileNo.Text != ""  && 
        cmb_Teacher.Text != "" && 
        img_Box.Image != null)
    {
        byte[] img = null;
        FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        img = br.ReadBytes((int)fs.Length);

        cmd = new OleDbCommand("INSERT INTO Student (FirstName, LastName, FatherName, FatherID, DOB, 
                                                     DOR, Class, Course, Gender, Teacher, Address, 
                                                     EmailID, MobileNo, TotalFee, FeePaid, Balance, Image) 
                                VALUES (@FirstName, @LastName, @FatherName, @FatherID, @DOB,
                                        @DOR, @Class, @Course, @Gender, @Teacher, @Address,
                                        @EmailID, @MobileNo, @TotalFee, @FeePaid, @Balance, @Img)", con);

        con.Open();

        cmd.Parameters.AddWithValue("@FirstName", txt_StudentFirstName.Text);
        cmd.Parameters.AddWithValue("@LastName", txt_StudentLastName.Text);
        cmd.Parameters.AddWithValue("@FatherName", txt_StudentFatherName.Text);
        cmd.Parameters.AddWithValue("@FatherID", txt_StudentFatherID.Text);
        cmd.Parameters.AddWithValue("@DOB", Student_DOB.Text);
        cmd.Parameters.AddWithValue("@DOR", Student_DOR.Text);
        cmd.Parameters.AddWithValue("@Class", cmb_Class.Text);
        cmd.Parameters.AddWithValue("@Course", cmb_Course.Text);
        cmd.Parameters.AddWithValue("@Gender", cmb_Gender.Text);
        cmd.Parameters.AddWithValue("@Teacher", cmb_Teacher.Text);
        cmd.Parameters.AddWithValue("@Address", txt_StudentAddress.Text);
        cmd.Parameters.AddWithValue("@EmailID", txt_StudentEmailID.Text);
        cmd.Parameters.AddWithValue("@MobileNo", txt_StudentMobileNo.Text);
        cmd.Parameters.AddWithValue("@TotalFee", cmb_CourseFee.Text);
        cmd.Parameters.AddWithValue("@FeePaid", txt_FeePaid.Text);
        cmd.Parameters.AddWithValue("@Balance", txt_Balance.Text);
        // byte[] yourPhoto = ();
        cmd.Parameters.AddWithValue("@Img", img);

        cmd.ExecuteNonQuery();
        con.Close();

        MessageBox.Show("Record inserted successfully");

        DisplayData();
        ClearData();
    }
    else
    {
        MessageBox.Show("Please provide complete details with image !");
    }
}
4

0 回答 0