Je travaille actuellement sur une nouvelle application avec un transfert de données de base de données entre deux bases de données. J'ai actuellement une méthode écrite, mais il semble que je puisse rendre ce code beaucoup plus compact. J'ai lu un article qui utilisait l'instruction Using
, mais je ne suis pas sûr comment je peux l'implémenter dans mon code. En outre, je tiens à préciser que j'ai les déclarations publiques suivantes :
private IDbConnection ExportConnection = new OleDbConnection(Helper.CnnVal("ExportDatabase"));
private IDbConnection SchoolConnection;
private DataTable exportDb = new DataTable();
private DataTable schoolDb = new DataTable();
private OleDbCommandBuilder cb;
private OleDbDataAdapter dataAdapterSchool;
private OleDbDataAdapter dataAdapterExport;
private OleDbCommand dbCommand;
private string ZoekQuery { get; set; }
private DataAccess db = new DataAccess();
private string SchoolConnectionString;
private string selectSchoolQuery;
Voici la méthode :
public void InlezenClientGegevens()
{
if ((bool)Checbox.IsChecked)
{
SchoolConnectionString = $@"I removed the string for privacy reasons";
try
{
ExportConnection.Open();
ZoekQuery = $"SELECT * FROM Client WHERE Cliëntnummer BETWEEN '{TxtVan.Text}' AND '{TxtTm.Text}'";
dbCommand = new OleDbCommand(ZoekQuery, (OleDbConnection)ExportConnection);
dataAdapterExport = new OleDbDataAdapter(ZoekQuery, (OleDbConnection)ExportConnection);
dbCommand.ExecuteNonQuery();
dataAdapterExport.AcceptChangesDuringFill = false;
dataAdapterExport.Fill(exportDb);
ExportConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SchoolConnection = new OleDbConnection(SchoolConnectionString);
try
{
SchoolConnection.Open();
selectSchoolQuery = "SELECT * FROM Info";
dbCommand = new OleDbCommand(selectSchoolQuery, (OleDbConnection)SchoolConnection);
dataAdapterSchool = new OleDbDataAdapter(selectSchoolQuery, (OleDbConnection)SchoolConnection);
dbCommand.ExecuteNonQuery();
dataAdapterSchool.FillSchema(schoolDb, SchemaType.Source);
dataAdapterSchool.Fill(schoolDb);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
cb = new OleDbCommandBuilder(dataAdapterSchool);
cb.GetUpdateCommand();
schoolDb.Merge(exportDb, false, MissingSchemaAction.Add);
dataAdapterSchool.Update(schoolDb);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
ÉDIT :
public void InlezenClientGegevens()
{
if ((bool)Checkbox.IsChecked)
{
try
{
using (var connection = new OleDbConnection(Helper.CnnVal("ExportDatabase")))
{
var ZoekQuery = $"SELECT * FROM client WHERE Cliëntnummer BETWEEN '{TxtVan.Text}' AND '{TxtTm.Text}'";
using (var dbcommand = new OleDbCommand(ZoekQuery, connection))
{
using (var exportAdapter = new OleDbDataAdapter(ZoekQuery, connection))
{
exportAdapter.AcceptChangesDuringFill = false;
exportAdapter.Fill(exportDb);
}
}
}
using (var connection = new OleDbConnection($"))
{
var ZoekQuery = "SELECT * FROM info";
using (var dbcommand = new OleDbCommand(ZoekQuery, connection))
{
using (var SchoolAdapter = new OleDbDataAdapter(ZoekQuery, connection))
{
SchoolAdapter.FillSchema(schoolDb, SchemaType.Source);
SchoolAdapter.Fill(schoolDb);
using (var cb = new OleDbCommandBuilder(SchoolAdapter))
{
cb.GetUpdateCommand();
schoolDb.Merge(exportDb, false, MissingSchemaAction.Add);
SchoolAdapter.Update(schoolDb);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}