在运行时将随机行从一个表复制到另一个表中

发布于 2024-11-07 02:29:28 字数 465 浏览 1 评论 0原文

我有一个数据库,里面有两个表。一个是项目表(表 1),另一个是当前项目表(表 2)。 在表单加载时,我需要从 table1 中生成随机数量的项目并用它们填充 table2 。我试图做这样的事情:

  While numberofitems >= 1
     Dim i As Integer

     index = myGenerator.Next(_minitem, _maxitem)

     i = _itemsListBindingSource.IndexOf(index)

     _bindingSource.Add(_itemsListBindingSource.Item(i))

     numberofitems -= 1

  End While

  Me.ItemsTableAdapter1.Fill(Me.Items2DataSet1.Items)

但这根本不起作用。任何人都可以提供任何想法吗?

I have a database that has two tables in it. One is a table of items (table1) and the other is the table of current items (table2).
On form load I need to generate a random amount of the items from table1 and populate table2 with them. I was trying to do something like this:

  While numberofitems >= 1
     Dim i As Integer

     index = myGenerator.Next(_minitem, _maxitem)

     i = _itemsListBindingSource.IndexOf(index)

     _bindingSource.Add(_itemsListBindingSource.Item(i))

     numberofitems -= 1

  End While

  Me.ItemsTableAdapter1.Fill(Me.Items2DataSet1.Items)

But that wasn't working at all. Can anyone offer any ideas?

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

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

发布评论

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

评论(1

不羁少年 2024-11-14 02:29:28

尝试我的解决方案(.net 3.5)。我创建了一个带有“Table1”和“Table2”以及表适配器的类型化数据集(在另一个名为 dsMain.xsd 的文件中)。我用数据库中“Table1”中的数据填充“SourceTable”,然后根据“SourceTable”中的行数抓取随机行并将它们放入“TargetTable”中。一旦“TargetTable”中有我的数据,我就使用“Update”语句将其提交到数据库。

    Dim SourceTable = (New dsMainTableAdapters.Table1TableAdapter).GetData
    Dim TargetTable As New dsMain.Table2DataTable

    Dim MyRandom As New Random()

    Const DesiredRowCount As Int32 = 20

    For i = 1 To DesiredRowCount
        Dim RandomRow = SourceTable(MyRandom.Next(0, SourceTable.Count))

        TargetTable.AddTable2Row(RandomRow(0))
    Next

    With (New dsMainTableAdapters.Table2TableAdapter)
        .Update(TargetTable) 'Save my data to the DB'
    End With

Try my solution (.net 3.5). I created a typed dataset with a "Table1" and a "Table2" with tableadapters (in another file called dsMain.xsd). I fill my "SourceTable" with data from "Table1" in the DB, then I grab random rows based on the number of rows in "SourceTable" and place them in "TargetTable". Once "TargetTable" has my data in it, I commit it to the database using an "Update" statement.

    Dim SourceTable = (New dsMainTableAdapters.Table1TableAdapter).GetData
    Dim TargetTable As New dsMain.Table2DataTable

    Dim MyRandom As New Random()

    Const DesiredRowCount As Int32 = 20

    For i = 1 To DesiredRowCount
        Dim RandomRow = SourceTable(MyRandom.Next(0, SourceTable.Count))

        TargetTable.AddTable2Row(RandomRow(0))
    Next

    With (New dsMainTableAdapters.Table2TableAdapter)
        .Update(TargetTable) 'Save my data to the DB'
    End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文