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

Posted

技术标签:

【中文标题】SQL Server CE 数据库升级 3.5 到 4.0 C#【英文标题】:SQL Server CE database upgrade 3.5 to 4.0 C# 【发布时间】:2013-09-10 19:54:13 【问题描述】:

我是全新的。我正在读一本书并自学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); 我收到一条错误消息

SqlCelnvalid 数据库格式异常未处理。 数据库文件是由早期版本的 SQL Server Compact 创建的。请使用 SqlCeEngine.Upgrade() 方法进行升级。 我使用 Visual 2010 得到上述错误

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

格雷格

【问题讨论】:

您说它在Visual 2012 express 中工作,在哪个版本不能工作? 版本 2010 抱歉忘记添加了 【参考方案1】:

使用以下代码将 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);
    

【讨论】:

以上是关于SQL Server CE 数据库升级 3.5 到 4.0 C#的主要内容,如果未能解决你的问题,请参考以下文章