View Full Version : اضافه کردن جداول
  
bebakhshid
سه شنبه 11 مرداد 1384, 18:36 عصر
سلام
من 2 تا table دارم که دارای فیلدهای یکسانی هستند.
خالا میخوام محتوای 2 تا table را با هم ادغام کنم.
ایا دستوری برای اینکار وجود داره.
بدون sql
اولین مشکل:
من اسم table 2 را از openfiledialog میخونم .
پس من اسم db را دارم نه tableدوم را
اگر امکان داره با کد باشه
علیرضا مداح
سه شنبه 11 مرداد 1384, 20:14 عصر
سلام .
برای انجام اینکار میبایست از مند Merge مربوط به شی ء دیتاست استفاده نمایید .
مثالی از MSDN :
    Private Sub DemonstrateMergeTable()
        ' Create a DataSet with one table, two columns, and ten rows.
        Dim ds As New DataSet("myDataSet")
        Dim t As New DataTable("Items")
        ' Add tables to the DataSet
        ds.Tables.Add(t)
        ' Add columns
        Dim c1 As New DataColumn("id", Type.GetType("System.Int32"), "")
        Dim c2 As New DataColumn("Item", Type.GetType("System.Int32"), "")
        t.Columns.Add(c1)
        t.Columns.Add(c2)
        ' DataColumn array to set primary key.
        Dim keyCol(1) As DataColumn
        ' Set primary key column.
        keyCol(0) = c1
        t.PrimaryKey = keyCol
        ' Add RowChanged event handler for the table.
        AddHandler t.RowChanged, AddressOf Row_Changed
        ' Add ten rows.
        Dim i As Integer
        Dim r As DataRow
        For i = 0 To 9
            r = t.NewRow()
            r("id") = i
            r("Item") = i
            t.Rows.Add(r)
        Next i
        ' Accept changes.
        ds.AcceptChanges()
        PrintValues(ds, "Original values")
        ' Create a second DataTable identical to the first.
        Dim t2 As DataTable
        t2 = t.Clone()
        ' Add three rows. Note that the id column can't be the 
        ' same as existing rows in the DataSet table.
        Dim newRow As DataRow
        newRow = t2.NewRow()
        newRow("id") = 14
        newRow("Item") = 774
        t2.Rows.Add(newRow)
        newRow = t2.NewRow()
        newRow("id") = 12
        newRow("Item") = 555
        t2.Rows.Add(newRow)
        newRow = t2.NewRow()
        newRow("id") = 13
        newRow("Item") = 665
        t2.Rows.Add(newRow)
        ' Merge the table into the DataSet.
        Console.WriteLine("Merging")
        ds.Merge(t2)
        PrintValues(ds, "Merged With Table")
    End Sub 'DemonstrateMergeTable
    Private Sub Row_Changed(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)
        Console.WriteLine("Row Changed " + e.Action.ToString() _
           + ControlChars.Tab + e.Row.ItemArray(0).ToString())
    End Sub 'Row_Changed
    Private Sub PrintValues(ByVal ds As DataSet, ByVal label As String)
        Console.WriteLine(ControlChars.Cr + label)
        Dim t As DataTable
        Dim r As DataRow
        Dim c As DataColumn
        For Each t In ds.Tables
            Console.WriteLine("TableName: " + t.TableName)
            For Each r In t.Rows
                For Each c In t.Columns
                    Console.Write(ControlChars.Tab + " " + r(c).ToString())
                Next c
                Console.WriteLine()
            Next r
        Next t
    End Sub
bebakhshid
سه شنبه 11 مرداد 1384, 23:42 عصر
سلام
این کد درسته.
اما در این کد محتوای کانکشن به datatable  باز است و در برنامه به ان مقدار داده شده.
اما من میخوام محتوای یه table  که قبلا پر شده را بخوانم
bebakhshid
پنج شنبه 13 مرداد 1384, 12:27 عصر
it is tooooooooooooooooooooooooooooooooooooooooooooooooo ooooooooooooo important
bebakhshid
جمعه 14 مرداد 1384, 13:09 عصر
من این کد به نظرم میرسه غلطه اگر امان داره بگین در تعریف dataadaptor باید چیچی بنویسم
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim con2 As New OleDbConnection()
            con2.ConnectionString = _
              ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                              "Data Source=" & OpenFileDialog1.FileName)
            con2.Open()
            Dim da As New OleDbDataAdapter("select * from" & con2.Container, con2)
            Dim ds As New DataSet()
            da.Fill(ds, "db2")
            con2.Open()
            Dim dr As DataRow()
            For Each dr In ds.Tables(0).Rows
                DataSet11.Merge(ds, True)
            Next
            OleDbDataAdapter1.Update(DataSet11)
        End If
    End Sub
 
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.