c# - 连接数据库时出错

标签 c# error-handling sql-server-ce database-connection

以下代码在执行期间给出错误。

string connectionString = "Data Source=D:\\Base.sdf;Persist Security Info=False";
SqlConnection sqlConnection = new SqlConnection(connectionString)) 
sqlConnection.Open();

错误是:

A network-related or instance-specific error occured while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL server is not configured to allow remote connections. (provider:SQL network Interfaces, error: 26 - Error locating Server/Instance Specified)



我尝试使用SqlCeConnection而不是SqlConnection,但编译器找不到具有该类的库。

请帮助解决这个问题。
  • 操作系统:Windows 7
  • 工具:Microsoft Visual Studio 2010
  • 语言:C#
  • 最佳答案

    您的数据库是Sql Server精简版之一,必须使用:

    SqlCeConnection sqlConnection = new SqlCeConnection(connectionString);
    

    从此处下载库Microsoft SQL Server Compact 4.0
  • 在项目中添加对System.Data.SqlServerCe.dll的引用
  • 使用System.Data.SqlServerCe;
  • 使用指令添加它
  • 使用SqlCeConnection代替SqlConnection
  • 关于c# - 连接数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12157209/

    相关文章:

    c# - GridView 单元格未在 RowCommand 事件上注册任何控件

    php - 全局设置 error_handler

    sql-server-ce - 如果 SQL Server CE 中存在则删除表

    c# - 是否有预定义的 ThisIsABugException?

    c# - "using static"杀死 AsParallel

    objective-c - 失败,但没有返回错误对象。为什么?

    c# - 如何读取 |DataDirectory| 的当前路径从配置设置

    c# - 如何从偶尔连接的设备(笔记本电脑)获取数据到后端 SQL Server

    c# - 使用 unity3d 获取设备的方向

    ruby-on-rails - Rails 5 中的自定义错误 JSON