public partial class _Default : System.Web.UI.Page
{
    public string fname;
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (UploadFile.Value != null && UploadFile.Value != "")
        {
            InputFile();
        }
        string sql;
        sql = "insert into ziyuanku(ziyuanmingcheng,ziyuanbao) values('" + TB1.Text.ToString().Trim() + "','" + fname + "')";
        int result;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["getcon"].ConnectionString);
        
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        result = cmd.ExecuteNonQuery();
        conn.Close();
  
        if (result == 1)
        {
            Response.Write("<script>javascript:alert('上传成功');</script>");
        }
        else
        {
            Response.Write("<script>javascript:alert('系统错误');</script>");
        }
    }
    private void InputFile()
    {
        if (UploadFile.Value != null)
        {
            string nam = UploadFile.PostedFile.FileName;
            int i = nam.LastIndexOf(".");
            string newext = nam.Substring(i);
            DateTime now = DateTime.Now;
            string newname = now.DayOfYear.ToString() + UploadFile.PostedFile.ContentLength.ToString();
            string filepath = UploadFile.PostedFile.FileName;
            string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
            fname = filename;            FileInfo f = new FileInfo(filepath);
            string newpath = Server.MapPath("File") + "\\" + filename;
            f.CopyTo(newpath, true);        }
    }}

解决方案 »

  1.   

     if (UploadFile.Value != null && UploadFile.Value != "") //判断上传文件是否存在,这里可以UploadFiel.hasFile()来进行判断。
      {
      InputFile(); //进行文件上传
      }
      string sql;
     //插入数据库ziyuanku表中一条件记录的sql语句
      sql = "insert into ziyuanku(ziyuanmingcheng,ziyuanbao) values('" + TB1.Text.ToString().Trim() + "','" + fname + "')";
      int result;
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["getcon"].ConnectionString);
        
      conn.Open(); //打开连接
      SqlCommand cmd = new SqlCommand(sql, conn);
    //创建执行cmd
      result = cmd.ExecuteNonQuery(); 
    //执行sql语句
      conn.Close(); //关闭连接   
      if (result == 1)
      {
      Response.Write("<script>javascript:alert('上传成功');</script>"); //输出js弹出框
      }
      else
      {
      Response.Write("<script>javascript:alert('系统错误');</script>");
      }
      }