if (this.FileUpload1.HasFile)
        {
            string filename = this.FileUpload1.FileName;
            string type = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
            if (type.Equals("jpg") && type.Equals("gif") && type.Equals("bmp"))
            {
                string dt = DateTime.Now.ToString("yyMMddhhmmss");
                string savename = dt + "." + type;
                string path = "~/BookImages/" + savename;
                TextBox1.Text = path;
                this.FileUpload1.SaveAs(Server.MapPath("Images") + "\\" + savename);            }
            try
            {
                string selstr = "update Book set PictureUrl='" + TextBox1.Text + "'";
                Database db1 = new Database();
                db.ExecuteSQL(selstr);
                this.FileUpload1.SaveAs(Server.MapPath("image") + "\\" + savename);
                Response.Write("<Script Language=JavaScript>alert(\"成功!\")</Script>");
            }
            catch
            {
                Response.Write("<Script Language=JavaScript>alert(\"失败!\")</Script>");            }
        }
        else
        {
            Response.Write("<Script Language=JavaScript>alert(\"文件类型不正确!\")</Script>");
        }
        string instring = "insert into [Book](BookName,CategoryID,Price,DisPrice,Publiser,PublishDate,Author,PageNum,PictureUrl,Description) values(" + "'" + txtbookName.Text + "','" + DropDownLisCategory.SelectedItem.Value + "','" + txtPrice + "','" + txtDisPrice + "','" + txtPublisher + "','" + DateTime.Now.ToString() + "','" + txtAuthor + "','" + txtPageNum + "','" + FileUpload1.FileName.ToString() + "','" + txtPro + "')";
        Database db = new Database();        db.ExecuteSQL(instring);

解决方案 »

  1.   

    我觉得应该是数据库的主外键的原因,比如在【Book】表中你插入 的CategoryID在Category表中有没有这条记录等等的。就是说Book表的外键(与其他表关联)字段,在其他表中有没有已经存在的值让你选择,如果没有的话,那就违反了主外键约束了,当然插入不了了。自己检查一下。
    还有就是你在
     string instring = "insert into [Book](BookName,CategoryID,Price,DisPrice..这里设置一个断点调试一下看看instring的值是什么,在查询分析器中看看有没有语法错误,能不能执行等。
      

  2.   

    try
    {
    if (this.FileUpload1.HasFile) 
            { 
                string filename = this.FileUpload1.FileName; 
                string type = filename.Substring(filename.LastIndexOf(".") + 1).ToLower(); 
                if (type.Equals("jpg") && type.Equals("gif") && type.Equals("bmp")) 
                { 
                    string dt = DateTime.Now.ToString("yyMMddhhmmss"); 
                    string savename = dt + "." + type; 
                    string path = "~/BookImages/" + savename; 
                    TextBox1.Text = path; 
                    this.FileUpload1.SaveAs(Server.MapPath("Images") + "\\" + savename);             } 
                try 
                { 
                    string selstr = "update Book set PictureUrl='" + TextBox1.Text + "'"; 
                    Database db1 = new Database(); 
                    db.ExecuteSQL(selstr); 
                    this.FileUpload1.SaveAs(Server.MapPath("image") + "\\" + savename); 
                    Response.Write(" <Script Language=JavaScript>alert(\"成功!\") </Script>"); 
                } 
                catch 
                { 
                    Response.Write(" <Script Language=JavaScript>alert(\"失败!\") </Script>");             } 
            } 
            else 
            { 
                Response.Write(" <Script Language=JavaScript>alert(\"文件类型不正确!\") </Script>"); 
            } 
            string instring = "insert into [Book](BookName,CategoryID,Price,DisPrice,Publiser,PublishDate,Author,PageNum,PictureUrl,Description) values(" + "'" + txtbookName.Text + "','" + DropDownLisCategory.SelectedItem.Value + "','" + txtPrice + "','" + txtDisPrice + "','" + txtPublisher + "','" + DateTime.Now.ToString() + "','" + txtAuthor + "','" + txtPageNum + "','" + FileUpload1.FileName.ToString() + "','" + txtPro + "')"; 
            Database db = new Database();         db.ExecuteSQL(instring);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.message);
    }
      

  3.   

    我的【Book】表中BookId 是主键,CategoryID是外键,category 表中有记录的,BookId是自动增加的,不需要添加吧.我在想不会是因为主键的原因【Book】表吧...