I have made a simple console app to execute sql queries to an access database. I'm also just learning how to use classes ect so I decided to try and make the program as the function of a class. Here it is.
using System.Data.OleDb;
namespace table
{
class Program
{
static public string query;
static public string path;
static public table table1 = new table();
static void Main(string[] args)
{
Console.WriteLine("Enter databse file location: ");
path = Console.ReadLine();
Console.WriteLine("Enter query: ");
query = Console.ReadLine();
table1.SqlRead (query, path);
for (int i = 0; i < table1.data.Count; i += 1)
{
for (int j = 0; j< table1.data[i].Length; j += 1)
{
Console .WriteLine (table1.data[i][j]);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
class table
{
List<string> tempDataList = new List<string>();
public List<string[]> data = new List<string[]>(); // both dimensions zero-based
public void SqlRead(string q, string p)
{
string connectionSting = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + p + "; User Id=admin;Password=;";
using (OleDbConnection connection = new OleDbConnection(connectionSting))
{
OleDbCommand command = new OleDbCommand(q, connection);
connection.Open();
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i += 1)
{
tempDataList.Add(reader[i].ToString());
}
string[] tempDataArray = tempDataList.ToArray();
tempDataList.Clear();
data.Add(tempDataArray);
}
}
}
}
}
}
This console program works fine. Might not be the best solution but it does work. The next step I wanted to do was to get a form with some kind of UI. I have started with a button, a input box for the query, and I copied the class into the form code. When I tried to use table1 I was given 'Inconsistent accessibility'. I worked around that by making the table class public. The program runs, but the OleDb connection does not work. I had some trouble with the Provider in the connection string in the console version but I fixed that by downloading the access database engine and changing the provider to Microsoft.ACE.OleDB.12.0 . Made sure that was the same in the forms. When the form runs, I can tell using breakpoints that it reaches the line connection.Open(), at which point debugging just stops. the form closes, everything ends with no explanation. I have done try catch on the line and no exception is thrown.
Its such a strange problem to me and i can't find anyone else with the same. The only differences I can think of between the two programs is that for the console I had stored my test database in the solution folder. And for the forms I was using the same one. So I made a copy of the database in the form solution folder- no change. The one other difference I notice is that the solution explorer for the console program lists Dependencies, Properties, Program.cs, database.accdb ; but the forms version lists Dependencies, database.accdb, Properties, Program.cs. Don't think that means anything but I have no idea what is going wrong.
Both programs in Vs2022. Targeting .net 7.0.
Thanks for any help.