【问题标题】:Insert operation failed: c#, sql server [closed]插入操作失败:c#,sql server [关闭]
【发布时间】:2014-03-07 15:24:24
【问题描述】:

我正在尝试插入我的表 Add_Product,但它不起作用。它没有进入数据库,也没有显示任何错误 这是我的连接类:

 public static class ConnectionClass
    {
        private static SqlConnection con;
        private static SqlCommand cmd;

        static ConnectionClass()
        {
            string conString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            con = new SqlConnection(conString);
        }

        public static void AddProduct(Product product)
        {
            cmd = new SqlCommand("insert into Add_Product (Party, Product_Name, Price, Quantity,Type,Details,Date) values (@Party, @Product_Name, @Price,Quantity,@Type,@Details, @Date,)", con);

            cmd.Parameters.AddWithValue("@Party", product.Party);
            cmd.Parameters.AddWithValue("@Product_Name", product.Product_Name);
            cmd.Parameters.AddWithValue("@Price", product.Price);
            cmd.Parameters.AddWithValue("@Quantity", product.Quantity);
            cmd.Parameters.AddWithValue("@Type", product.Type);
            cmd.Parameters.AddWithValue("@Details", product.Quantity);
            cmd.Parameters.AddWithValue("@Date", product.Date);

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();


            }

            finally
            {

                con.Close();
                cmd.Parameters.Clear();
            }


        }
    }
}

这是 Product 类构造函数:

 public Product(string Party, string Product_Name, double Price, int Quantity, string Type, string Details, string Date)
        {
            this.Party = Party;
            this.Product_Name = Product_Name;
            this.Price = Price;
            this.Quantity = Quantity;
            this.Type = Type;
            this.Details = Details;
            this.Date = Date;
        }

而后面的代码是:

 protected void btnAddProduct_Click(object sender, EventArgs e)
        {
            try
            {
                string Party = TbPartyName.Text;
                string Name = TbProdName.Text;
                double Price = Convert.ToDouble(TbPrice.Text);
                int Quantity = Convert.ToInt32(TbQuantity.Text);
                string Type = TbType.Text;
                string Details = TbDetail.Text;
                string Date = TbDate.Text;

                Product product = new Product(Party, Name, Price, Quantity, Type, Details, Date);
                ConnectionClass.AddProduct(product);
                lblAdd.Text = "Added succesfully";
            }

            catch (Exception)
            {
                lblAdd.Text = "Operation Failed";
            }

        }

请告诉我哪里出错了

【问题讨论】:

  • 捕获的异常的详细信息是什么?
  • "@Date, " 在日期之后你有额外的逗号
  • 这就是为什么你应该先调试你的代码..
  • 糟糕,语法错误太多。我的错。它现在工作正常。真的很抱歉我的愚蠢!谢谢大家

标签: c# database sql-server-2008 sql-insert


【解决方案1】:

我注意到您的插入语句的摘录:

@价格、数量、@类型

看到“数量”缺少@ 字符吗?


另外:请参阅@Usman Butt 的评论:查询末尾有一个额外的逗号。

【讨论】:

  • 我的朋友眼睛很好!
  • 糟糕,语法错误太多。我的错。它现在工作正常。真的很抱歉我的愚蠢!谢谢大家
【解决方案2】:

试试这个插入查询...@quantity and remove comma after @date

cmd = new SqlCommand("插入 Add_Product (Party, Product_Name, Price, Quantity,Type,Details,Date) 值 (@Party, @Product_Name, @Price,@Quantity,@Type,@Details, @Date) ", 反对);

【讨论】:

    【解决方案3】:

    您在查询中的@Date 参数后添加了额外的逗号。没有错误,因为您将命令的 ExecuteNonQuery 方法放在 try 子句中,并且从未被异常子句捕获。

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多