简体   繁体   English

应用程序停止后数据库不会保留数据 Visual Studio C#

[英]Database will not keep data after app stopped Visual Studio C#

Hope someone can help with this.希望有人可以帮助解决这个问题。 The following code is meant to add an entry to a Table;以下代码旨在向表中添加条目; however, whilst the data is added at runtime, when the app is closed it disappears.然而,虽然数据是在运行时添加的,但当应用程序关闭时,它就会消失。 The Database is set to 'Copy if Newer'.数据库设置为“如果较新则复制”。

   private void AddToNameList()
        {
            SqlConnection con = new SqlConnection(connectionString);

            cmd = new SqlCommand("INSERT INTO NameSurname VALUES (@NameId, @Surname)", con);
     
            cmd.Parameters.AddWithValue("@NameId", textBox2.Text);

            cmd.Parameters.AddWithValue("@Surname", textBox3.Text);
            con.Open();
            int i= cmd.ExecuteNonQuery();
            con.Close();
            if (i != 0)
            {
                MessageBox.Show(i + "Data Saved");
            }
            textBox2.Clear();
            textBox3.Clear();

        }

         private void button1_Click(object sender, EventArgs e)
        {
            if(textBox2.Text!= ("") && textBox3.Text!=(""))
            {
                AddToNameList();
            }
            else
            {
                MessageBox.Show("Please enter Name and Surname","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

The message box confirms that the data is saved, and when I carry out a search it is there.消息框确认数据已保存,当我进行搜索时,它就在那里。 However, when I stop the app running and check the database, the data is not there.但是,当我停止应用程序运行并检查数据库时,数据不存在。

Grateful for any help.感谢任何帮助。

Thanks谢谢

If you want to read a.mdf file, you must install SQL Server Express or SqlLocalDb runtime .如果要读取 .mdf 文件,必须安装SQL Server ExpressSqlLocalDb runtime Not all the other users are willing to download and install them.并非所有其他用户都愿意下载和安装它们。

To save the table to a database, I think SQLite will be a better choice.要将表保存到数据库中,我认为 SQLite 会是更好的选择。

Hope the following steps can help you.希望以下步骤可以帮助到您。

First, install System.Data.SQLite using NuGet.首先,使用 NuGet 安装System.Data.SQLite

Second, configure the connection string in App.config.二、在App.config中配置连接字符串。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
  <connectionStrings>
     <add name="SQLiteDbContext" connectionString="Data Source=MyDatabase.sqlite" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>
</configuration>

Third, you can use the following code to create/insert/read the database/datatable.第三,您可以使用以下代码来创建/插入/读取数据库/数据表。

// create
SQLiteConnection.CreateFile("MyDatabase.sqlite");
SQLiteConnection con = new SQLiteConnection("Data Source=MyDatabase.sqlite");
con.Open();
string sql = "create table NameSurname (Nameid varchar(20), Surname varchar(20))";
SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();
// insert
SQLiteCommand cmd = new SQLiteCommand("INSERT INTO NameSurname VALUES (@NameId, @Surname)", con);
cmd.Parameters.AddWithValue("@NameId", textBox2.Text);
cmd.Parameters.AddWithValue("@Surname", textBox3.Text);
cmd.ExecuteNonQuery();
// read
SQLiteCommand sqlCom = new SQLiteCommand("Select * From NameSurname", con);
SQLiteDataReader sqlDataReader = sqlCom.ExecuteReader();
int i = 1;
while (sqlDataReader.Read())
{
    listBox1.Items.Add(i);
    listBox1.Items.Add(sqlDataReader.GetValue(0));
    listBox1.Items.Add(sqlDataReader.GetValue(1));
    i++;
}
con.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM