Results 1 to 6 of 6

Thread: SQL Table Informations

Threaded View

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Smile SQL Table Informations

    'This code will display the SQL Table Information
    Code:
    'Global declartion
    Imports System.Data
    Imports System.Data.SqlClient
    
    'In a Form
    'Add one command button and name it as Button2
    'Add one Dropdownbox and name it as cmbTablenames
    'Add one DataGridview control
    'And in the command button click event add this code
    
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                Dim con As New SqlConnection("User ID=sa;pwd=secreate;Initial Catalog=YourDatabaseName;Data Source=SQLSERVERNAME")
                Dim SchemaAdapter As New SqlDataAdapter("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME", con)
                con.Open()
                Dim Schematable As New DataTable
                SchemaAdapter.Fill(Schematable)
                Dim i As Long
                For i = 1 To Schematable.Rows.Count - 1
                    cmbTablenames.Items.Add(Schematable.Rows(i).Item("TABLE_NAME").ToString) 'add the tablenames
                Next
                Schematable.Dispose()
                con.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    
    'FUNCTION TO DISPLAY THE TABLE INFORMATION WITH RESPECT TO TABLENAME
     Private Sub DisplayTableInformation(ByVal tablename As String)
            Try
    
                'Function to display the table information
                'Add a datagridview control
                Dim con As New SqlConnection"User ID=sa;pwd=secreate;Initial Catalog=YourDatabaseName;Data Source=SQLSERVERNAME")
                Dim SQL As String
                SQL = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='" & tablename & "'"
                Dim SchemaAdapter As New SqlDataAdapter(SQL, con)
                con.Open()
                Dim Schematable As New DataTable
                SchemaAdapter.Fill(Schematable)
                DataGridView1.DataSource = Schematable
                 Schematable.Dispose()
                con.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    'In the DropDownBox Change event add this code
    Private Sub cmbTablenames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTablenames.SelectedIndexChanged
            DisplayTableInformation(cmbTablenames.Text)
        End Sub
    Last edited by danasegarane; Feb 27th, 2007 at 11:14 PM.
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width