locked
Select code in vb.net using MS Access RRS feed

  • Question

  • User632304213 posted

    i want select code in vb.net using ms access

    Monday, January 26, 2015 7:48 AM

Answers

  • User281315223 posted

    If memory serves, data connections to Access databases use the OleDbConnection class. So if you wanted to execute a query in Visual Basic, it might look something like this :

    ' Open your connection '
    Using accessConnection = New OleDbConnection("Your Connection String")
    	' Build your query '
    	Dim query = "SELECT * FROM YourTable"
    
    	' Build a command to execute your query '
    	Using accessCommand = New OleDbCommand(query, accessConnection)
    		' Open your connection '
    		accessConnection.Open()
    
    		' Execute and read your query '
    		Using accessReader = accessCommand.ExecuteReader()
    			' Read through your results '		
    			While accessReader.Read()
                                  ' Access your individual rows here (using reader("Column")) '
    			End While
    		End Using
    	End Using
    End Using

    It really depends on exactly what you wanted to do with the query, but the above is how you might execute it. If you wanted to do something like bind it to a GridView, then you could use the following :

    ' Open your connection '
    Using accessConnection = New OleDbConnection("Your Connection String")
    	' Build your query '
    	Dim query = "SELECT * FROM YourTable"
    
    	' Build a command to execute your query '
    	Using accessCommand = New OleDbCommand(query, accessConnection)
    		' Open your connection '
    		accessConnection.Open()
    
    		' Execute and read your query '
    		Using accessReader = accessCommand.ExecuteReader()
    			' Bind your results to the Grid '
                            YourGridView.DataSource = accessReader
                            YourGridView.DataBind()
    		End Using
    	End Using
    End Using

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2015 8:00 AM

All replies

  • User281315223 posted

    If memory serves, data connections to Access databases use the OleDbConnection class. So if you wanted to execute a query in Visual Basic, it might look something like this :

    ' Open your connection '
    Using accessConnection = New OleDbConnection("Your Connection String")
    	' Build your query '
    	Dim query = "SELECT * FROM YourTable"
    
    	' Build a command to execute your query '
    	Using accessCommand = New OleDbCommand(query, accessConnection)
    		' Open your connection '
    		accessConnection.Open()
    
    		' Execute and read your query '
    		Using accessReader = accessCommand.ExecuteReader()
    			' Read through your results '		
    			While accessReader.Read()
                                  ' Access your individual rows here (using reader("Column")) '
    			End While
    		End Using
    	End Using
    End Using

    It really depends on exactly what you wanted to do with the query, but the above is how you might execute it. If you wanted to do something like bind it to a GridView, then you could use the following :

    ' Open your connection '
    Using accessConnection = New OleDbConnection("Your Connection String")
    	' Build your query '
    	Dim query = "SELECT * FROM YourTable"
    
    	' Build a command to execute your query '
    	Using accessCommand = New OleDbCommand(query, accessConnection)
    		' Open your connection '
    		accessConnection.Open()
    
    		' Execute and read your query '
    		Using accessReader = accessCommand.ExecuteReader()
    			' Bind your results to the Grid '
                            YourGridView.DataSource = accessReader
                            YourGridView.DataBind()
    		End Using
    	End Using
    End Using

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2015 8:00 AM