momchaffeivb
Programmer
- Jan 18, 2012
- 2
i want to select a sql table using ado in my visual basic program
Imports System.Data.SqlClient
Imports System.Data
Public Class select_data
Private Sub Beep_Button1_Click(sender As System.Object, e As System.EventArgs) Handles Beep_Button1.Click
Dim dt As New DataTable()
Dim obj As New Ado
dt = GetStudentByName(Table.Text)
BindingSource1.DataSource = dt
DataGridView1.DataSource = BindingSource1
End Sub
Public Function GetStudentByName(ByVal name As String) As DataTable
Dim con As New SqlConnection("Data Source=.;initial catalog= test;integrated security=True")
Dim com As New SqlCommand
con.Open()
com.CommandText = "select * from " + Table.Text + ""
com.CommandType = CommandType.Text
com.Connection = con
Dim dr As SqlDataReader = com.ExecuteReader()
Dim dt As DataTable = New DataTable()
dt.Load(dr)
If dt.Rows.Count <> 0 Then
Return dt
con.Close()
Else
MsgBox("ERROR !", MsgBoxStyle.Critical)
Return Nothing
con.Close()
End If
End Function
End Class
this is my code are there any think bad (table)is a textbox
Imports System.Data.SqlClient
Imports System.Data
Public Class select_data
Private Sub Beep_Button1_Click(sender As System.Object, e As System.EventArgs) Handles Beep_Button1.Click
Dim dt As New DataTable()
Dim obj As New Ado
dt = GetStudentByName(Table.Text)
BindingSource1.DataSource = dt
DataGridView1.DataSource = BindingSource1
End Sub
Public Function GetStudentByName(ByVal name As String) As DataTable
Dim con As New SqlConnection("Data Source=.;initial catalog= test;integrated security=True")
Dim com As New SqlCommand
con.Open()
com.CommandText = "select * from " + Table.Text + ""
com.CommandType = CommandType.Text
com.Connection = con
Dim dr As SqlDataReader = com.ExecuteReader()
Dim dt As DataTable = New DataTable()
dt.Load(dr)
If dt.Rows.Count <> 0 Then
Return dt
con.Close()
Else
MsgBox("ERROR !", MsgBoxStyle.Critical)
Return Nothing
con.Close()
End If
End Function
End Class
this is my code are there any think bad (table)is a textbox