连接到数据库时出错 [英] Error in connection to database

查看:136
本文介绍了连接到数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

错误是:


建立与SQL Server的连接时出现网络相关或实例特定的错误。找不到服务器或
无法访问。验证实例名称是否正确,并且
SQL服务器未配置为允许远程连接。
(提供程序:SQL网络接口,错误:26 - 找到
时出错服务器/指定实例)


我尝试了 SqlCeConnection 而不是 SqlConnection ,但是编译器找不到该类的库。 / p>

请帮助解决这个问题。




  • 操作系统:Windows 7

  • 工具:Microsoft Visual Studio 2010

  • 语言:C#


解决方案

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

  SqlCeConnection sqlConnection = new SqlCeConnection(connectionString); 

从这里下载libs Microsoft SQL Server Compact 4.0


  1. System.Data.SqlServerCe.dll 的引用添加到您的项目

  2. 使用指令使用 .Data.SqlServerCe;

  3. 使用 SqlCeConnection 而不是 SqlConnection


The following code gives an error during execution.

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

The error is:

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)

I tried SqlCeConnection instead of SqlConnection but, the compiler couldn't find the library with that class.

Please, help to solve this problem.

  • The OS : Windows 7
  • Tool : Microsoft Visual Studio 2010
  • Language : C#

解决方案

Your database is a Sql Server compact edition one, you must use :

SqlCeConnection sqlConnection = new SqlCeConnection(connectionString);

Download the libs from here Microsoft SQL Server Compact 4.0

  1. Add a reference to System.Data.SqlServerCe.dll to your project
  2. Add this using directive using System.Data.SqlServerCe;
  3. Use SqlCeConnection instead of SqlConnection

这篇关于连接到数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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