使用 arraylist 将项目插入数据库

发布于 2024-11-08 22:53:09 字数 1008 浏览 0 评论 0原文

我已经使用 Order.aspx.vb 中的项目创建了一个 ArrayList。我将这些传递给我的 bllOrder,后者又将其传递给我的 dalOrder

Order.aspx.vb

Dim item As RepeaterItem
For Each item In rptProductList.Items
   objOrder.OrderCustID = Session("CustID")
   objOrder.OrderProdID = DirectCast(item.FindControl("ddlProducts"), DropDownList).SelectedValue
   bllOrder.InsertOrder(objOrder)
Next item

dalOrder

Function InsertOrder(ByVal objOrder As Order) As Boolean
    Dim Result as New Boolean

    myconn.open()

    Dim SQL As String = "INSERT INTO order(OrderCustID, OrderProdID) VALUES (?,?)"
    Dim cmd As New OdbcCommand(SQL, myconn)

    cmd.Parameters.AddWithValue("OrderCustID", objOrder.OrderCustID)
    cmd.Parameters.AddWithValue("OrderProdID", objorder.OrderProdID)

    result = cmd.ExecuteNonQuery()

    myconn.close()

    Return Result
End Function

这对于一项很有用,但是我该如何为我的 ArrayList 做到这一点呢?

非常感谢所有帮助!

I have created an ArrayList with items in my Order.aspx.vb. I pass these on to my bllOrder, which passes it on to my dalOrder.

Order.aspx.vb

Dim item As RepeaterItem
For Each item In rptProductList.Items
   objOrder.OrderCustID = Session("CustID")
   objOrder.OrderProdID = DirectCast(item.FindControl("ddlProducts"), DropDownList).SelectedValue
   bllOrder.InsertOrder(objOrder)
Next item

dalOrder

Function InsertOrder(ByVal objOrder As Order) As Boolean
    Dim Result as New Boolean

    myconn.open()

    Dim SQL As String = "INSERT INTO order(OrderCustID, OrderProdID) VALUES (?,?)"
    Dim cmd As New OdbcCommand(SQL, myconn)

    cmd.Parameters.AddWithValue("OrderCustID", objOrder.OrderCustID)
    cmd.Parameters.AddWithValue("OrderProdID", objorder.OrderProdID)

    result = cmd.ExecuteNonQuery()

    myconn.close()

    Return Result
End Function

This is good for one item, but how would I do it for my ArrayList?

All help is much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

焚却相思 2024-11-15 22:53:10

不要传递单个订单项,而是传递一个订单列表,然后它们在您的方法内循环它。使其像 Public Function InsertOrder(objOrder As List(Of Order)) As Boolean 一样,然后使用 objOrder 作为 Orders 列表来循环它。

将以下代码放入 foreach 循环中并传递当前项目值;

cmd.Parameters.AddWithValue("OrderCustID", objOrder.OrderCustID)
    cmd.Parameters.AddWithValue("OrderProdID", objorder.OrderProdID)

    result = cmd.ExecuteNonQuery()

instead of passing single Order item, pass a List of Orders and them loop it though inside your method. make it like that Public Function InsertOrder(objOrder As List(Of Order)) As Boolean and then use objOrder as a list of Orders to loop it through.

put the following code inside a foreach loop following code and pass the current item values;

cmd.Parameters.AddWithValue("OrderCustID", objOrder.OrderCustID)
    cmd.Parameters.AddWithValue("OrderProdID", objorder.OrderProdID)

    result = cmd.ExecuteNonQuery()
在风中等你 2024-11-15 22:53:10

将项目数组转换为 xml 字符串,然后您可以使用 openxml 在存储过程中进行批量插入。 http://msdn.microsoft.com/en-us/library/ms191268.aspx 另请参阅 sql server 2005 的旧帖子http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx

Convert the array of items into an xml string and you can do a bulk insert in the stored procedure using openxml. http://msdn.microsoft.com/en-us/library/ms191268.aspx also refer an older post for sql server 2005 http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx

香草可樂 2024-11-15 22:53:10

** 进行编辑以考虑额外信息 **

您可以调整代码隐藏中现有的“for every”逻辑来构建 arraylist 或 List<>产品 - 该数组/列表应该是您的 Order 对象的属性。目前,通过 BAL 将 Order 对象传递给 DAL。

然后在 DAL 代码 (InsertOrder) 中迭代 Order 对象中的产品,并将

  • 每行单独插入到
    循环
  • 或动态构建 Order 的插入语句

您应该将其包装在事务中,以确保在一行插入失败时订单能够完全回滚。

对于大量产品的订单,我会选择@Aravind 的答案。

** edited to account for extra info **

You could adapt the existing "for each" logic in your codebehind to build an arraylist or List<> of products - this array/list should be a property of your Order object. Pass the Order object to the DAL via your BAL as currently.

Then iterate through the products in the Order object in the DAL code(InsertOrder) and either

  • insert each row individually in a
    loop
  • or dynamically build an insert statement for the Order
    .

You should wrap it in a transaction to ensure the order is rolled back competely if one row fails to insert.

For orders with large amout of products i'd go for @Aravind's answer.

我不是你的备胎 2024-11-15 22:53:10

我会使用 SqlClient.SqlBulkCopy。这实际上是批量插入的 .Net 版本,要使用它,您需要将要插入的对象插入到 DataTable 中,或者创建一个实现 IDDataReader 的类来读取数据。如果您插入 1,000 行,那么您应该会看到性能显着提高并且代码更加整洁。

http://msdn.microsoft.com/en-us /library/system.data.sqlclient.sqlbulkcopy.aspx

I’d use SqlClient.SqlBulkCopy. This is effectively a .Net version of Bulk Insert, to use it you need to either have your objects you want to insert in a either a DataTable or create a class to read your data that implements IDDataReader. If your inserting 1,000’s of rows then you should see a dramatic performace increase and much tidier code.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

耳钉梦 2024-11-15 22:53:10

我建议您使用逗号分隔值。不要在 DAL 层中发送数组列表,而是函数应从表示层返回格式化的逗号分隔值,并将该值发送到 DAL,最后在存储过程中分隔这些值。

选择
您可以将值格式化为 XML 并将其发送到存储过程。

为了执行它,您的参数应该是 varchar 类型。

I will suggest you to use the comma seperated values . Do not send the array list in your DAL Layer instead a function should return a formatted comma seperated value from the Presentation Layer and send this value to DAL and finally seperate the values in your stored procedure.

Alternative
You can format your values in an XML and send it to Stored Procedure.

In order to perform it your parameter should be of varchar type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文