为特定用户添加图像和路径到数据库
Add the image and path to the database for the specific users
protected void Upload(object sender, EventArgs e)
{
//Extract Image File Name.
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Set the Image File Path.
string filePath = "~/Uploads/" + fileName;
//Save the Image File in Folder.
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
string mycon = "server =localhost; Uid=root; password = ; persistsecurityinfo = True; database =ovs; SslMode = none";
MySqlConnection con1 = new MySqlConnection(mycon);
// string sql = "INSERT INTO candidate VALUES(@Name, @Path)";
MySqlCommand cmd = null;
cmd = new MySqlCommand("INSERT INTO candidate(candidateImage,path where studentID ='" + Session["UserName"] + "') VALUES (@Name,@Path)", con1);
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
con1.Open();
cmd.ExecuteNonQuery();
con1.Close();
}
当用户点击上传按钮时,图片和路径将保存到属于特定用户的数据库中。例如ID为R1001的用户登录自己的账号,点击上传按钮,图片和路径会保存在数据库中的R1001下。我显示的错误消息表明存在语法错误,但我找不到它
database
当您想将数据插入 table 时,您不需要 Where
。
我在你的 command
中删除了 where
并为 sessionId 使用了一个参数。
protected void Upload(object sender, EventArgs e)
{
//Extract Image File Name.
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Set the Image File Path.
string filePath = "~/Uploads/" + fileName;
//Save the Image File in Folder.
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
string mycon = "server =localhost; Uid=root; password = ; persistsecurityinfo = True; database =ovs; SslMode = none";
MySqlConnection con1 = new MySqlConnection(mycon);
// string sql = "INSERT INTO candidate VALUES(@Name, @Path)";
SqlCommand cmd = null;
string command = "update candidate set candidateImage=@Name ,path = @Path where StudentID=@studnetId";
cmd = new MySqlCommand(command, con1);
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
cmd.Parameters.AddWithValue("@studnetId", Session["UserName"]??""); // if student id is accept number in database, you must convert it to int
con1.Open();
cmd.ExecuteNonQuery();
con1.Close();
}
protected void Upload(object sender, EventArgs e)
{
//Extract Image File Name.
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Set the Image File Path.
string filePath = "~/Uploads/" + fileName;
//Save the Image File in Folder.
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
string mycon = "server =localhost; Uid=root; password = ; persistsecurityinfo = True; database =ovs; SslMode = none";
MySqlConnection con1 = new MySqlConnection(mycon);
// string sql = "INSERT INTO candidate VALUES(@Name, @Path)";
MySqlCommand cmd = null;
cmd = new MySqlCommand("INSERT INTO candidate(candidateImage,path where studentID ='" + Session["UserName"] + "') VALUES (@Name,@Path)", con1);
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
con1.Open();
cmd.ExecuteNonQuery();
con1.Close();
}
当用户点击上传按钮时,图片和路径将保存到属于特定用户的数据库中。例如ID为R1001的用户登录自己的账号,点击上传按钮,图片和路径会保存在数据库中的R1001下。我显示的错误消息表明存在语法错误,但我找不到它
database
当您想将数据插入 table 时,您不需要 Where
。
我在你的 command
中删除了 where
并为 sessionId 使用了一个参数。
protected void Upload(object sender, EventArgs e)
{
//Extract Image File Name.
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Set the Image File Path.
string filePath = "~/Uploads/" + fileName;
//Save the Image File in Folder.
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
string mycon = "server =localhost; Uid=root; password = ; persistsecurityinfo = True; database =ovs; SslMode = none";
MySqlConnection con1 = new MySqlConnection(mycon);
// string sql = "INSERT INTO candidate VALUES(@Name, @Path)";
SqlCommand cmd = null;
string command = "update candidate set candidateImage=@Name ,path = @Path where StudentID=@studnetId";
cmd = new MySqlCommand(command, con1);
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
cmd.Parameters.AddWithValue("@studnetId", Session["UserName"]??""); // if student id is accept number in database, you must convert it to int
con1.Open();
cmd.ExecuteNonQuery();
con1.Close();
}