1

我正在尝试将文件名和文件路径保存在数据库中。但无法保存它:(。数据库连接工作正常,但我不知道是什么问题

 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!";
        }

    }
        }
4

1 回答 1

2

您尚未为该命令提供 SqlConnection。你需要:

SqlCommand cmd = new  SqlCommand("insertintoTbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)", con);

您实际上也不会在任何地方执行命令。这也需要明确地完成:

cmd.ExecuteNonQuery();
于 2013-03-14T19:49:06.817 回答