C# 文件上载值未保存在数据库中 SqlConnection con=newsqlconnection(); SqlCommand cmd=新的SqlCommand(“插入Tbl_视频(VideoName,VideoPath)值(@VideoName,@VideoPath)”; 字符串fileExt=System.IO.Path.GetExtension(FileUpload1.FileName); 如果(fileExt==“.avi”) { 尝试 { FileUpload1.SaveAs(Server.MapPath(“~/video/”+FileUpload1.FileName)); Label1.Text=“文件名:”+ FileUpload1.PostedFile.FileName+“”+ FileUpload1.PostedFile.ContentLength+“kb”+“内容类型:”+FileUpload1.PostedFile.ContentType; cmd.Parameters.AddWithValue(“@VideoName”,FileUpload1.FileName); cmd.Parameters.AddWithValue(“@VideoPath”,FileUpload1.FileName); cmd.ExecuteNonQuery(); } 捕获(例外情况除外) { Label1.Text=“错误:”+ex.Message.ToString(); } } 其他的 { Label1.Text=“仅允许.avi文件!”; } }
您在哪里提到您的SQL server连接字符串C# 文件上载值未保存在数据库中 SqlConnection con=newsqlconnection(); SqlCommand cmd=新的SqlCommand(“插入Tbl_视频(VideoName,VideoPath)值(@VideoName,@VideoPath)”; 字符串fileExt=System.IO.Path.GetExtension(FileUpload1.FileName); 如果(fileExt==“.avi”) { 尝试 { FileUpload1.SaveAs(Server.MapPath(“~/video/”+FileUpload1.FileName)); Label1.Text=“文件名:”+ FileUpload1.PostedFile.FileName+“”+ FileUpload1.PostedFile.ContentLength+“kb”+“内容类型:”+FileUpload1.PostedFile.ContentType; cmd.Parameters.AddWithValue(“@VideoName”,FileUpload1.FileName); cmd.Parameters.AddWithValue(“@VideoPath”,FileUpload1.FileName); cmd.ExecuteNonQuery(); } 捕获(例外情况除外) { Label1.Text=“错误:”+ex.Message.ToString(); } } 其他的 { Label1.Text=“仅允许.avi文件!”; } },c#,asp.net,sql-server-2008,C#,Asp.net,Sql Server 2008,您在哪里提到您的SQL server连接字符串 SqlConnection con = new SqlConnection(); SqlCommand cmd = new SqlCommand("insert into Tbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)"); string fileExt = System.IO.Path.GetExtension(FileUpload1.FileNa
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand("insert into Tbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)");
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt == ".avi")
{
try
{
FileUpload1.SaveAs(Server.MapPath("~/video/" +FileUpload1.FileName));
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +"Content type: "+FileUpload1.PostedFile.ContentType;
cmd.Parameters.AddWithValue("@VideoName",FileUpload1.FileName);
cmd.Parameters.AddWithValue("@VideoPath", FileUpload1.FileName);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "Only .avi files allowed!";
}
}
然后执行您的查询。您能获得
文件上传1.FileName
值吗?&你犯了什么错误?您需要像con.open()一样打开SqlConnection代码>:在声明SqlCommand
后,需要添加以下给定的CommandType提及的代码也cmd.CommandType=CommandType.Text代码>其中是实际将文件发送(发布)到服务器的事件。您确实应该检查以确保该文件实际上是一个.avi文件,这是一项简单的任务,可以上载一个.exe文件,并在.con.Parameters.AddWithValue(“@path”,“video/”+FileUpload1.FileName”)末尾附加一个.avi扩展名;thnx伙计们需要帮助…我已经找到了解决方案..我没有写文件夹名
SqlConnection con = new SqlConnection(YOUR_CONNECTION_STRING_HERE);
con.Open();