SQLite数据库会在应用程序启动时删除数据,但不会在应用程序关闭时删除数据?

inkz8wg9  于 2023-03-19  发布在  SQLite
关注(0)|答案(1)|浏览(178)

我正在使用SQLite构建一个带有Insert函数的函数应用程序。如果我使用“func start”启动应用程序,插入一些数据,然后我通过ctrl + c关闭应用程序,数据就在那里,我可以在VSCode中查询它。但是当我 * 再次 * 启动应用程序时,所有数据都被擦除。你知道是什么原因导致的吗?我的理论是.db文件每次都被重新创建,所以它才会删除数据。
天气上下文. cs文件:

public partial class WeatherContext : DbContext
{
    public WeatherContext()
    {
        Database.EnsureCreated();
    }
    public WeatherContext(DbContextOptions<WeatherContext> options)
        : base(options)
    {
        
    }

    public DbSet<WeatherDays> WeatherDays{ get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connString = new SQLiteConnection(@"Data Source=weather.db;PRAGMA journal_mode=WAL;");
        var conn = new SQLiteConnection(connString);
        optionsBuilder.UseSqlite(conn);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<WeatherDays>(entity =>
        {
            entity.ToTable("WeatherDays");
            entity.Property(e => e.Id)
                .HasColumnName("id")
                .ValueGeneratedOnAdd();
            entity.Property(e => e.date)
                .HasColumnName("date")
                .HasMaxLength(255);
            entity.Property(e => e.temperature)
                .HasColumnName("temperature")
                .HasMaxLength(255);
            entity.Property(e => e.humidity)
                .HasColumnName("humidity")
                .HasMaxLength(255);
        }
        );

        OnModelCreatingPartial(modelBuilder);
    }
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

}

Startup.cs

[assembly: FunctionsStartup(typeof(WeatherApp.Startup))]
namespace WeatherApp
{
    public class Startup : FunctionsStartup
    {
        {
            builder.Services.AddSingleton<WeatherServices>();
            var connString = new SQLiteConnection(@"Data Source=weather.db;PRAGMA journal_mode=WAL;");
            var conn = new SQLiteConnection(connString);
            builder.Services.AddDbContext<WeatherContext>(options =>
              options.UseSqlite(conn)
            );

        }

    }
}

我试过:

  • 将weather.db的输出属性设置为在更新时始终复制
  • 在连接字符串中设置;PRAGMA journal_mode=WAL;
j8ag8udp

j8ag8udp1#

我的想法是,当我第一次加载项目时,加载的第一个窗体是frmStart,它检查DB是否存在下面的代码

private void frmStart_Load(object sender, System.EventArgs e)
    {
        if (!File.Exists(dbName))
        {
            btnCreateDB.Visible = true;
            btnAdd.Visible = false;
            Width = 205;
            Height = 115;
            Refresh();
        }
        else
        {
            Width = 460;
            Height = 320;
            Refresh();
            btnCreateDB.Visible = false;
            btnAdd.Visible = true;
            tbMessage.Text = "Select Function to Manage Peoples Information";
        }
    }

btnCreate调用frmMakeDB,该frmMakeDB创建下面的DB和表代码

public partial class frmMakeDB : Form
{
    public string dbName = "APeople.db";
    public frmMakeDB()
    {
        InitializeComponent();
    }

    private void frmMakeDB_Load(object sender, System.EventArgs e)
    {
        makeDB();
        makeFriendsTable();
        tbMessage.Text = "Database Created";
    }
    public void makeDB()
    {
        using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
            if (!File.Exists(dbName))
            {
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    }

    public void makeFriendsTable()
    {
        // create table FriendsData String for cmd
        string create_table = @"CREATE TABLE IF NOT EXISTS FriendsData(
                    FID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                    fxFirstName TEXT,
                    fxLastName TEXT,
                    fxInfo TEXT)";

        string dbTable = "FriendsData";
        using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
            if (!File.Exists(dbTable))
            {
                try
                {
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(create_table, conn);
                    cmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    }

    private void btnReturn_Click(object sender, EventArgs e)
    {
        Close();
        frmStart fST = new frmStart();
        fST.Show();
    }
}

现在,您知道DB和表已经创建完毕,可以执行其他CRUD函数了

相关问题