SqlDataAdapter + BindingSource 去 Textbox

 

   Layout Information
 
   TextBox1 – 放 Soccer ID 既 Textbox
   TextBox2 – 放 Soccer Name 既 Textbox
   Button2 – Move First 既制
   Button3 – Move Previous 既制
   Button4 – Move Next 既制
   Button5 – Move Last 既制
 

 

   SqlDataAdapter + BindingSource 去 Textbox
 
   Imports System.Data.SqlClient
 
   …………………
 
   Dim bs As BindingSource
 
   Private Sub Bind_Textbox()
 
      Dim connectionString1 As String = "Data Source=.\SqlExpress; Initial Catalog=Soccer; Integrated Security=SSPI"
      Dim con1 As SqlConnection = New SqlConnection(connectionString1)
      con1.Open()
 
      Dim SqlString1 As String = "SELECT Player_ID, Player_Name FROM dbo.Player"
 
      Dim Adapter As New SqlDataAdapter
      Adapter.SelectCommand = New SqlCommand(SqlString1.ToString(), con1)
 
      Dim ds As New DataSet
      Adapter.Fill(ds, "dbo.Player")
      bs = New BindingSource(ds, "dbo.Player")
 
      Dim binding1 As New Binding("Text", bs, "Player_ID")
      TextBox1.DataBindings.Add(binding1)
      TextBox1.ReadOnly = True
 
      Dim binding2 As New Binding("Text", bs, "Player_Name")
      TextBox2.DataBindings.Add(binding2)
      TextBox2.ReadOnly = True
 
      ds.Dispose()
      con1.Dispose()
 
   End Sub
 
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      If bs.Position > 0 Then
         bs.MoveFirst()
      End If
   End Sub
 
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      If bs.Position > 0 Then
         bs.MovePrevious()
      End If
   End Sub
 
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
      If bs.Position < bs.Count – 1 Then
         bs.MoveNext()
      End If
   End Sub
 
   Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
      If bs.Position < bs.Count – 1 Then
         bs.MoveLast()
      End If
   End Sub