求助大神,我的毕设题目,用的visual studio开发的网上作业提交系统,数据库选用的access数据库,下面是作业上传部分的代码,作业图片采用pdf格式,上传到access中的附件列(字段名称为作业,字段属性为附件),但在代码运行的时候updateCmd.ExecuteNonQuery()老是会报错:至少一个参数没有被指定值,求大神解答
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 网上作业提交系统
{
public partial class 作业上传 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FileUpload1.Attributes["onchange"] = "UploadFile(this)";
}
Label2.Text = ""; // 初始时将 Label2 置为空
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = Session["username"].ToString();
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\vs2022 asp项目\网上作业提交系统\App_Data\Database1.mdb";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
string fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower(); // 获取文件扩展名并转换为小写字母
if (fileExtension == ".pdf") // 判断文件扩展名是否为 .pdf 格式
{
string studentID = "";
string queryString = "SELECT 学号 FROM 学生 WHERE 学号=?";
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.AddWithValue("@p1", username);
OleDbDataReader reader = command.ExecuteReader();
if (reader.Read())
{
studentID = reader.GetString(0);
}
reader.Close();
string homework = Server.MapPath("~/App_Data/") + studentID + ".pdf"; // 修改文件名为学号.pdf
FileUpload1.SaveAs(homework); // 保存文件到指定路径
byte[] pdfData = File.ReadAllBytes(homework); // 将 PDF 文件读取为字节数组
string updateQuery = "UPDATE 学生 SET 作业.value=? WHERE 学号=?";
OleDbCommand updateCmd = new OleDbCommand(updateQuery, connection);
OleDbParameter pdf = new OleDbParameter("@p1", OleDbType.Binary); // 指定第一个参数的数据类型为二进制数据
pdf.Value = (pdfData == null) ? (object)DBNull.Value : pdfData; // 将 PDF 文件读取为字节数组
updateCmd.Parameters.Add(pdf);
OleDbParameter studentIDParam = new OleDbParameter("@p2", OleDbType.VarChar, 50);
studentIDParam.Value = studentID;
updateCmd.Parameters.Add(studentIDParam); // 指定第三个参数的值
updateCmd.ExecuteNonQuery(); // 执行更新操作
Label2.Text = "上传成功,五秒后返回首页.";
System.Threading.Thread.Sleep(5000); // 暂停5秒
Response.Redirect("首页.aspx");
}
}
else
{
Label2.Text = "仅支持上传 .pdf 格式的文件!";
}
connection.Close();
}
}
}