我尝试在数据库中保存文件的file_name和路径。但无法保存:(。数据库连接工作正常,但我不知道是什么问题
con.Open();
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
SqlCommand cmd = new SqlCommand("insertintoTbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)",con);
if (fileExt == ".avi")
{
try
{
cmd.Parameters.AddWithValue("@VideoName", "video/"+FileUpload1.FileName);
cmd.Parameters.AddWithValue("@VideoPath", "video/" +FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/video/" + FileUpload1.FileName));
Literal1.Text = "upload";
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "Only .avi files allowed!";
}
}
}
答案 0 :(得分:2)
您尚未使用SqlConnection提供该命令。你需要:
SqlCommand cmd = new SqlCommand("insertintoTbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)", con);
您也无法在任何地方执行该命令。这也需要明确地完成:
cmd.ExecuteNonQuery();