当前位置:编程学习 > C#/ASP.NET >>

如下代码,为什么显示插入数据成功,数据库中却没东西?

string filePath = null;
    BaseClass bc = new BaseClass();
    protected void DataInsert()
    {
        string sqlConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlConnection sqlCon = new SqlConnection(sqlConString);
        string strCmd = "insert into criterion (title,number,time,department,details) values('" + TxtName.Text.Trim() + "','" + Txtnumber.Text.Trim() + "','" + txtIntoTime.Text.Trim() + "','" + Txtpart.Text.Trim() + "','" + filePath + "')";
        SqlCommand sqlCmd = new SqlCommand(strCmd, sqlCon);
        try
        {
            if (sqlCon.State == ConnectionState.Closed)
            {
                sqlCon.Open();
            }
            int i = sqlCmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            Response.Write("<script>alert('" + ex.Message + "')</script>");
        }
        finally
        {
            if (sqlCon.State == ConnectionState.Open)
            {
                sqlCon.Close();
            }
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        if (TxtName.Text == string.Empty)
        {
            Response.Write("<script>alert('请输入标准名称!')</script>");
            return;
        }
        if (Txtpart.Text == string.Empty)
        {
            Response.Write("<script>alert('请输入颁布部门!')</script>");
            return;
        }
        if (txtIntoTime.Text == string.Empty)
        {
            Response.Write("<script>alert('请输入颁布时间!')</script>");
            return;
        }
        if (Txtnumber.Text == string.Empty)
        {
            Response.Write("<script>alert('请输入国标号!')</script>");
            return;
        }
        if (!FileUpload1.HasFile)
        {
            Response.Write("<script>alert('请输入标准内容!')</script>");
            return;
        }
        bool isadd = FileUpload();
        DataInsert();
        if (isadd)
        {
            Response.Write("<script>alert('插入标准成功')</script>");
            TxtName.Text = Txtpart.Text = txtIntoTime.Text = Txtnumber.Text = string.Empty;

        }
    }
    protected void BtnBack_Click(object sender, ImageClickEventArgs e)
    {
        TxtName.Text = Txtpart.Text = txtIntoTime.Text=Txtnumber.Text = string.Empty;

    }
    public bool FileUpload()
    {
        bool isadd = false;
        if (FileUpload1.HasFile) //获取一个值 判断是否包含文件
        {
            string extension = System.IO.Path.GetExtension(FileUpload1.FileName);  //获得扩展名  
            string paths = "../../upload/paper2";
            if (!System.IO.Directory.Exists(Server.MapPath(paths)))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(paths));
            }
            filePath = paths + "/" + DateTime.Now.ToFileTime().ToString() + extension;
            
                if (FileUpload1.PostedFile.ContentLength < 10000000)  //文件小于10M
                {
                    FileUpload1.SaveAs(Server.MapPath(filePath));  //设置文件上传路径
                    isadd = true;

                }
                else
                {
                    Response.Write("<script>alert('文件不能超过10M,请从新选择!')</script>");
                    isadd = false;
                }
        }
        else
        {
            Response.Write("<script>alert('请选择标准内容文件!')</script>");
            isadd = false;
        }
        return isadd;
    }
} --------------------编程问答-------------------- 是不是提示“插入标准成功”?你那个提示是用isadd判断的!但是这个来源只是检查有没输入正确,这个和数据库插入成功没什么关心吧!
--------------------编程问答-------------------- 1、运行程序,加断点在stirng strCmd 行

2、拷贝strCmd 变量的sql文本串,放在sql查询分析器中执行一下,
   看看报错吗? --------------------编程问答-------------------- 断点调试一下呢! --------------------编程问答-------------------- 先判断数据库选择对了没。
在断点调试下,看下执行的SQL语句 是否正确。 --------------------编程问答-------------------- 判断下i的值 如果大于0插入成功  某些情况下不会报异常插入失败会返回-1 --------------------编程问答-------------------- bool isadd = FileUpload();你这个判断的是上传文件是否正确。

你应该判断的是它int i = sqlCmd.ExecuteNonQuery(); --------------------编程问答-------------------- 同意1楼说法
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,