多次插入记录 [英] Insert Record Multiple times

查看:102
本文介绍了多次插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我是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

推荐答案



使用DataTable,
根据需要添加列,然后一一添加.

当您添加所有3行时,您将使用一个数据表在您的数据库中进行修改
Hi,

use DataTable,
add column as much you want then add row one by one.

when u add all 3 rows u hv a datatable use it for modification at your DataBase


在SQL Server 2008及更高版本中,您可以使用MySQL扩展语法
In SQL Server 2008 and up you can use the MySQL extension syntax
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.


更好使用数据表的方式.

就像.

将dim dtdatata用作新的数据表

使用dtdatata.Columns
.Add(")
.Add(")
结尾为
dtdatata.Rows.Add(dtdatata.NewRow)
better way you use datatable.

just like.

dim dtdatata as new datatable

With dtdatata.Columns
.Add("")
.Add("")
End With
dtdatata.Rows.Add(dtdatata.NewRow)


这篇关于多次插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆