C# 在插入数据库之前在列表中插入多个项目
因此,我在数据库中的关系如下: 其想法是在将列表插入数据库之前将多个产品类别添加到列表中 我使用的是ADO.NET框架,映射的类products包含产品类别列表。因此,当用户填写产品类别所需的表格时,我会说:C# 在插入数据库之前在列表中插入多个项目,c#,sql-server,list,ado.net,C#,Sql Server,List,Ado.net,因此,我在数据库中的关系如下: 其想法是在将列表插入数据库之前将多个产品类别添加到列表中 我使用的是ADO.NET框架,映射的类products包含产品类别列表。因此,当用户填写产品类别所需的表格时,我会说: Products p = new Products(... initializing); ProductCategories p2 = new ProductCategories(...initializing); p.ProductCategories.Add(p2); 我不明白的是
Products p = new Products(... initializing);
ProductCategories p2 = new ProductCategories(...initializing);
p.ProductCategories.Add(p2);
我不明白的是,在我将这些ProductCategories项目插入数据库之前,如何将更多这些ProductCategories项目添加到此列表中。每当我添加新的,旧的似乎从列表中删除,我不明白为什么?顺序应如下所示:
第一产品类别项目
第二
第三
如果用户停止添加它们…->单击保存按钮->将列表中的所有产品类别插入到产品类别表中
相反,我只是用我刚刚插入的新值覆盖了旧产品类别。有人能帮我解决这个问题吗
编辑:下面是负责将多个项目添加到列表中的完整代码
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0)
{
p = new Product();
// p is declared as property outside of all methods...
p.ProductName = txtProductTitle.Text;
p.Dimensions = ".";
p.PricePerUnit = float.Parse(txtProductPrice.Text);
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
MemoryStream mem = new MemoryStream();
img.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
p.ProductPicture = mem.ToArray();
p.ProductPicThumb = mem.ToArray();
p.MeasurementUnitID = int.Parse(dropMeasure.SelectedValue);
p.PricePerFeet = 1.2F;
int pid = ServiceClass.InsertProduct(p);
ProductCategories p2 = new ProductCategories(...initializing);
p.ProductCategories.Add(p2);
Response.Write("<script>alert('Product successfully added!');</script>");
}
else
{
Response.Write("<script>alert('Something went wrong!');</script>");
}
需要更多信息、工作代码等。您每次都在重新创建列表吗?看起来您的db有1到多个产品到类别,但您确实需要多个产品添加一个新的表产品。\u ProductCategory ProductId,ProductCategoryId可能会这样做,但如何在将多个项目插入db之前在列表中插入多个项目,这是主要问题lolz.@BerndLinde我已经解决了,这不是问题的原因。我现在将粘贴整个代码。。。另外,我已经用整个代码编辑了主要问题。。。