You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已发布的.NET MAUI应用连接Access数据库崩溃,求日志排查方案

.NET MAUI发布版连接Access数据库崩溃问题

开发了一个连接本地Access数据库的.NET MAUI应用(请勿询问为何雇主使用Access),该应用在Visual Studio的Debug和Release模式下运行正常,但发布后会崩溃。目前不清楚如何为应用创建日志文件来排查崩溃原因。

通过在代码各阶段添加Label文本输出调试,发现崩溃发生在尝试连接数据库的步骤。使用OleDbConnection,连接字符串为:

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\access\accessdb.accdb"

应用会根据Entry控件传入的主键值加载数据表:当输入字符串时不会崩溃(因为主键要求为整数,已有判断逻辑),但输入整数时触发数据库连接就会崩溃。

XAML代码

<Grid ColumnDefinitions="*,*,*,*" RowDefinitions="Auto,Auto,*">
    <Label x:Name="MainLbl" Text="" FontSize="Header"
           VerticalOptions="Center" HorizontalOptions="Center" 
           Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2"/>
    <Label Grid.Column="1" Grid.Row="1" Text="Add a ID"/>
    <Entry x:Name="StudentIdTxt" Placeholder="Add here" Grid.Column="2" Grid.Row="1" VerticalOptions="Start" TextChanged="StudentIdTxt_TextChanged"/>
    <Grid x:Name="StudentLst" IsVisible="False" ColumnDefinitions="*,*" RowDefinitions="*,*,*,*,*" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2">
        <Label Grid.Column="0" Grid.Row="0" Text="Name"/>
        <Label Grid.Column="1" Grid.Row="0" Text="{Binding StudentName}"/>
    </Grid>
</Grid>

C#代码(通过Label文本调试崩溃位置,添加async和await是为了定位具体崩溃行)

DataTable localDT = new();

public StudentSearch()
{
    InitializeComponent();
    MainLbl.Text = "0";
}

private async void StudentIdTxt_TextChanged(object sender, TextChangedEventArgs e)
{
    await Task.Delay(100);
    MainLbl.Text = "1";
    await Task.Delay(100);
    ShowData();
}

private async void ShowData()
{
    MainLbl.Text = "2";
    await Task.Delay(100);
    if (int.TryParse(StudentIdTxt.Text, out int id))
    {
        MainLbl.Text = "3";
        await Task.Delay(100);
// This is where it crashes in published version and it shuts down the program.
// In debug and in release when running from visual studio it continues and shows the data.
        localDT = Load(id);
        MainLbl.Text = "4";
        await Task.Delay(100);
        if (localDT != null && localDT.Rows.Count > 0)
        {
            MainLbl.Text = "5";
            await Task.Delay(100);
            StudentLst.IsVisible = true;
        }
        else
        {
            StudentLst.IsVisible = false;
        }
    }
    else
    {
        StudentLst.IsVisible = false;
    }
}

private DataTable Load(int primarykeyvalue)
{
    DataTable dt;
    OleDbConnection con = new();
    con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\access\accessdb.accdb";
    con.Open();
    OleDbCommand cmd = con.CreateCommand();

    cmd.CommandText = $"select * from StudentProgramRewards where StudentId = {primarykeyvalue}";
    dt = GetDataTable(cmd);
    return dt;
}

private DataTable GetDataTable(OleDbCommand cmd)
{
    DataTable dt = new();
    OleDbDataAdapter adapter = new(cmd);
    adapter.Fill(dt);
    return dt;
}

注:还有更多代码,忽略Entry中的绑定,这不是问题所在。

内容的提问来源于stack exchange,提问作者Shmiel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:54:56