在一项交易中插入多条记录 [英] Insert multiple records in one transaction

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

问题描述

早上好,

方案:用户希望使用Web表单将库存项目添加到库存表中.

例如

用户想要将textbox1中的Value添加到数据库10次.分别将值相加十倍.

例如
(GoodsInID,ProductID)
(1,555)
(2,555)
(3,555)

Good day,

Scenario: Using a web form the user wants to add stock items to the stock table.

E.g.

User wants to add Value in textbox1 to the database 10 times. as apposed adding the value individually ten times.

e.g.
(GoodsInID,ProductID)
(1,555)
(2,555)
(3,555)

protected void btnSubmit_click(object sender, EventArgs e)
    {
        try
        {
            using ( dbmodel dbContext = new dbmodel())
            {
                GoodsInDetails gdin = new GoodsInDetails();
                gdin.GoodsInID = int.Parse(txtorderID.Text);
                gdin.ProductID = int.Parse(txtbox1.Text);
                //**Add ProductID x amount of times dependednt on value in
                //txtQuanitity.text;***

                dbContext.Add(gdin);
                dbContext.SaveChanges();
                lblMessage.Text = "Succesfully added!";
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }
}



任何建议都很棒!

欢呼声
Jc

移动错误的解决方案":
我想在datagridview中多记录视图如何在sqltable



Any advice would be great!

Cheers
Jc

Move erroneous ''solution'':
hi i want to mutiple record view in datagridview how insert in sqltable

推荐答案

联系.基本上,打开数据库连接时将打开事务,然后在插入数据时保持事务打开.最后,您将根据插入的状态来提交或回滚数据.

中间的位很容易-您只需要循环n次,并在循环中进行迭代即可执行插入.
Well, the first thing you are going to need to do is open up a TransactionScope on your database connection. Basically, you''d open the transaction when you open the connection to the database and then you''d keep the transaction open while you insert the data. Finally, you''ll either commit or rollback the data depending on the status of the insert.

The bit in the middle is easy - you simply need to loop n times, and perform the insert while iterating over the loop.


了解交易基础知识

在此链接中查看交易主题

SQL Server 2005中的错误处理概述 [
Learn transaction basics

see transaction topic in this link

Overview of Error Handling in SQL Server 2005[^]


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

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