简体   繁体   中英

Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile

I am developing a Windows Mobile 6.5 application with C# using Visual Studio 2008, it's connected to a SQL Server CE database.

I used this code to insert rows, the value of each column is related to a textBox string :

namespace GesTPL
{
    public partial class Form4 : Form
    {
        public Form4()
        {
           InitializeComponent();
        }

        public Form RefToForm1 { get; set; }

        static String pathDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        static String pathDB = System.IO.Path.Combine(pathDir, "releve.sdf");
        static String connectionString = string.Format(@"DataSource={0}", pathDB);
        SqlCeConnection connection = new SqlCeConnection(connectionString);

        private void Form4_Load(object sender, EventArgs e)
        {
            connection.Open();
        }

        private void button_Fermer_Click_1(object sender, EventArgs e)
        {
            this.RefToForm1.Show();
            this.Close();
        }

        private void button_OK_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox5.Text == "")
            {
                MessageBox.Show("Fill in all the information first!");
            }
            else
            {
                SqlCeCommand cmd = new SqlCeCommand("INSERT INTO compteur(numeroCompteur, idGeographique, abonne, police) VALUES(@numeroCompteur, @idGeographique, @abonne, @police)", connection);
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("@numeroCompteur", textBox1.Text);
                cmd.Parameters.AddWithValue("@idGeographique", textBox2.Text);
                cmd.Parameters.AddWithValue("@abonne", textBox3.Text);
                cmd.Parameters.AddWithValue("@police", textBox5.Text);

                try
                {
                    int affectedrows = cmd.ExecuteNonQuery();

                    if (affectedrows > 0)
                    {
                        MessageBox.Show("Data added successfully!");
                    }
                    else
                    {
                        MessageBox.Show("Failed to add data!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } 
        }
    }
}

The problem is that the modifications are only applied on the database in the emulator, but not on the database .sdf file I have in my project.

Select the releve.sdf database file in your project, go to the Properties section, and make sure Build Action = Content and Copy to Output Directory = Copy if newer .

屏幕截图

That should be all you need, but if there are other issues you may want to double check the file location:

    private void button_OK_Click_1(object sender, EventArgs e)
    {
        MessageBox.Show(pathDB, "Check Path Location!");
        // ... rest of your code
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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