Results 1 to 7 of 7

Thread: save datatable to database

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    save datatable to database

    Hello There,
    i would to ask question on how to update my database table by the data inside my datatable.

    this is my class
    Code:
    public class Sample
    
    Private ds As New DataSet("myDs")
    private dt as new Datatable("myDT")
    Private data_adapter As SqlDataAdapter
    
    private sub FormLoad(....)
    
    
            'Add datatable object to dataset object.
            ds.Tables.Add(dt)
    
            'Create a new column for datatable.
            Dim dc As New DataColumn("RangeNo", System.Type.[GetType]("System.String"))
            dc.DefaultValue = ""
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            dt.PrimaryKey = New DataColumn() {dt.Columns("RangeNo")}
    
            'Create a new column for datatable.
            dc = New DataColumn("Minimum", System.Type.[GetType]("System.String"))
            dc.DefaultValue = ""
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            'Create a new column for datatable.
            dc = New DataColumn("Maximum", System.Type.[GetType]("System.String"))
            dc.DefaultValue = "0"
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            'Create a new column for datatable.
            dc = New DataColumn("Max_Interest", System.Type.[GetType]("System.String"))
            dc.DefaultValue = "0"
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            'Create a new column for datatable.
            dc = New DataColumn("Term", System.Type.[GetType]("System.String"))
            dc.DefaultValue = "0"
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            'Create a new column for datatable.
            dc = New DataColumn("Remainder", System.Type.[GetType]("System.String"))
            dc.DefaultValue = "0"
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            'Create a new column for datatable.
            dc = New DataColumn("IsPercent", System.Type.[GetType]("System.Boolean"))
            'dc.DefaultValue = False
            dc.AllowDBNull = False
    
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
            'Create a new column for datatable.
            dc = New DataColumn("ID", System.Type.[GetType]("System.Int32"))
            'dc.DefaultValue = False
            'dc.AllowDBNull = False
    
            'Add created column to datatable object.
            dt.Columns.Add(dc)
    
    '--------------------------------------------------------------
                   ds.Tables("myDT").Clear()
                    If oCNN.State = ConnectionState.Closed Then
                        DB_Init()
                    End If
                    data_adapter = New SqlDataAdapter(SELECT_STRING, oCNN)
    
                    data_adapter.FillSchema(ds, SchemaType.Source, "myDT")
    
                    data_adapter.Fill(ds, "myDT")
    end sub
    
    private sub DeleteRow(...)
       ds.tables("myDT").rows(myGrid.visiblerow -1).delete
       ds.tables("myDT").AcceptChanges()
    
       myGrid.datasource = ds.tables("myDT")
    end sub
    
    private sub UpdateDatabase(..)
        data_adapter = New SqlDataAdapter("SELECT * FROM Table1 WHERE myField like 'Range%' order by ID", oCNN)
    
                    Dim objCommandBuilder As New SqlCommandBuilder(data_adapter)
    
                    data_adapter.InsertCommand = objCommandBuilder.GetInsertCommand()
                    data_adapter.DeleteCommand = objCommandBuilder.GetDeleteCommand()
                    data_adapter.UpdateCommand = objCommandBuilder.GetUpdateCommand()
    
                    data_adapter.Update(ds, "myDT")
    end sub
    end class
    i had problem in sub deleterow AcceptChanges() method not seems to work.i cannot update my database if i put AcceptChanges() method after the delete row..

    if i delete 1 row then hit the updatedatabase sub the record in my database is the same..seems like there is no delete happened..my server is mssql2000

    Thank you..
    Last edited by [gja]; Sep 27th, 2010 at 10:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width