I am using visual studio Windows forms for a login/sign-up project. So how would I go about connecting my Microsoft access data base to my visual studio project and establishing a connection in the code so I can write out a command.
Asked
Active
Viewed 468 times
0
-
1You don't need to connect database files to the IDE - just write database code using ADO.NET and specify the path to the Access DB file in your `OleDbConnection`'s connection-string. ...though you _can_ connect to it using the "Server Explorer" pane if you want to use the DB as a reference or to inspect it, it isn't necessary for software-dev at all. – Dai Nov 21 '22 at 20:26
1 Answers
1
It uses a connection string similar to SQL.
public string ConnString => $"Provider=Microsoft.ACE.OLEDB.16.0;Data Source = {FilePathHere};"
To persist to the file using ADO.NET, it will look like
private void ExecuteWrite(string sql)
{
try
{
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
OleDbCommand cmd = new OleDbCommand(sql, conn) { CommandType = CommandType.Text };
conn.Open();
_ = cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
Console.WriteLine(e.ToString());
}
}
A couple notes on Access:
- Access has its own form of SQL which is very different than what you are used to, for example instead of
char
varchar
you will have types liketext
. Check the full list of differences here - the SQL string you write must use
"
to escape things such as problematic column names - Tables cannot exceed 255 columns for some reason. Be ready to create seperate queries to split any massive table that goes over that limit, as Access will straight up refuse to process that query. Its the only db I know that suffers from such a limitation and it was a pain for a project I worked on. I got around it using some LINQ to split up the desired columns, and then crafting the separate CREATE or INSERT queries

Narish
- 607
- 4
- 18
-
_"the SQL string you write must use " to escape things such as problematic column names"_ - I thought Access used `[]` like SQL Server (and Access doesn't have an ANSI/ISO mode switch, afaik?) – Dai Nov 21 '22 at 20:44
-
1_"Its the only db I know that suffers from such a limitation"_ - [SQL Server](https://learn.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server?view=sql-server-ver16) and [Postgres](https://stackoverflow.com/questions/12606842/what-is-the-maximum-number-of-columns-in-a-postgresql-select-query) both have page-based limits (which means you can't exceed ~1,000 columns per table, at least without using `SPARSE` columns). – Dai Nov 21 '22 at 20:45
-
Thanks but would I need to add any libraries at the top like "using system.text" or something. And where would I find the file path for the data base. – Abdi L Nov 21 '22 at 21:03
-
1It depends on which edition of .NET you're targeting. If you're targeting .NET Framework then you don't need to do anything, but if you're targeting .NET Core or .NET 6+ then you will need https://www.nuget.org/packages/System.Data.OleDb/ – Dai Nov 21 '22 at 21:04
-
Oh ok but where would I find the file path for the database? Sorry for being dumb just very new to this. – Abdi L Nov 21 '22 at 21:24
-
1@AbdiL You said you already have an Access database file (`.mdb` or `.accdb`), so use the full path to that file. – Dai Nov 21 '22 at 22:14
-
@AbdiL yes as in the literal filepath on disk to the Access File. @Dai I believe both `""` and `[]` are acceptable, in my experience I've used `""` without any issue – Narish Nov 22 '22 at 15:21
-
Also @AbdiL this is on a tangent but worth mentioning, if this is a project for new development I would highly recommend using WPF or MAUI or Blazor. WinForms is *only* maintained because of the sheer volume of legacy apps that are still in use throughout the corporate world. Moreover its more of a headache to deal with as you do not get things like responsive windows out-of-the-box. Any new application being developed is very likely to be WPF, and MAUI/Blazor are the new kids on the block so they will grow in popularity – Narish Nov 22 '22 at 15:23