SQL Server CE数据库升级3.5到4.0 C#

时间:2013-09-10 19:54:14

标签: c# sql-server-ce

我是全新的。我正在读一本书并教我自己的C#。我正在读一本HEAD FIRST书。我刚刚创建了一个联系人数据库程序。我把SQL数据库放在表单上做了所有的步骤。当我去运行程序时,我得到这个视图源打印?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e) {
            MessageBox.Show("Contact List 1.0. \nWritten by: Greg Amero", "About");
        }

        private void peopleBindingNavigatorSaveItem_Click(object sender, EventArgs e) {
            this.Validate();
            this.peopleBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.contactDBDataSet);
        }

        private void Form1_Load(object sender, EventArgs e) {
            // TODO: This line of code loads data into the 'contactDBDataSet.People' table. You can move, or remove it, as needed.  
            this.peopleTableAdapter.Fill(this.contactDBDataSet.People);
        }
    }
}

this.peopleTableAdapter.Fill(this.contactDBDataSet.People);我收到错误消息说

  

SqlCelnvaliddatabase格式异常未处理   数据库文件由早期版本的SQL Server Compact创建。请使用SqlCeEngine.Upgrade()方法升级。   我使用visual 2010

得到了上述错误

如果我使用Visual 2012 express它工作正常,我认为它与它们运行的​​SQL Server CE版本有关。我已经安装了SQL Server CE 3.5,4.0等但仍然无法正常工作..请帮助..

Greg

1 个答案:

答案 0 :(得分:0)

使用以下代码将SQL Server Compact版本3.5文件升级到4.0。

public static void Upgrade(string fileName, string password)
{
    if (String.IsNullOrEmpty(fileName) || password == null)
        throw new Exception("Unable to create connection string because filename or password was not defined");

    try
    {
        var connectionString = string.Format(@"Data source={0};Password={1}", fileName, password);
        var engine = new SqlCeEngine(connectionString);
        engine.Upgrade();
    }
    catch (Exception ex)
    {
        throw (ex);
    }
}