无法创建SQLCE数据库.我究竟做错了什么? [英] Cannot create SQLCE database. What am I doing wrong?

查看:81
本文介绍了无法创建SQLCE数据库.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看使用SQL Compact Edition作为基于文本的游戏的源(是的,是的,非常怀旧),但是我无法创建数据库.在Visual Studio 2008中,我尝试了以下方法:

1.在服务器资源管理器中右键单击数据连接,然后从菜单中选择添加连接".

2.在选择数据源中选择"Microsoft SQL Server Compact 3.5",然后单击继续.

3.在连接属性下,单击创建... .

此时,添加连接窗体关闭,并且没有用于创建数据库的界面.无论我提供数据库文件的名称还是将其保留为空白,结果都是相同的.我也尝试过通过菜单项 Data |添加新数据源;连接向导将我带回相同问题的 Choose Data Source 界面.

首先,此顺序是否正确或我缺少什么?尝试重新安装SQLCE是否会有所作为?还是应该完全放弃这种方法,并使用压缩的XML文件存储数据?

I want to look at using SQL Compact Edition as the source for a text based game (yeah, yeah, very nostalgic) but I cannot get a database created. In Visual Studio 2008, I have tried this:

1. Right click Data Connections in the Server Explorer and select "Add Connection" from the menu.

2. Select "Microsoft SQL Server Compact 3.5" in the Choose Data Source and click Continue.

3. Under Connection Properties, click Create....

At that point, the Add Connection form closes and there is no interface for creating the database. Whether I provide a name for the database file or leave it blank, the result is the same. I have also tried going in through the menu item Data | Add New Data Source; the connection wizard takes me back to the same Choose Data Source interface with the same problem.

First off, is this sequence correct or am I missing something? Would it make a difference to attempt a reinstall of SQLCE? Or should I give up this approach entirely and go with a compacted XML file for my data?

推荐答案

序列正确-当我尝试获得创建新的单击创建"按钮后,单击"SQL Server Compact数据库"对话框.

但是,我的计算机上也安装了SQL Server 2008 R2,因此我的配置可能与您的配置不同.我确实记得在创建SqlCE fdatabase时遇到问题,但这大约是两年前的事,我不记得我如何修复它的所有细节.

检查事项:

您可以通过服务器资源管理器创建与任何其他数据库的连接吗?
您可以通过服务器资源管理器连接到现有的SQLCE数据库吗?

如果一切都崩溃了,则可以执行以下操作,并查看是否可以连接到该端口:
The sequence is correct - when I try I get the "Create New SQL Server Compact Database" dialog after pressing the "Create" button.

However, I also have SQL Server 2008 R2 installed on my machine, so my configuration may not be the same as yours. I do vaugely remember having problems creating SqlCE fdatabases, but that was about two years ago, and I can''t remember all the details of how I fixed it.

Things to check:

Can you create a connection to any other database via the Server Explorer?
Can you connect to an existing SQLCE database via Server Explorer?

If it all falls apart, you could do the following, and see if you can connect to that:
private void RebuildDB()
    {
    if (!File.Exists(strDB))
        {
        try
            {
            SqlCeEngine engine = new SqlCeEngine(strCon);
            engine.CreateDatabase();
            Log.AddLog("DataBase Created");
            }
        catch (System.Exception ex)
            {
            Log.AddLog("Error creating database: " + ex.ToString());
            }
        }
    using (SqlCeConnection con = new SqlCeConnection(strCon))
        {
        con.Open();
        SqlCeCommand cmd = new SqlCeCommand("SELECT table_name FROM INFORMATION_SCHEMA.TABLES", con);
        SqlCeDataReader r = cmd.ExecuteReader();
        Log.AddLog("Reading Tables");
        bool tablePresent = false;
        while (r.Read())
            {
            string tableName = (string) r[0];
            if (tableName == strTable)
                {
                tablePresent = true;
                }
            }
        if (tablePresent)
            {
            Log.AddLog("Removing table");
            cmd = new SqlCeCommand(string.Format("DROP TABLE {0}", strTable), con);
            cmd.ExecuteNonQuery();
            }
        Log.AddLog("Creating table");
        cmd.Dispose();
        cmd = new SqlCeCommand(string.Format("CREATE TABLE {0} (id int NOT NULL, name nvarchar(100), level nvarchar(10))", strTable), con);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        cmd = new SqlCeCommand(string.Format("CREATE UNIQUE INDEX idx1 ON {0} (id)", strTable), con);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        cmd = new SqlCeCommand(string.Format("INSERT INTO {0} (id, name, level) VALUES (@ID, @NM, @LV)", strTable), con);
        cmd.Parameters.AddWithValue("@ID", 1);
        cmd.Parameters.AddWithValue("@NM", "Joe Green");
        cmd.Parameters.AddWithValue("@LV", "User");
        cmd.ExecuteNonQuery();
        cmd.Parameters["@ID"].Value = 2;
        cmd.Parameters["@NM"].Value = "Pete Smith";
        cmd.Parameters["@LV"].Value = "User";
        cmd.ExecuteNonQuery();
        cmd.Parameters["@ID"].Value = 3;
        cmd.Parameters["@NM"].Value = "Miss Scarlet";
        cmd.Parameters["@LV"].Value = "User";
        cmd.ExecuteNonQuery();
        cmd.Parameters["@ID"].Value = 4;
        cmd.Parameters["@NM"].Value = "Mike Black";
        cmd.Parameters["@LV"].Value = "User";
        cmd.ExecuteNonQuery();
        cmd.Parameters["@ID"].Value = 5;
        cmd.Parameters["@NM"].Value = "The Boss";
        cmd.Parameters["@LV"].Value = "Admin";
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
        }
    }

这将创建一个小的单表数据库,并使用一些值填充它.

That will create a small, single table DB, and populate it with a few values.


这篇关于无法创建SQLCE数据库.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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