public class form1 inherits system.windows.forms.form
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click dim conn as new sqlconnection("data source=localhost;initial catalog=studentcourse;" & _ "user id=;password=;") dim ds as new dataset try conn.open() '在形成sqldataadapter前打开conn dim daauthors as new sqldataadapter("select * from sc", conn) dim bldr as new sqlcommandbuilder(daauthors) daauthors.fill(ds,"sc") conn.close() '在填充完ds后关闭连接,接着对ds进行操作
dim tbl as new datatable tbl = ds.tables("sc")
dim rowvals(3) as object rowvals(0) = "5" rowvals(1) = "00003" rowvals(2) = "0001" rowvals(3) = 99 dim insertedrow as datarow insertedrow = tbl.rows.add(rowvals) '添加一行
conn.open() daauthors.update(ds.tables("sc")) '须将结果传回数据源时打开连接,update conn.close() catch ex as exception messagebox.show(ex.message) end try end sub end class