10月 25, 2014

免用SQL的SqlDataAdapter的新增修改和刪除

        Dim dt As New DataTable
        Using cn As New SqlConnection(conn)
            Dim rr$ = "select * from city where id=@id"

            Dim cmds As New SqlDataAdapter(rr, cn)
            cmds.SelectCommand.Parameters.AddWithValue("id", 8)
            cmds.Fill(dt)
            Dim cb As New SqlCommandBuilder(cmds)
            '新增
            'Dim dr As DataRow = dt.NewRow
            'dr("id") = 9
            'dr("pp") = "hh"
            'dt.Rows.Add(dr)
            'dt.Rows.Add(8, "ert")'偷懶用
            '修改
            'dt.Rows(0)("city") = "666"'偷懶用
            'For Each dr As DataRow In dt.Select("id=" & 8)
            '    dr("kk") = "hello"
            'Next
            '刪除
            'dt.Rows(0).Delete()'偷懶用
            'For Each dr As DataRow In dt.Select("id=" & 8)
            '    dr.Delete()
            'Next
            cmds.Update(dt)
            cmds.Dispose()
        End Using
        GridView1.DataSource = dt
        GridView1.DataBind()