Textbox 配合 SQL Query + SqlDataAdapter + DataGridView 使用方法
Form1.vb |
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As KeyEventArgs) _ Handles TextBox1.KeyUp If e.KeyCode = Keys.Enter Then Dim A As String Try A = CStr(CInt(TextBox1.Text.Trim())) Catch ex As Exception A = "0" End Try getdataDG(A) End If End Sub Private Sub getdataDG(ByVal A As String) Try If Not (A = 0) Then 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 WHERE Player_ID = '" & A & "'" Dim Adapter As New SqlDataAdapter Adapter.SelectCommand = New SqlCommand(SqlString1.ToString(), con1) Dim ds As New DataSet Adapter.Fill(ds) If ds.Tables(0).Rows.Count > 0 Then DataGridView1.DataSource = ds.Tables(0).DefaultView Else DataGridView1.DataSource = New DataTable End If ds.Dispose() con1.Dispose() Else DataGridView1.DataSource = New DataTable End If Catch ex As Exception End Try End Sub |