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