插入操作失败:c#,sql server

时间:2014-03-07 15:24:24

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

我正在尝试插入我的表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";
            }

        }

请告诉我哪里出错了

3 个答案:

答案 0 :(得分:5)

我注意到你的插入声明摘录:

  

@价格,数量,@类型

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


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

答案 1 :(得分:0)

尝试此插入查询... @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) “,con);

答案 2 :(得分:0)

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