c# - 根据 web.config 检查 sqlce 数据库是否存在

标签 c# sql-server-ce

引用问题How to check for the existence of a DB?我试图根据 web.config 连接字符串检测数据库是否存在,但运气不好。

我使用上面的最后一个答案作为我的试验。

试验 1: web.config 中的连接字符串

    <add name="sqlCEConnString" connectionString="Data Source=|DataDirectory|db.sdf;Initial Catalog=master" providerName="System.Data.SqlServerCE.4.0"/>

我收到错误:不支持关键字:“初始目录”。

试验 2:没有关键字

字符串 conString = ConfigurationManager.ConnectionStrings["sqlCEConnString"].ConnectionString;

    using(SqlCeConnection cnn = new SqlCeConnection(conString))
    { 
        cnn.Open();
        using (SqlCeCommand com = new SqlCeCommand("select count(*) from sys.databases where name = 'db.sdf'" , cnn))
        {
            int j=com.ExecuteNonQuery();
            Response.Write("Result:" + j);
        }
    }

这次我遇到了以下错误: 指定的表不存在。 [@@sys.databases]

我做错了什么?

最佳答案

最后我找到了根据连接字符串检测数据库是否存在的方法。在“SqlCeEngine”类下有一个名为“Verify”的方法,可以通过该方法进行检测。

        string conString = ConfigurationManager.ConnectionStrings["sqlCEConnectionString"].ConnectionString;
    using (SqlCeEngine objCeEngine = new SqlCeEngine(conString))
    {
        if (!objCeEngine.Verify())
        {
            objCeEngine.CreateDatabase();
        }
    }

关于c# - 根据 web.config 检查 sqlce 数据库是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4986677/

相关文章:

c# - 处理后SqlDataReader记录的使用

c# - Windows7 中的 CefSharp 问题

sql - 无法将 SQL Compact Edition 添加到我的 VB.net 应用程序

entity-framework - EF 代码优先迁移 : SqlCeException altering NTEXT column

sql-server - SQL Server CE 3.5 更新行错误 DB_E_ERRORSOCCURRED 列错误是 DBSTATUS_E_SCHEMAVIOLATION

c# - 我没有 SqlServerCe 程序集?

c# - 从列表中选择后对象仍然链接(引用)?

C# 将焦点放在按钮上/使其处于事件状态

c# - 静态方法中的泛型参数正在调用同一个类——有更好的选择吗?

windows-mobile - 调试 CF.NET 应用程序时如何禁用 SQL Server Compact 的自动部署?