我正在使用 Visual Studio 2022 以及 C# 和 WPF 应用程序来创建桌面应用程序。 SQLite 是数据库。下面是代码。
数据库表代码
CREATE TABLE "Disclaimer" (
"ID" INTEGER,
"DisName" TEXT,
"DisDateTime" TEXT,
PRIMARY KEY("ID" AUTOINCREMENT)
);
应用程序.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# 代码中更改表的名称,则会出现“找不到表”错误。这意味着路径和连接是正确的。但是运行代码后,如果我使用数据库浏览器检查表,它仍然是空的。
hide()
和显示窗口工作正常。该代码要么跳过数据库部分,要么更新表但不保存它。我尝试为 connection.open()
和 command.ExecuteNonQuery()
添加错误处理程序,但它没有捕获错误。程序运行顺利,只是数据库表中没有数据。
代码好像有问题。这是实际有效的代码。
private void Button1_Click(object sender, RoutedEventArgs e)
{ 尝试 { // 获取系统名称 字符串系统名称=环境.机器名称; // 获取当前日期和时间 日期时间 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);
}
}