简体   繁体   English

在SQL Server 2008 R2中使用Linq-to-Sql

[英]Using Linq-to-Sql with SQL Server 2008 R2

I have created some Linq-to-SQL classes object in VS2008 for SQL Server 2008 R2 fine. 我已经在VS2008中为SQL Server 2008 R2很好地创建了一些Linq-to-SQL类对象。

I added a connection to it in server explorer. 我在服务器资源管理器中为其添加了连接。 Then add a stored proc to design surface. 然后将存储的proc添加到设计图面。

But when I run the app it produces this error: 但是,当我运行该应用程序时,会产生此错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

I have enabled TCP/IP and Named Pipes for remote connections according to a googled blog. 根据谷歌博客,我已经为远程连接启用了TCP / IP和命名管道。

Go into your designer file, look in the properties, and find the connection string your designer is using. 进入设计器文件,查看属性,并找到设计器正在使用的连接字符串。

Then dig into your dbml file, find the config setting the default constructor of your DataContext is using, and make sure that connection string that setting has is the same as the one from your designer. 然后深入研究dbml文件,找到DataContext默认构造函数正在使用的配置设置,并确保该设置具有的连接字符串与设计器中的相同。

Here's where you can find the designer's connection info: 在这里可以找到设计器的连接信息:

在此处输入图片说明

And this is what my default constructor looks like: 这是我的默认构造函数的样子:

    public DataClasses1DataContext() : 
            base(global::Junk.Properties.Settings.Default.ZoomieRestConnectionString, mappingSource)
    {
        OnCreated();
    }

Since you haven't provided the code, its presumably hard to think what going wrong underneath. 由于您尚未提供代码,因此大概很难想到其底层是什么。 Anyways here is the code for Linq to SQL connection. 无论如何,这里是Linq到SQL连接的代码。 Match it with yours 与您的相匹配

SqlConnection dataConnection = new SqlConnection();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "YourSeverName";
builder.InitialCatalog = "YourDatabase";
builder.IntegratedSecurity = true;
dataConnection.ConnectionString = builder.ConnectionString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM