I'm writting an application in C# that connects to a database that is used by other application. I'm coding the class that access the database like this:
class conexionBD
{
string connString;
protected void miConexion(string ruta)
{
connString = String.Concat("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=", ruta);
}
protected DataTable misEmpleados()
{
string query = "SELECT Fiel1, Field2 FROM Table1";
DataTable dTable = miDatatable(query);
return dTable;
}
protected DataColumn misDptos()
{
DataTable dTable = miDatatable("SELECT OtherField from OtherTable");
return dTable.Columns[0];
}
private DataTable miDatatable(string sqlQuery)
{
OleDbDataAdapter dAdapter = new OleDbDataAdapter(sqlQuery, connString);
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
return dTable;
}
}
The app firts calls the method "miConexion" to set the path to the database in the hard disc. Then the app connects and disconnects each time I want to get the data stored in "Table1" and "OtherTable".
The database is likely to be accessed and modified by both apps at the same time. "Connecting and disconnecting" is the best way to access the database in this case?