Connection To DataTable


Public Class Form24

    Private Sub Form24_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try

            'Create Connection Object using SQLClient Namespace
            Dim SQLCon As New SqlClient.SqlConnection


            'Assign Connection String to Object of SQlConnection
            SQLCon.ConnectionString = "Password=Sql2012;Persist Security Info=False;User ID=sa;Initial Catalog=ACFASajjan24112016;Data Source=DS\CDATA2012"


            'To Open Connectivity With Database
            SQLCon.Open()


            'SQL to Access Table/View from
            Dim strSQL As String = "Select * from Table1 Order by Field1"


            'Data Adapter :-This Act as Bridge between Backend and Front End. Whenever you need Data in Front you need to have Data Adapter  
            Dim SQLADP As New SqlClient.SqlDataAdapter(strSQL, SQLCon)


            'Data Table :- XML based Storage of Data in Front End [Locally/In Memory]. This can't direct Access Backend, SO it uses Data Adapter.
   'Data Table Memory Copy of View or Table. you can use same Loop/Read Column by Column or Row by Row
            Dim dtTable As New DataTable

   'Adapter Method Fill :- Command will Fill DatatTable with Source Sql passed in Data Adapter.
            ' We Can Compare this ADO vs of Recordset but local. Once its Filled in Datatable there is no link between Backend and Datatable and Change to source    ' Data will not effect to Datatable
            SQLADP.Fill(dtTable)


            ' Datatable.Rows.Count will Return no. of Rows in Table Index Starts from Zero
            If dtTable.Rows.Count <> 0 Then
                MessageBox.Show("Data Found", "Message", MessageBoxButtons.OK)
            Else
                MessageBox.Show("No Records Found", "Message", MessageBoxButtons.OK)
            End If


        Catch ex As Exception
            MessageBox.Show("Error Occured in Connection :-" & ex.Message, "Error", MessageBoxButtons.OK)
        End Try

    End Sub
End Class

Comments

Popular posts from this blog

Shared / Static Class in vb.net/C#

Xamarin Forms : PopUp Page with RG.Plugins.Popup

Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)