gpt4 book ai didi

c# - 我怎样才能让我的数据库方法更适合 future ?

转载 作者:行者123 更新时间:2023-12-04 15:02:22 24 4
gpt4 key购买 nike

<分区>

我目前正在开发一个在两个数据库之间进行数据库数据传输的新应用程序。我目前已经编写了一个方法,但看起来我可以让这段代码更紧凑。我阅读了一篇使用 Using 语句的文章,但我不确定如何在我的代码中实现它。此外,我想声明我有以下公开声明:

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;

这是方法:

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);
}


}
}

编辑:

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);
}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com