插入操作失败:c#,sql server
我试图在我的表中插入Add_产品,但它不工作。它没有进入数据库,也没有显示任何错误 这是我的连接类:插入操作失败:c#,sql server,c#,database,sql-server-2008,sql-insert,C#,Database,Sql Server 2008,Sql Insert,我试图在我的表中插入Add_产品,但它不工作。它没有进入数据库,也没有显示任何错误 这是我的连接类: public static class ConnectionClass { private static SqlConnection con; private static SqlCommand cmd; static ConnectionClass() { string conString =
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();
}
}
}
}
这是产品类构造函数:
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";
}
}
请告诉我哪里出了问题我注意到了您插入声明中的这段摘录: @价格、数量、类型 是否看到“数量”缺少
@
字符
另外:请参阅@Usman Butt的评论:查询末尾有一个额外的逗号。尝试此插入查询…
@quantity并删除@date之后的逗号。
cmd=new SqlCommand(“插入Add_Product(Party、Product_Name、Price、Quantity、Type、Details、Date)值(@Party、@Product_Name、@Price、@Quantity、@Type、@Details、@Date)”,con)
您在查询中的@Date参数后添加了额外的逗号。没有错误,因为您将命令的ExecuteOnQuery方法置于try子句中,而从不使用exception子句捕获异常。捕获的异常的详细信息是什么?@Date,“在Date之后,您有额外的COMMATH,这就是您应该首先调试代码的原因。。哎呀,语法错误太多了。我的错。现在工作正常了。真的很抱歉我的愚蠢!谢谢大家每一个好眼睛我的朋友!Oops语法错误太多。我的错。现在工作正常了。真的很抱歉我的愚蠢!谢谢大家