XAML 无法使用C#、WPF将数据保存到SQLite数据库

pexxcrt2  于 11个月前  发布在  C#
关注(0)|答案(1)|浏览(132)

我使用Visual Studio 2022,用C#和WPF应用程序来创建一个桌面应用程序。SQLite是数据库。下面是代码。
DB表代码

CREATE TABLE "Disclaimer" (
    "ID"    INTEGER,
    "DisName"   TEXT,
    "DisDateTime"   TEXT,
    PRIMARY KEY("ID" AUTOINCREMENT)
);

字符串
App.xaml

<Application x:Class="Sanganak.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             StartupUri="Splash.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <system:String x:Key="SQLiteConnection">Data Source=|DataDirectory|\Sanganak.db;Version=3;</system:String>
        </ResourceDictionary>
    </Application.Resources>
</Application>


C#代码背后

private void Button1_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         // Get the system name
         string systemName = Environment.MachineName;
         // Get the current date and time
         DateTime currentDateAndTime = DateTime.Now;

         // Save data to Sanganak database
         string connectionString = (string)Application.Current.Resources["SQLiteConnection"];
         using (SQLiteConnection connection = new SQLiteConnection(connectionString))
         {
             connection.Open();
             string sql = "INSERT INTO Disclaimer (DisName, DisDateTime) VALUES (@DisName, @DisDateTime)";
             using (SQLiteCommand command = new SQLiteCommand(sql, connection))
             {
                 command.Parameters.AddWithValue("@DisName", systemName);
                 command.Parameters.AddWithValue("@DisDateTime", currentDateAndTime.ToString("yyyy-MM-dd HH:mm:ss"));
                 command.ExecuteNonQuery();
             }
             connection.Close();
         }
         Hide();
         var setupWindow = new SetUp();
         setupWindow.Show();
     }
     catch (Exception ex)
     {
         // Log the exception (replace with your actual logging mechanism)
         Console.WriteLine("Exception: " + ex.Message);
     }
 }


如果我在C#代码后面更改表的名称,那么我会得到'table not found'错误。这意味着路径和连接是正确的。但是在运行代码之后,如果我使用DB Browser检查表,它仍然是空的。hide()和显示窗口工作正常。代码要么跳过数据库部分,要么更新表,但不保存它。我已经尝试添加错误处理程序,connection.open() & command.ExecuteNonQuery()但它没有捕获错误。程序运行顺利,只是数据库表中没有数据。

bybem2ql

bybem2ql1#

看起来代码中有一个问题。这是实际工作的代码。

private void Button1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        // Get the system name
        string systemName = Environment.MachineName;
        // Get the current date and time
        DateTime currentDateAndTime = DateTime.Now;

        // Save data to Sanganak database
        string connectionString = (string)Application.Current.Resources["SQLiteConnection"];
        using (SQLiteConnection connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            if (connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Failed to open the SQLite connection.");
            }
            // Now that the connection is open, proceed with the database operation
            string sql = "INSERT INTO Disclaimer (DisName, DisDateTime) VALUES (@DisName, @DisDateTime)";
            using (SQLiteCommand command = new SQLiteCommand(sql, connection))
            {
                command.Parameters.AddWithValue("@DisName", systemName);
                command.Parameters.AddWithValue("@DisDateTime", currentDateAndTime.ToString("yyyy-MM-dd HH:mm:ss"));
                command.ExecuteNonQuery();
            }
            // No need to close the connection explicitly, as 'using' block takes care of it
        }

        Hide();
        var setupWindow = new SetUp();
        setupWindow.Show();
    }
    catch (Exception ex)
    {
        // Log the exception (replace with your actual logging mechanism)
        Console.WriteLine("Exception: " + ex.Message);
    }
}

字符串

相关问题