How to connect Access Database to a VB.Net project in VS 2022 using .Net 8.0?

VKSB 236 Reputation points
2024-06-20T18:51:16.6666667+00:00

Hi All,

I am trying to use .Net 8.0 in Visual Studio 2022; I am unable to use Access Database; the usual way of adding "Data Sources" is not available ( I have been using .Net Framework 4.5 for my projects without any difficulties.)

I used the book "Visual Basic 2005 Step By Step" to learn how to add Database in a project.

Can you please guide me to get it done ?

Thanks,

VKSB

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,906 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,733 questions
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 3,031 Reputation points
    2024-06-23T02:31:54.3166667+00:00

    Why do you need to use .NET 8.0 for Windows Forms app? I suggest that you use .NET Framework 4.8 which will be supported probably longer than .NET 8.0.

    If for some reason use of .NET 8.0 is must, you will be able to create Windows Forms app as described below although it is not practical in my opinion.

    Even in the .NET 8.0 Windows Forms app project you will be able to create a strongly-typed DataSet / DataTable and TableAdapter.

    Add new item, DataSet:

    enter image description here

    xsd file will be created:

    enter image description here

    Add the following Nuget packages and build the project:

    enter image description here

    After the above, you will have to do everything which is usually done by the Data Source Configuration Wizard in the .NET Framework project by yourself.

    Copy the png images used in the BindingNavigator form the existing .NET Framework Windows Forms app to the Form1.resx:

    enter image description here

    Write code as shown in the following sample (only sample):

    namespace WinFormsApp2
    {
        public partial class Form1 : Form
        {
            private NorthwindDataSet northwindDataSet;
            private System.Windows.Forms.BindingSource productsBindingSource;
            private NorthwindDataSetTableAdapters.ProductsTableAdapter productsTableAdapter;
            private NorthwindDataSetTableAdapters.TableAdapterManager tableAdapterManager;
            private System.Windows.Forms.BindingNavigator productsBindingNavigator;
            private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
            private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
            private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;
            private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;
            private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;
            private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;
            private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;
            private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;
            private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;
            private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;
            private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;
            private System.Windows.Forms.ToolStripButton productsBindingNavigatorSaveItem;
            private System.Windows.Forms.DataGridView productsDataGridView;
     
            public Form1()
            {
                InitializeComponent();
     
                this.components = new System.ComponentModel.Container();
                System.ComponentModel.ComponentResourceManager resources = 
                    new System.ComponentModel.ComponentResourceManager(typeof(Form1));
                this.northwindDataSet = new NorthwindDataSet();
                this.productsBindingSource = new System.Windows.Forms.BindingSource(this.components);
                this.productsTableAdapter = new NorthwindDataSetTableAdapters.ProductsTableAdapter();
                this.tableAdapterManager = new NorthwindDataSetTableAdapters.TableAdapterManager();
                this.productsBindingNavigator = new System.Windows.Forms.BindingNavigator(this.components);
                this.bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();
                this.bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();
                this.bindingNavigatorDeleteItem = new System.Windows.Forms.ToolStripButton();
                this.bindingNavigatorMoveFirstItem = new System.Windows.Forms.ToolStripButton();
                this.bindingNavigatorMovePreviousItem = new System.Windows.Forms.ToolStripButton();
                this.bindingNavigatorSeparator = new System.Windows.Forms.ToolStripSeparator();
                this.bindingNavigatorPositionItem = new System.Windows.Forms.ToolStripTextBox();
                this.bindingNavigatorSeparator1 = new System.Windows.Forms.ToolStripSeparator();
                this.bindingNavigatorMoveNextItem = new System.Windows.Forms.ToolStripButton();
                this.bindingNavigatorMoveLastItem = new System.Windows.Forms.ToolStripButton();
                this.bindingNavigatorSeparator2 = new System.Windows.Forms.ToolStripSeparator();
                this.productsBindingNavigatorSaveItem = new System.Windows.Forms.ToolStripButton();
                this.productsDataGridView = new System.Windows.Forms.DataGridView();
                ((System.ComponentModel.ISupportInitialize)(this.northwindDataSet)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.productsBindingSource)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.productsBindingNavigator)).BeginInit();
                this.productsBindingNavigator.SuspendLayout();
                ((System.ComponentModel.ISupportInitialize)(this.productsDataGridView)).BeginInit();
                this.SuspendLayout();
     
                // 
                // northwindDataSet
                // 
                this.northwindDataSet.DataSetName = "NorthwindDataSet";
                this.northwindDataSet.SchemaSerializationMode = 
                    System.Data.SchemaSerializationMode.IncludeSchema;
                // 
                // productsBindingSource
                // 
                this.productsBindingSource.DataMember = "Products";
                this.productsBindingSource.DataSource = this.northwindDataSet;
                // 
                // productsTableAdapter
                // 
                this.productsTableAdapter.ClearBeforeFill = true;
                // 
                // tableAdapterManager
                // 
                this.tableAdapterManager.BackupDataSetBeforeUpdate = false;
                this.tableAdapterManager.ProductsTableAdapter = this.productsTableAdapter;
                this.tableAdapterManager.UpdateOrder = 
                    NorthwindDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete;
                // 
                // productsBindingNavigator
                // 
                this.productsBindingNavigator.AddNewItem = this.bindingNavigatorAddNewItem;
                this.productsBindingNavigator.BindingSource = this.productsBindingSource;
                this.productsBindingNavigator.CountItem = this.bindingNavigatorCountItem;
                this.productsBindingNavigator.DeleteItem = this.bindingNavigatorDeleteItem;
                this.productsBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.bindingNavigatorMoveFirstItem,
                this.bindingNavigatorMovePreviousItem,
                this.bindingNavigatorSeparator,
                this.bindingNavigatorPositionItem,
                this.bindingNavigatorCountItem,
                this.bindingNavigatorSeparator1,
                this.bindingNavigatorMoveNextItem,
                this.bindingNavigatorMoveLastItem,
                this.bindingNavigatorSeparator2,
                this.bindingNavigatorAddNewItem,
                this.bindingNavigatorDeleteItem,
                this.productsBindingNavigatorSaveItem});
                this.productsBindingNavigator.Location = new System.Drawing.Point(0, 0);
                this.productsBindingNavigator.MoveFirstItem = this.bindingNavigatorMoveFirstItem;
                this.productsBindingNavigator.MoveLastItem = this.bindingNavigatorMoveLastItem;
                this.productsBindingNavigator.MoveNextItem = this.bindingNavigatorMoveNextItem;
                this.productsBindingNavigator.MovePreviousItem = this.bindingNavigatorMovePreviousItem;
                this.productsBindingNavigator.Name = "productsBindingNavigator";
                this.productsBindingNavigator.PositionItem = this.bindingNavigatorPositionItem;
                this.productsBindingNavigator.Size = new System.Drawing.Size(1136, 25);
                this.productsBindingNavigator.TabIndex = 0;
                this.productsBindingNavigator.Text = "bindingNavigator1";
                // 
                // bindingNavigatorAddNewItem
                //
    #nullable disable
                this.bindingNavigatorAddNewItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.bindingNavigatorAddNewItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("bindingNavigatorAddNewItem.Image")));
                this.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem";
                this.bindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = true;
                this.bindingNavigatorAddNewItem.Size = new System.Drawing.Size(23, 22);
                this.bindingNavigatorAddNewItem.Text = "新規追加";
                // 
                // bindingNavigatorCountItem
                // 
                this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";
                this.bindingNavigatorCountItem.Size = new System.Drawing.Size(29, 22);
                this.bindingNavigatorCountItem.Text = "/ {0}";
                this.bindingNavigatorCountItem.ToolTipText = "項目の総数";
                // 
                // bindingNavigatorDeleteItem
                // 
                this.bindingNavigatorDeleteItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.bindingNavigatorDeleteItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("bindingNavigatorDeleteItem.Image")));
                this.bindingNavigatorDeleteItem.Name = "bindingNavigatorDeleteItem";
                this.bindingNavigatorDeleteItem.RightToLeftAutoMirrorImage = true;
                this.bindingNavigatorDeleteItem.Size = new System.Drawing.Size(23, 22);
                this.bindingNavigatorDeleteItem.Text = "削除";
                // 
                // bindingNavigatorMoveFirstItem
                // 
                this.bindingNavigatorMoveFirstItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.bindingNavigatorMoveFirstItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveFirstItem.Image")));
                this.bindingNavigatorMoveFirstItem.Name = "bindingNavigatorMoveFirstItem";
                this.bindingNavigatorMoveFirstItem.RightToLeftAutoMirrorImage = true;
                this.bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size(23, 22);
                this.bindingNavigatorMoveFirstItem.Text = "最初に移動";
                // 
                // bindingNavigatorMovePreviousItem
                // 
                this.bindingNavigatorMovePreviousItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.bindingNavigatorMovePreviousItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMovePreviousItem.Image")));
                this.bindingNavigatorMovePreviousItem.Name = "bindingNavigatorMovePreviousItem";
                this.bindingNavigatorMovePreviousItem.RightToLeftAutoMirrorImage = true;
                this.bindingNavigatorMovePreviousItem.Size = new System.Drawing.Size(23, 22);
                this.bindingNavigatorMovePreviousItem.Text = "前に戻る";
                // 
                // bindingNavigatorSeparator
                // 
                this.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator";
                this.bindingNavigatorSeparator.Size = new System.Drawing.Size(6, 25);
                // 
                // bindingNavigatorPositionItem
                // 
                this.bindingNavigatorPositionItem.AccessibleName = "位置";
                this.bindingNavigatorPositionItem.AutoSize = false;
                this.bindingNavigatorPositionItem.Font = new System.Drawing.Font("Yu Gothic UI", 9F);
                this.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem";
                this.bindingNavigatorPositionItem.Size = new System.Drawing.Size(50, 23);
                this.bindingNavigatorPositionItem.Text = "0";
                this.bindingNavigatorPositionItem.ToolTipText = "現在の場所";
                // 
                // bindingNavigatorSeparator1
                // 
                this.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator1";
                this.bindingNavigatorSeparator1.Size = new System.Drawing.Size(6, 25);
                // 
                // bindingNavigatorMoveNextItem
                // 
                this.bindingNavigatorMoveNextItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.bindingNavigatorMoveNextItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveNextItem.Image")));
                this.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem";
                this.bindingNavigatorMoveNextItem.RightToLeftAutoMirrorImage = true;
                this.bindingNavigatorMoveNextItem.Size = new System.Drawing.Size(23, 22);
                this.bindingNavigatorMoveNextItem.Text = "次に移動";
                // 
                // bindingNavigatorMoveLastItem
                // 
                this.bindingNavigatorMoveLastItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.bindingNavigatorMoveLastItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveLastItem.Image")));
                this.bindingNavigatorMoveLastItem.Name = "bindingNavigatorMoveLastItem";
                this.bindingNavigatorMoveLastItem.RightToLeftAutoMirrorImage = true;
                this.bindingNavigatorMoveLastItem.Size = new System.Drawing.Size(23, 22);
                this.bindingNavigatorMoveLastItem.Text = "最後に移動";
                // 
                // bindingNavigatorSeparator2
                // 
                this.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator2";
                this.bindingNavigatorSeparator2.Size = new System.Drawing.Size(6, 25);
                // 
                // productsBindingNavigatorSaveItem
                // 
                this.productsBindingNavigatorSaveItem.DisplayStyle = 
                    System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.productsBindingNavigatorSaveItem.Image = 
                    ((System.Drawing.Image)(resources.GetObject("productsBindingNavigatorSaveItem.Image")));
                this.productsBindingNavigatorSaveItem.Name = "productsBindingNavigatorSaveItem";
                this.productsBindingNavigatorSaveItem.Size = new System.Drawing.Size(23, 22);
                this.productsBindingNavigatorSaveItem.Text = "データの保存";
                this.productsBindingNavigatorSaveItem.Click += ProductsBindingNavigatorSaveItem_Click;
    #nullable enable
                // 
                // productsDataGridView
                // 
                //this.productsDataGridView.AutoGenerateColumns = false;
                this.productsDataGridView.ColumnHeadersHeightSizeMode = 
                    System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.productsDataGridView.DataSource = this.productsBindingSource;
                this.productsDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
                this.productsDataGridView.Location = new System.Drawing.Point(0, 25);
                this.productsDataGridView.Name = "productsDataGridView";
                this.productsDataGridView.RowTemplate.Height = 21;
                //this.productsDataGridView.Size = new System.Drawing.Size(1136, 643);
                this.productsDataGridView.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.Controls.Add(this.productsDataGridView);
                this.Controls.Add(this.productsBindingNavigator);
                this.Name = "Form1";
                this.Text = "Northwind";
                this.Load += Form1_Load;
                ((System.ComponentModel.ISupportInitialize)(this.northwindDataSet)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.productsBindingSource)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.productsBindingNavigator)).EndInit();
                this.productsBindingNavigator.ResumeLayout(false);
                this.productsBindingNavigator.PerformLayout();
                ((System.ComponentModel.ISupportInitialize)(this.productsDataGridView)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
     
            }
     
            private void Form1_Load(object? sender, EventArgs e)
            {
                this.productsTableAdapter.Fill(this.northwindDataSet.Products);
            }
     
            private void ProductsBindingNavigatorSaveItem_Click(object? sender, EventArgs e)
            {
                this.Validate();
                this.productsBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.northwindDataSet);
            }
        }
    }
    

    Result is:

    enter image description here


2 additional answers

Sort by: Most helpful
  1. KOZ6.0 6,480 Reputation points
    2024-06-21T00:10:41.98+00:00

    To connect to an Access Database in VS2022, you need the 64-bit Access OLEDB Provider.

    https://www.microsoft.com/en-us/download/details.aspx?id=54920

    Download accessdatabaseengine_X64.exe and run it with the parameters /passive /quiet.

    Once you have created a VB project, you can get System.Data.OleDb from nuget and connect with the following code.

    Imports System.Data.OleDb
    #Disable Warning CA1416
    
    Module Program
        Sub Main(args As String())
            Dim builder As New OleDbConnectionStringBuilder With {
                .Provider = "Microsoft.ACE.OLEDB.16.0",
                .DataSource = "your database path"
            }
            Dim con As New OleDbConnection(builder.ConnectionString)
            con.Open()
        End Sub
    End Module
    

  2. abdiMahamad-9363 0 Reputation points
    2024-06-25T20:29:42.03+00:00

    Abdi Mohamed abdimahamad255@gmail.com


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.