问题描述
早上好,
我是C#编码的新手,因此非常感谢您提供一些指导.
我正在使用以下方法写入SQL Server数据库:
Good day,
I am new to C# coding, so would really appreciate some guidance.
I am using the following method to write to a SQL server database:
protected void btnclick_click(object sender, EventArgs e)
{
try
{
using (oaPDQmodel dbContext = new oaPDQmodel())
{
TestTables tst = new TestTables();
tst.ProductID = Convert.ToInt32(txtProductCode.Text);
dbContext.Add(tst);
dbContext.SaveChanges();
lblMessage.Text = "Succesfully added!";
}
}
我想创建一个将多个项目添加到数据库的应用程序,例如一次添加一个项目,用户可以确定要添加的数量,例如
产品代码555 =收到3件商品.
因此,数据库将记录以下内容.
库存编号,产品代码
1- 555
2- 555
3- 555
所以也许:
I want to create an application that will add multiple items to the database for example as appose to adding one item at a time, the user can determine the amount to be added e.g.
Product code 555 = 3 items recieved.
So the database would have the following logged.
stock ID, Productcode
1- 555
2- 555
3- 555
so perhaps:
protected void btnclick_click(object sender, EventArgs e)
{
try
{
using (oaPDQmodel dbContext = new oaPDQmodel())
{
TestTables tst = new TestTables();
tst.ProductID = Convert.ToInt32(txtboxProductCode.Text);
for (int i = 0 ; i<=convert.ToInt32(txtboxQuantity.text); i++);
dbContext.Add(tst);
dbContext.SaveChanges();
lblMessage.Text = "Succesfully added!";
}
}
在aspx页面上,我将具有以下控件:
txtboxProductCode =插入产品代码555
txtboxquantity =我要将产品代码555插入表中的次数.
btnCLick =根据txtbox2特定时间写入产品代码
非常感谢任何建议!
亲切的问候JC
On the aspx page I would have the following controls:
txtboxProductCode= insert product code 555
txtboxquantity = the amount of times I want to insert product code 555 to the table.
btnCLick= Writes the product code a specific amount of times dependent on txtbox2
Would really appreciate any advice!
Kind regards JC
推荐答案
insert into tablename (stockID, productCode) values (1, 555), (2, 555), (3, 555)
但是,由于您使用的是实体关系层,所以我认为您可以向dbContext添加多个详细信息副本,并且在提交时应该提交一个巧妙的查询.
However, since you''re using an entity relationship layer, I would think you can just add several copies of detail to dbContext and it should submit a clever query when you commit it.
这篇关于多次插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!