我用.net做一个企业站,添加好用,就是在更新的时候为什么我点更新,数据更新了,但是他又换重新在SQL里生成一行更新的数据!!!杂回事!!!

解决方案 »

  1.   

    sqldata da = new sqldata();
        int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
                DataTable dt = new DataTable();
                string a = Request.QueryString["id"];
                a = a == null ? "0" : a;
                int id = Convert.ToInt32(a);
                Image1.ImageUrl = videoURL.Text;
                
                this.addtime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                if(id>0){
                   
                    dt = GVDataBind(id).DataSet.Tables[0];
                    title.Text=dt.Rows[0][1].ToString();
                    contont.Text = dt.Rows[0][2].ToString();
                    videoURL.Text = dt.Rows[0][3].ToString();
                    range.Text = dt.Rows[0][4].ToString();
                    Button1.Text = "修改";
                }
            if(!IsPostBack){            Image1.ImageUrl = videoURL.Text;
                DataSet ds = da.ExceDS("select * from Products");
                type.DataSource = ds.Tables[0].DefaultView;
                type.DataTextField = "title";
                type.DataValueField = "id";
                type.DataBind();        }
                   }
        protected DataTable GVDataBind(int id)
        {        string sql = "select  * from Details where id=" + id;        return da.ExceDS(sql).Tables[0];
        }
        protected void Button1_Click(object sender, EventArgs e)
        {  
            if(id==0){
            
            if(Image1.ImageUrl.Length==0){
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请上传图片!')</script>");
            return;
              }        string title=Request["title"].ToString();
            string addtime = Request["addtime"].ToString();
            string contont = Request["contont"].ToString();
            string img = Request["videoURL"].ToString();
            string range = Request["range"].ToString();
            int type =Convert.ToInt32(this.type.SelectedValue);        //应用范围与产品分类
            string sql = "insert into Details(title,contont,img,addtime,type,range) values ('" + title + "','" + contont + "','" + img + "','" + addtime + "'," + type + ",'" + range + "') ";
            da.ExceSQL(sql);
            string sql1 = " select top 1 [id] from [details] order by [id] desc";
            Object abc = da.ExceSQLIden(sql1);
            string sqltype = "insert into app_pro(app,pro,protype)values(" + range + "," + abc.ToString() + "," +type.ToString() + ")";
            da.ExceSQL(sqltype);
            da.connectClose();
            Response.Redirect("productsCX.aspx",true);
           
            }
            if (id > 0) {            string title = Request["title"].ToString();
                string addtime = Request["addtime"].ToString();
                string contont = Request["contont"].ToString();
                string img = Request["videoURL"].ToString();
                string range = Request["range"].ToString();
                int type = Convert.ToInt32(this.type.SelectedValue);
                string sql = "update Details set title='" + title + "',contont='"+contont+"',img='"+img+"',addtime='"+addtime+"',type="+type+",range='"+range+"'";
                da.ExceSQL(sql);
                string sql2 = " select top 1 [id] from [details] order by [id] desc";
                Object abc = da.ExceSQLIden(sql2);
                string sqltype1 = "update Details set app="+range+",pro="+abc.ToString()+",protype="+type.ToString()+"";
                da.ExceSQL(sqltype1);
                da.connectClose();
                Response.Redirect("productsCX.aspx", true);
            }
              
        }
           
        private DataTable GetFl(int id) {        string fl = "select * from Products where id="+id;
            return da.ExceDS(fl).Tables[0];
        }
        protected void type_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(this.type.SelectedValue);
            type.DataSource = GetFl(id);
            type.DataBind();    }
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            this.SaveImages();
            Image1.ImageUrl = videoURL.Text;
        }    private Boolean SaveImages()
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            string filePath = "";        string fileName = "";
            string fileExtension = "";/// 
            string fileUrl = Request.ApplicationPath;
            string filea = HttpContext.Current.Request.Url.Host.ToString();
            string fileport = ":" + Convert.ToInt32(Request.ServerVariables["server_port"]);
            System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
            try
            {
                for (int iFile = 0; iFile < files.Count; iFile++)
                {                HttpPostedFile postedFile = files[iFile];
                    filePath = System.Web.HttpContext.Current.Request.MapPath("~/video/");
                    fileName = System.IO.Path.GetFileName(postedFile.FileName);                if (fileName != "")
                    {
                        fileExtension = System.IO.Path.GetExtension(fileName);                    postedFile.SaveAs(filePath + fileName);
                    }
                }            videoURL.Text = "http://" + filea + fileport + fileUrl + "/video/" + fileName;            return true;
            }
            catch (System.Exception Ex)
            {
                return false;
            }    }
      

  2.   

    现在就是id=0的时候是添加,id>0的时候是更新,但是更新完了在SQL表里这更新的数据重新生成一列,不是在原来的数据上更新
      

  3.   


    sqldata da = new sqldata();
      int id = 0; //此处申明了全局变量
      protected void Page_Load(object sender, EventArgs e)
      {
        
        
      DataTable dt = new DataTable();
      string a = Request.QueryString["id"];
      a = a == null ? "0" : a;
      // int id = Convert.ToInt32(a); //此处又申明了一重复id
    id = Convert.ToInt32(a);  //应该这样给全局变量付值 或者用viewstate存储一下id 后边button_click事件调用此viewstate读到id
      Image1.ImageUrl = videoURL.Text;
        
      this.addtime.Text = DateTime.Now.ToString("yyyy-MM-dd");
      if(id>0){
        
      dt = GVDataBind(id).DataSet.Tables[0];
      title.Text=dt.Rows[0][1].ToString();
      contont.Text = dt.Rows[0][2].ToString();
      videoURL.Text = dt.Rows[0][3].ToString();
      range.Text = dt.Rows[0][4].ToString();
      Button1.Text = "修改";
      }
      

  4.   

    关键是你得好好的检查 调试跟踪一下你的更新那行的索引,获得的那个ID是不是你数据库中那行记录的IDid=0的时候是添加, 你的意思是数据库中没有这条记录  那么就增加一条心的记录
    id>0的时候是更新,有记录的话就更新 你所选择的那条记录,但前提是你必须根据你所选择的索引,得到你的操作行的ID(数据表中的ID).
      

  5.   

    现在我想更新第21行的数据
    name  1111 把1111更新成22222点更新他就出来22行name 2222是这样
      

  6.   

    在button1的点击事件里设断点看看是不是id的问题
      

  7.   

    把 UPDATE  当做了 insert 在做了。。