/// /// 上传文件到服务器 /// protected void ButtonAdd_Click(object sender, EventArgs e) { string Userid = Session["num"].ToString(); string Homeworkname; string Type; int Size; string Date; string Url; if (FULHomeWork.PostedFile.FileName != "") { string[] filename = FULHomeWork.PostedFile.FileName.Split('.'); Homeworkname = filename[0].Substring(filename[0].LastIndexOf("\\")+1); Type = filename[1]; Size = FULHomeWork.PostedFile.ContentLength; Date = System.DateTime.Now.ToShortDateString(); string uppath = @"UpLoadFiles\" + Userid; Url = uppath.Replace(@"\", "/") + "/" + Homeworkname + '.' + Type; try { if (!System.IO.Directory.Exists(Server.MapPath(uppath))) { System.IO.Directory.CreateDirectory(Server.MapPath(uppath)); } string strUrl = Server.MapPath(uppath + @"\" + this.FULHomeWork.FileName); FULHomeWork.SaveAs(strUrl); Mssql sql = new Mssql(); string str = "insert into HomeWork (UserID,CourseName,CourseCount,HomeWorkName,Type,Size,Date,Remark,Url) values('" + Userid + "','" + DDLCourse.SelectedValue + "','" + DDLCount.SelectedValue + "','" + Homeworkname + "','" + Type + "','" + Size + "','" + Date + "','"+TBRemark.Text+"','" + Url + "')"; sql.ExecuteNoReturn(str); Response.Write(""); } catch { LabelMessage.Text = "文件上传失败!"; } } else { LabelMessage.Text = "请选择要上传的文件!"; } } public class Mssql { public SqlConnection myconn; /// /// 构造函数,并打开数据库 /// public Mssql() { myconn = new SqlConnection(connectionString()); myconn.Open(); } /// /// 返回数据库连接字符串 /// private string connectionString() { return ConfigurationManager.ConnectionStrings["ERCPConnectionString"].ConnectionString; } /// /// 根据传入sql查询语句,返回SqlDataReader类型对象。 /// public SqlDataReader GetResultAsDataReader(string strSql) { SqlCommand command = new SqlCommand(strSql, myconn); return command.ExecuteReader(); //SqlDataAdapter cm = command.ExecuteReader(); //return cm; } /// /// 根据传入sql插入、更新、删除语句完成数据库的操作。 /// public void ExecuteNoReturn(string strSql) { SqlCommand myCommand = new SqlCommand(strSql, myconn); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } myCommand.Dispose(); myconn.Close(); } /// /// 关闭数据库连接。 /// public void killMe() { myconn.Close(); }} 这上在上传的文件中插入数据库代码----------------------------------------------------------------------------------- 也可以先获得播放器地址后,再插进去啊