连接到本地数据库,Visual Studio 2012 [英] Connection to local db ,Visual studio 2012

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

问题描述

大家好,
我正在使用Visual Studio 2012,Framework 4.0,
我正在尝试使用连接字符串连接到本地数据库

SqlConnection con =新的SqlConnection(@"Server =(localdb)\ v11.0; Integrated Security = true; Database = Practice;");

但是我得到如下异常:-

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:SQL网络接口,错误:26-错误指定服务器/实例的位置)

请指教,我想念什么吗?

关于

Hi all,
I am using visual studio 2012,framework 4.0,
I am trying to connect to the local db using connection string

SqlConnection con = new SqlConnection(@"Server=(localdb)\v11.0;Integrated Security=true;Database=Practice;");

But I am getting the exception as follows:-

A network-related or instance-specific error occurred 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 configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Please advice,Am I missing something?

Regards

推荐答案

首先要确保SQL Server服务正在运行.其他原因可能是错误的连接字符串或某些防火墙阻塞.

在这里,请查看此博客文章以解决问题:异常-错误26-错误定位了指定的服务器实例 [ ^ ]
提供程序:SQL网络接口,错误:26-错误定位服务器/实例... [ ^ ]
未找到指定的SQL Server" o .. [^ ]
First and foremost make sure that the SQL Server services are running. Other reasons could be, incorrect connection string or some Firewall blockage.

Here, have a look at this blog post to troubleshoot the issue : MSDN Blogs: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified[^]

If needed, look at these one:
Exception - error 26 - Error Locating Server Instance Specified[^]
provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance...[^]
"Specified SQL server not found" o..[^]


您使用的连接字符串错误.
请尝试根据您的配置使用正确的版本.

为此,您拥有一个很棒的网站- http://www.connectionstrings.com/ [ ^ ].

根据数据库和技术搜索所需的连接字符串.

例如(SQL Server 2008)- SQL Server 2008的连接字符串 [
The connection string you are using is wrong.
Please try to use the correct one according to your configurations.

For that you have one awesome website - http://www.connectionstrings.com/[^].

Search for the required connection string according to the database and technology.

For Example (SQL Server 2008) - Connection strings for SQL Server 2008[^].
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;



对于本地数据库,您可以仅提供一个点."而不是(localdb)\ v11.0 .

因此,您可以尝试以下一种方法...



For Local DB, you can just provide a dot "." instead of (localdb)\v11.0.

So, you can try with the below one...

Server=.;Database=Practice;User Id=myUsername;Password=myPassword;


提供必要的UseName和密码.


Provide necessary UseName and Passwords.


1.- Arranca la instancia para poder accesarla desde tucódigo:
西班牙的拉兰卡·德·科兰多·拉·阿兰卡:
c:\> slqlocaldb启动MSSQLLOCALDB

2.- Ejemplo decódigopara conectar一个SQLLocalDb
1.- Arranca la instancia para poder accesarla desde tu código:
La linea de comando para arrancar la instancia es:
c:\>slqlocaldb start MSSQLLOCALDB

2.- Ejemplo de código para conectar a SQLLocalDb
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=database;AttachDbFilename=C:\Desarrollo\database.mdf;Integrated Security=True";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("select * from usuarios", connection))
using (var adapter = new SqlDataAdapter(command))
{
    var table = new DataTable();
    int count = adapter.Fill(table);
    MessageBox.Show(count.ToString());
}


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

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