【发布时间】: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