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

本文关键字:数据库 刷新 更新 数据 | 更新日期: 2023-09-27 18:11:04

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

下面是我添加数据的方法:
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!");
                }
}

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

我通过改变数据库的路径解决了这个问题,这是错误的,并通过为我的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());
                }
            }
        }