【问题标题】:Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile插入、更新、删除不适用于 Windows Mobile 的 SQL Server CE 数据库文件
【发布时间】:2013-08-07 20:44:47
【问题描述】:

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

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

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文件。

【问题讨论】:

  • 您描述的行为是预期的和正常的。通过从 VS 执行 WM 项目,二进制文件和内容文件(在本例中为 .sdf)被复制到设备/模拟器。如果您想保留修改后的 .sdf 文件的副本,您必须自己从模拟器中手动提取它。
  • 有没有办法自动从设备中提取数据库文件并替换项目中的原始文件?
  • 您可以使用 itsutils(远程工具:forum.xda-developers.com/wiki/XdaUtils)从 ActiveSync 连接的设备下载文件:hjgode.de/wp/2010/02/26/…

标签: c# windows-mobile sql-server-ce


【解决方案1】:

在你的项目中选择releve.sdf数据库文件,进入Properties部分,确保Build Action = ContentCopy to Output Directory = Copy if newer

应该就是您所需要的,但如果还有其他问题,您可能需要仔细检查文件位置:

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

【讨论】:

  • 他想走另一条路,我想。他希望设备 DB 的更改返回到 PC。
  • 在我第一次将数据库文件添加到我的项目时,当我被询问时,我选择不将原始文件复制到项目目录,因此没有出现带有黄色图标的文件。我不知道这是否正确,但至少我可以看到我在模拟器中应用的修改。
  • 那么你的程序运行正常。如果您想在 PC 上查看数据库条目,则需要将该数据库从模拟器复制回 PC。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多