Hi JohnCTX-6479,
First, you can add a new class in your project via following steps(as a template):
Right-click your project name->Add->Class->Change class name to DataOperations.cs
Then you need put the template code into DataOperations class.
In this step, I suggest you construct a function with parameters and pass in "ConnectionString" as a parameter.
Due to the Read method is static, so you can call it via class name directly.
class DataOperations
{
public static string ConnectionString = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "[DatabaseFileName].mdb")};";
public static List<string> Read(string s)
{
var list = new List<string>();
using (var cn = new OleDbConnection() { ConnectionString = s })
{
using (var cmd = new OleDbCommand() { Connection = cn })
{
cmd.CommandText = "SELECT [FieldOne] FROM tblOne;";
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(reader.GetString(0));
}
cn.Close();
}
}
return list;
}
}
public class DataOpsTwo
{
public void test()
{
List <string> list= DataOperations.Read(DataOperations.ConnectionString);//you can also pass another connectionString
}
}
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.