应用程序自动启动时,c#数据库连接无法打开

时间:2017-09-16 18:38:12

标签: c# sqlite

我正在尝试此网站上的闹钟代码 http://foxlearn.com/article/how-to-make-an-alarm-clock-with-sound-in-csharp-249.html

我使用SQLite数据库来存储时间值,以便当警报应用程序关闭然后打开时,它会保留触发警报的时间值。这是使应用程序自动启动Windows的准备工作。

我总是将连接字符串存储在名为SessionInfo的类中,如此

namespace AlarmClock.DataAccessLayer
{
    class SessionInfo
    {
        public static SQLiteConnection sqlconnection = new SQLiteConnection();
        public static string connectionstring = @"Data Source=AlarmClockDB.db;Version=3;";
    }
}

然后我在这样的program.cs中打开连接 - 即与整个项目建立一个连接:

namespace AlarmClock
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DataAccessLayer.SessionInfo.sqlconnection.ConnectionString DataAccessLayer.SessionInfo.connectionstring;         

            DataAccessLayer.SessionInfo.sqlconnection.Open();         
            Application.Run(new Form1());
        }
    }
}

在警报代码块中,我创建了注册表项,如该段代码所示

namespace AlarmClock
{
    public partial class Form1: Form
    {
        System.Timers.Timer timer; 
        BusinessLayer.CLASS_ALARM alarm = new BusinessLayer.CLASS_ALARM();
        SoundPlayer player = new SoundPlayer();
        /*-->*/RegistryKey reg =Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

        public Form1()
        {
            /*-->*/reg.SetValue("AlarmClock", "\"" + Application.ExecutablePath.ToString() + "\"");
            InitializeComponent();           

            try
            {
                dateTimePicker1.Value = DateTime.Parse(alarm.AlarmGetDate());
            }
            catch (Exception)
            { }
        }

        //rest of code...
    }
}

现在警报工作正常,并且注册表项已创建且一切正常,但是当我重新启动计算机时,应用程序刚刚启动,它只会引发错误:

  

申请停止工作

它甚至在我看到它之前很快消失了!

我检查了事件日志,它显示的信息显示问题出在Program.cs中打开连接线:

DataAccessLayer.SessionInfo.sqlconnection.Open();

我不知道问题是什么......我一直在我的应用程序中使用这种连接方法,并且在打开连接时从未出现过错误。

包含源标题(.NET运行时)的事件日志消息:

    Application: AlarmClock.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.Data.SQLite.SQLiteException
    at System.Data.SQLite.SQLite3.Open(System.String, System.String, 
    System.Data.SQLite.SQLiteConnectionFlags, 
    System.Data.SQLite.SQLiteOpenFlagsEnum, Int32, Boolean)
    at System.Data.SQLite.SQLiteConnection.Open()
    at AlarmClock.Program.Main()

第二个带有源标题(应用程序错误)

    Faulting application name: AlarmClock.exe, version: 1.0.0.0, time stamp: 
    0x59bd4e44
    Faulting module name: KERNELBASE.dll, version: 6.3.9600.18666, time 
    stamp: 0x58f33794
    Exception code: 0xe0434352
    Fault offset: 0x00000000000095fc
    Faulting process id: 0xfd0
    Faulting application start time: 0x01d32f11151a66e5
    Faulting application path: C:\Users\dabbour\Documents\Visual Studio 
    2012\Projects\AlarmClock\AlarmClock\bin\Debug\AlarmClock.exe
    Faulting module path: C:\Windows\system32\KERNELBASE.dll
    Report Id: 53db87a5-9b04-11e7-82b4-8c89a5fd578a
    Faulting package full name: 
    Faulting package-relative application ID: 

我尽力解释情况,如果需要更多信息,请告诉我。

0 个答案:

没有答案