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

独自空忆成欢 提交于 2019-12-11 22:57:28

问题


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.


回答1:


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
    }


来源:https://stackoverflow.com/questions/18113464/insert-update-delete-are-not-applied-on-sql-server-ce-database-file-for-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!