Want connect VB.NET console application to SQL server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MuneerShajahan
    New Member
    • May 2014
    • 1

    #1

    Want connect VB.NET console application to SQL server

    Hi,

    I am new to VB.Net. I want to connect my VB.Net project to SQL server to pass the data. How do I do that?

    Appreciate your help with an example.
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    The same way you do with a Windows App :)

    Code:
    Module Module1
    
        'specify connection details.  If your domain accounts can't connect to the server/db you will need to provide credentials
        Dim sqCon As New SqlClient.SqlConnection("Server=SERVERNAME;;Database=DATABASENAME; Trusted_Connection=Yes;")
        Dim sqCmd As New SqlClient.SqlCommand
        Dim sdrRow As SqlClient.SqlDataReader
    
        Sub Main()
            'the next 5 rows are pretty self explanatory
            sqCmd.Connection = sqCon
            sqCmd.CommandText = "select top 10 * from TABLE"
            sqCon.Open()
            sqCmd.ExecuteNonQuery()
            sdrRow = sqCmd.ExecuteReader()
            For Each itm In sdrRow   'check each returned result from your query
                Dim column0 As String = sdrRow.GetValue(0)   'specify which column value you want
                MsgBox(column0)
            Next
            'make sure you close your connections!
            sdrRow.Close()
            sqCon.Close()
        End Sub
    
    End Module

    Comment

    Working...