插入、更新、删除不适用于 Windows Mobile 的 SQL Server CE 数据库文件 [英] Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile

查看:36
本文介绍了插入、更新、删除不适用于 Windows Mobile 的 SQL Server CE 数据库文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 2008 使用 C# 开发 Windows Mobile 6.5 应用程序,它连接到 SQL Server CE 数据库.

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

我用这段代码插入行,每一列的值与一个textBox字符串相关:

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);
                }
            } 
        }
    }
}

问题是修改仅适用于模拟器中的数据库,而不适用于我的项目中的数据库 .sdf 文件.

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.

推荐答案

选择项目中的 releve.sdf 数据库文件,转到 Properties 部分,然后使确定 Build Action = ContentCopy to Output Directory = Copy if newer.

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
    }

这篇关于插入、更新、删除不适用于 Windows Mobile 的 SQL Server CE 数据库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆