从 C# 更新数据后本地数据库不刷新

Local database not refreshing after updating data from C#

我正在使用本地数据库,整个应用程序运行良好。问题是我正面临一些烦人的事情。如果我更新或向我的本地数据库添加新数据,我必须关闭整个应用程序并重新启动它,这样我才能看到我输入的新数据。为什么不刷新,如何解决?

这是我添加数据的方式:

private void button1_Click(object sender, EventArgs e)
{
            if (radioButton1.Checked)
            {
                label5.Text = "1";
            }
            else
            {
                label5.Text = "0";
            }

            if (radioButton2.Checked)
            {
                label5.Text = "2";
            }

            if (textBox1.Text.Length == 0)
            {
                textBox1.Text = "Fara";
            }

            if (textBox2.Text.Length == 0)
            {
                textBox2.Text = "0";
            }

            if (textBox3.Text.Length == 0)
            {
                textBox3.Text = "0";
            }

            if (numeTextBox.TextLength != 0 && prenumeTextBox.TextLength != 0)
            {
                var connString = (@"Data Source=" + Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\Angajati.sdf");

                //var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";

                using (var conn = new SqlCeConnection(connString))
                {
                    try
                    {
                        //deschide conectiunea in db
                        conn.Open();
                        //creaza comanda in SQL Server CE
                        SqlCeCommand cmd = new SqlCeCommand();
                        //conecteaza cmd la conn
                        cmd.Connection = conn;
                        //adauga parametru pt campul poza cu value image
                        SqlCeParameter picture = new SqlCeParameter("@Poza", SqlDbType.Image);
                        MemoryStream ms = new MemoryStream();
                        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                        byte[] a = ms.GetBuffer();
                        ms.Close();
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@Poza", a);
                        cmd.CommandText = "INSERT INTO info(Nume, Prenume, Data, Proiect, Schimburi, Poza, Acord, Baza) VALUES('" + numeTextBox.Text.Trim() + "', '" + prenumeTextBox.Text.Trim() + "', '" + dateTimePicker1.Value.ToShortDateString() + "', '" + textBox1.Text.Trim() + "', " + label5.Text + " , @Poza, " + textBox2.Text + ", " + textBox3.Text + ");";
                        cmd.ExecuteNonQuery();

                        conn.Close();
                        MessageBox.Show("Salvat cu succes!");
                        textBox1.Clear();
                        textBox2.Clear();
                        textBox3.Clear();
                        numeTextBox.Clear();
                        prenumeTextBox.Clear();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            else
            {
                MessageBox.Show("Trebuie sa completezi campurile inainte de a salva!");
            }
}

以下是我的更新方式:

private void button1_Click_1(object sender, EventArgs e)
{
            //var connString = (@"Data Source= |DataDirectory|\Angajati.sdf");
            var connString = (@"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\Angajati.sdf");

            using (var conn = new SqlCeConnection(connString))
            {
                try
                {
                    conn.Open();
                    SqlCeCommand cmd = new SqlCeCommand();
                    //conecteaza cmd la conn
                    cmd.Connection = conn;
                    //adauga parametru pt campul poza cu value image
                    SqlCeParameter picture = new SqlCeParameter("@Poza", SqlDbType.Image);
                    MemoryStream ms = new MemoryStream();
                    pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                    byte[] a = ms.GetBuffer();
                    ms.Close();
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@Poza", a);
                    var query = "UPDATE info SET Nume='" + textBox5.Text + "' , Prenume='" + textBox4.Text + "' , Data='" + dateTimePicker1.Value.ToShortDateString() + "', Proiect='" + textBox1.Text + "', Schimburi='" + label10.Text + "', Poza=@Poza, Acord='" + textBox2.Text + "', Baza='" + textBox3.Text + "'  WHERE Nume='" + textBox5.Text + "' AND Prenume='" + textBox4.Text + "'";
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Salvat cu succes!");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            } 
} 

以下是我搜索数据的方式:

private void button1_Click(object sender, EventArgs e)
{
            if (textBox1.Text.Length != 0)
            {

                var numePrenume = textBox1.Text.Trim().Split(' ');


                    if (numePrenume.Count() > 1)
                    {
                        var nume = numePrenume[0];
                        var prenume = numePrenume[1];
                        var connString = (@"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\Angajati.sdf");

                        using (var conn = new SqlCeConnection(connString))
                        {
                            try
                            {
                                conn.Open();
                                var query = "SELECT COUNT(*) FROM info WHERE Nume='" + nume + "' AND Prenume='" + prenume + "'";
                                var command = new SqlCeCommand(query, conn);
                                var dataAdapter = new SqlCeDataAdapter(command);
                                var dataTable = new DataTable();
                                dataAdapter.Fill(dataTable);

                                //checks if there's the searched record is in the db.
                                int infoCount = (int)command.ExecuteScalar();

                                if (infoCount > 0)
                                {
                                    Info form = new Info(nume, prenume);
                                    form.Show();
                                }
                                else
                                {
                                    MessageBox.Show("Nu exista un angajat cu acest nume");
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Nu ai introdus prenumele");
                    }
                }
                else
                {
                    MessageBox.Show("Nu ai introdus nici un nume!");
                }
}

我已经通过更改数据库的路径解决了这个问题,这是错误的,并且通过为我的 gridView 创建一个在插入后调用的刷新函数。下面是解决方案的代码:

现在得到这样的路径:

string startPath = Application.StartupPath;
var filepath = startPath + "\" + "Grupe.sdf";
var connString = (@"Data Source=" + filepath +"");

刷新函数:

public void refresh()
        {
            var connString = (@"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\Grupe.sdf");
            using (var conn = new SqlCeConnection(connString))
            {
                try
                {
                    conn.Open();
                    var query = "SELECT * FROM copii";
                    var command = new SqlCeCommand(query, conn);
                    var dataAdapter = new SqlCeDataAdapter(command);
                    var dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);

                    dataGridView1.DataSource = dataTable;

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

            }
        }