none
SqlDataSource 添加数据没反应 RRS feed

  • 问题

  • BtnOK_Click添加事件后,数据没能添加到数据库,断点也没有反应,添加数据的代码好像直接跳过去了
    if (fileOK=true)//如果条件满足
                        {   
                            SqlConnection conn = null;
                            SqlCommand cmd = null;
                            string strSql = ConfigurationManager.ConnectionStrings["eshop"].ConnectionString;
                            conn = new SqlConnection(strSql);
                            string sql = string.Format("select * from xgl_products where ProductName='" + txtProName.Text.Trim() + "'");
                            cmd = new SqlCommand(sql, conn);
                            conn.Open();
                            SqlDataReader sdr = cmd.ExecuteReader();                   
                           
                            if (sdr.Read() == false)//判断ProductName是否己存在,程序到这儿都能正常判断
                            {
                                
                                try
                                {
                                    //时间随机数,妨止图片名重复
                                    string RoundNum = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
                                    + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
                                    + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
                                    + DateTime.Now.Millisecond.ToString();
    
                                    string ProName = txtProName.Text.Trim();
                                    string ProInfo = FreeTextBox1.Text;
                                    int IsRecommend = int.Parse(RBL.SelectedValue);
                                    string ProImg = RoundNum + IMGup.FileName;                                
                                    int typeID = int.Parse(DDL.SelectedValue);
                                   
                                    SqlDataSource sd = new SqlDataSource();
                                    sd.ConnectionString = ConfigurationManager.ConnectionStrings["eshop"].ConnectionString;
                                    sd.InsertCommand = "insert into xgl_products(ProductName,ProductInfo,IsRecommend,ProductImg,typeID,) values('" + ProName + "','" + ProInfo + "','" + IsRecommend + "','" + ProImg + "','" + typeID + "')";
                                    sd.Insert();
                                    IMGup.PostedFile.SaveAs(path + ProImg);                               
                                    Response.Write("<script language='javascript'>alert('添加成功!')</script>");
                                    conn.Close();
                                    txtProName.Text = "";
                                    FreeTextBox1.Text = "";
                                    RBL.SelectedIndex = 0;
                                }
                                catch (System.Exception ex)//当要添加的数据都满足条件时,跳到这儿了
                                {
                                    Response.Write("<script language='javascript'>alert('添加失败!')</script>");//在try中添加断点后也没有反应,点击添加后,直接弹出"添加失败"了                }
                                finally
                                {
                                    conn.Close();
                                }
                                                
                            }
                            else
                            {
                                Response.Write("<script language='javascript'>alert('该产品名称己存在!')</script>");
                                conn.Close();
                            }
    
                        }
                        else
                        {
                            Response.Write("<script language='javascript'>alert('该文件类型不对或文件超过300KB!')</script>");
                        }
    
    2009年6月6日 18:26

答案

  • using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["eshop"].ConnectionString))
    {
        string RoundNum = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
        + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
        + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
        + DateTime.Now.Millisecond.ToString();
    
        string ProName = txtProName.Text.Trim();
        string ProInfo = FreeTextBox1.Text;
        int IsRecommend = int.Parse(RBL.SelectedValue);
        string ProImg = RoundNum + IMGup.FileName;
        string Img = "T" + RoundNum + IMGup.FileName;
        int typeID = int.Parse(DDL.SelectedValue);
    
         string fCommandText =
    @"if exists (select 1 from xgl_products where ProductName = @ProductName) begin
    select 1
    end else begin
    insert into xgl_products(ProductName,ProductInfo,IsRecommend,ProductImg,Img,typeID) 
    values(@ProductName, @ProductInfo,@IsRecommend, @ProductImg, @Img, @typeID)
    select 0
    end";
        SqlCommand cmd = new SqlCommand(fCommandText);
    
        cmd.Parameters.AddRange(new SqlParameter[] { 
            new SqlParameter("@ProductName", ProName),
            new SqlParameter("@ProductInfo", ProInfo),
            new SqlParameter("@IsRecommend", IsRecommend),
            new SqlParameter("@ProductImg", ProImg),
            new SqlParameter("@Img", Img),
            new SqlParameter("@typeID", typeID)
        });
    
        try
        {
            conn.Open();
            int fResult = (int)cmd.ExecuteScalar();
            Response.Write(string.Format("<script language='javascript'>alert('{0}!')</script>",
                (fResult == 0 ? "该产品名称己存在" : "添加成功")));
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('添加失败!')</script>");
        }
    }
    1. SQL提交最好用参数,不要拼字符串
    2. SQL命令本身有语法错
    3. 你的需要一次与数据交互就能得到结果了,上面的技巧可以减少与服务器交互的次数

    知识改变命运,奋斗成就人生!
    • 已标记为答案 斌斌NO1 2009年6月7日 6:26
    2009年6月7日 4:24
    版主

全部回复

  • 因为你的catch捕获到了try中的异常,
    你可以在catch里将异常输出,看看是什么原因
    catch (System.Exception ex)//当要添加的数据都满足条件时,跳到这儿了
                                {
                                    Response.Write(ex.Message );
    }
    SQL SERVER Engine Test
    2009年6月7日 4:11
    版主
  • using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["eshop"].ConnectionString))
    {
        string RoundNum = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
        + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
        + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
        + DateTime.Now.Millisecond.ToString();
    
        string ProName = txtProName.Text.Trim();
        string ProInfo = FreeTextBox1.Text;
        int IsRecommend = int.Parse(RBL.SelectedValue);
        string ProImg = RoundNum + IMGup.FileName;
        string Img = "T" + RoundNum + IMGup.FileName;
        int typeID = int.Parse(DDL.SelectedValue);
    
         string fCommandText =
    @"if exists (select 1 from xgl_products where ProductName = @ProductName) begin
    select 1
    end else begin
    insert into xgl_products(ProductName,ProductInfo,IsRecommend,ProductImg,Img,typeID) 
    values(@ProductName, @ProductInfo,@IsRecommend, @ProductImg, @Img, @typeID)
    select 0
    end";
        SqlCommand cmd = new SqlCommand(fCommandText);
    
        cmd.Parameters.AddRange(new SqlParameter[] { 
            new SqlParameter("@ProductName", ProName),
            new SqlParameter("@ProductInfo", ProInfo),
            new SqlParameter("@IsRecommend", IsRecommend),
            new SqlParameter("@ProductImg", ProImg),
            new SqlParameter("@Img", Img),
            new SqlParameter("@typeID", typeID)
        });
    
        try
        {
            conn.Open();
            int fResult = (int)cmd.ExecuteScalar();
            Response.Write(string.Format("<script language='javascript'>alert('{0}!')</script>",
                (fResult == 0 ? "该产品名称己存在" : "添加成功")));
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('添加失败!')</script>");
        }
    }
    1. SQL提交最好用参数,不要拼字符串
    2. SQL命令本身有语法错
    3. 你的需要一次与数据交互就能得到结果了,上面的技巧可以减少与服务器交互的次数

    知识改变命运,奋斗成就人生!
    • 已标记为答案 斌斌NO1 2009年6月7日 6:26
    2009年6月7日 4:24
    版主
  • 你好,你这里多了个,号
    
    "insert into xgl_products(ProductName,ProductInfo,IsRecommend,ProductImg,typeID,) 
    
    "insert into xgl_products(ProductName,ProductInfo,IsRecommend,ProductImg,typeID) 
    
    
    

    努力着就不会放弃!.net技术交流群 33083877http://hi.baidu.com/1987raymond
    2009年6月7日 4:26
    版主