Visual Basic database support under .NET Core 5

Brandon Stewart 136 Reputation points
2021-03-16T05:17:55.947+00:00

I have a couple of Visual Basic apps, each of which connects to a local Microsoft Access database. I have upgraded my apps from using the .NET Framework to now use .NET Core 5. For some reason, I can no longer add a new data source via the wizard, nor by manually coding. I can still do it when creating VB WinForms app under the Framework, but not under .NET 5. This should be incredibly simply to do. Does VB under .NET 5 no longer support connecting to a database? I cannot find anything on a Google search which mentions anything about connecting to a local database under .NET 5, and the methods of doing it under .NET Framework don't seem to work.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,356 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-03-17T07:15:32.33+00:00

    Hi BrandonStewart-4522,
    Follow the steps below, you will find that "this window is not supported for the selected project." in Winforms(VB) .NET5.
    1.Open the Data Sources window, on the View menu, select Other Windows > Data Sources.
    It means that the wizard is not supported yet. So you need to connect to the database manually.
    I made a test and can access the local Microsoft Access database successfully.
    First add data connection in Server Explorer which on View menu.
    78559-317.png
    Then use the following code example to load records from Access Database to Datagridview.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\danielzh\Desktop\test.accdb")  
        Try  
            con.Open()  
    
            If con.State = ConnectionState.Open Then  
                MsgBox("Connected")  
            Else  
                MsgBox("Not Connected!")  
    
            End If  
        Catch ex As Exception  
            MsgBox(ex.Message)  
        Finally  
            con.Close()  
        End Try  
        Try  
            Dim sql As String  
            Dim cmd As New OleDb.OleDbCommand  
            Dim dt As New DataTable  
            Dim da As New OleDb.OleDbDataAdapter  
            con.Open()  
            sql = "Select * from Table1"  
            cmd.Connection = con  
            cmd.CommandText = sql  
            da.SelectCommand = cmd  
    
            da.Fill(dt)  
    
            DataGridView1.DataSource = dt  
        Catch ex As Exception  
            MsgBox(ex.Message)  
        Finally  
            con.Close()  
        End Try  
    End Sub  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.