升级后的.net webapp无法打开与数据库的连接

发布于 2024-07-30 02:10:35 字数 1666 浏览 6 评论 0原文

我有一个旧的 .net 1.0 Web 应用程序,需要对其进行一些维护。 我已经使用自动升级程序将其升级到.net 3.5(后来又尝试了2.0),但现在它无法连接到数据库。

从表面上看,这看起来像是一个菜鸟连接字符串问题,但我认为它更有可能与升级中的一些微妙问题有关。


我收到一般错误消息:

建立与服务器的连接时发生错误。 当连接到SQL Server 2005时,出现此故障的原因可能是SQL Server在默认设置下不允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开到 SQL Server 的连接)


源代码非常基本,没有任何附加功能:

protected static SqlConnection objConn;

objConn = new SqlConnection(strConnectionString);

try
{
    objConn.Open();
}

并且还尝试过:

using (objConn = new SqlConnection(strConnectionString))
{
    objConn.Open();
    ...
}

连接字符串来自网络。配置,并使用调试器设置断点并在尝试打开连接之前查看连接的属性,我可以看到它正确找到了连接字符串:

connectionString =“Data Source = XXX.XXX.XXX.XXX” ;初始目录=XXXXXX;用户ID=XXXXXX;密码=XXXXXX"


我认为我已经排除了一些事情:

无论连接到Sql Server 2005还是Sql Server 2000,错误消息都是相同的。

没有错误或VS 错误列表中的警告。

连接字符串与也使用相同数据库的其他几个网站相同,因此我知道连接字符串是正确的。

我尝试让它从我的本地计算机(我可以通过 Sql 查询分析器连接到数据库)和普通的网络服务器连接,所以这不是防火墙问题,也不是最大连接问题。

它连接到服务器的 IP 地址,因此不是计算机浏览器问题(其他 Web 应用程序可以正常连接。TCP

和命名管道是 sql 服务器上启用的 2 个网络协议。

将“Network Library=DBMSSOCN;”添加到连接字符串更改错误消息以包含“provider:TCP Provider,错误”(前几天我在连接字符串中更改了其他一些内容也产生了这种影响,但我现在不记得是什么了。)


我已经已经浏览了关于堆栈溢出的其他 3 篇类似帖子:
(无法在此处列出链接,因为我是新用户,但如果其他人想在评论中链接到他们,他们的问题 ID 是:63875、1038888、846479)
另一个网站上的这篇文章提出了一些可以尝试的好主意,但也没有帮助:
http://weblogs.sqlteam.com/tarad/archive/ 2008/05/23/60609.aspx


我最好的猜测是它是由.net版本之间的升级引起的 - 也许web.config中有问题?

这是一个 C# 应用程序,非常小/基本,但已分为三个项目。

I have an old .net 1.0 webapp that needs a bit of maintenance done on it. I've used the auto-upgrader to upgrade it to .net 3.5 (and also later tried 2.0) but now it can't connect to the database.

On the surface this looks like a noob connection-string problem, but I'm thinking it's more likely to be related to some subtle problem from the upgrade.

I get the generic error message:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

The source code is pretty basic, with no bells or whistles:

protected static SqlConnection objConn;

objConn = new SqlConnection(strConnectionString);

try
{
    objConn.Open();
}

and also tried it as:

using (objConn = new SqlConnection(strConnectionString))
{
    objConn.Open();
    ...
}

The connection string comes from the web.config, and using the debugger to set a break point and look through the properties of the connection before it tries to open it, I can see that its found the connection string properly:

connectionString="Data Source=XXX.XXX.XXX.XXX;Initial Catalog=XXXXXX;User ID=XXXXXX;Password=XXXXXX"

A handful of things I think I've ruled out:

The error message is the same whether connecting to Sql Server 2005 or Sql Server 2000.

No errors or warnings in the VS Error List.

The connection string is the same as a couple of other websites that also use the same database, so I know that the connection string is correct.

I've tried having it connect from my local computer (where I can connect to the databases via Sql Query Analyzer) and from the normal webserver, so it's not a firewall issue, neither is it a max-connections issue.

It's connecting to the server's IP address, so its not a computer browser issue (and other webapps can connect fine.

TCP and named pipes are the 2 network protocols enabled on the sql server.

Adding "Network Library=DBMSSOCN;" to the connection string changes the error message to include "provider: TCP Provider, error" (there was something else I changed in the connection string the other day that also had that affect, but I can't remember what now.)

I've already looked through 3 other similar posts on stack overflow:
(Can't list links here because I'm a new user, but their question ID's if someone else wants to link to them in a comment are: 63875, 1038888, 846479)
And this article on another site had some good ideas of things to try that didn't help either:
http://weblogs.sqlteam.com/tarad/archive/2008/05/23/60609.aspx

My best guess is that its something caused by the upgrade between .net versions - maybe something wrong in the web.config?

It's a C# app, pretty small/basic, but has been split into three projects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

要走就滚别墨迹 2024-08-06 02:10:35

由于从未针对此问题提供实际的解决方案,因此这里有一个对我有帮助的解决方案:

当我将完美运行的 ASP.NET 2.0 应用程序升级到 .NET 3.5 时,我遇到了完全相同的问题。 Web 应用程序由 Windows XP 上的 IIS 5.1 托管,我使用的是 SQL Server Express 2005。我在 web.config 中使用以下连接字符串:

<add name="myDbConnection" providerName="System.Data.SqlClient" connectionString="Data Source=http://localhost;Server=.\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True"/>

如您所见,我在这里没有进行 tcp 连接或连接到远程服务器。 我只是通过命名管道连接本地主机上的 SQLEXPRESS 实例。 这就是为什么我认为它与允许 SQL Server 2005 上的远程连接完全无关。但正如我发现的那样! (我不知道真正的原因!)我花了几个小时才找到答案,所以也许这对某人有帮助......

你必须做什么(请使用谷歌了解详细信息):

  1. 允许本地和远程连接通过 SQL Server 2005 Surface Area 配置工具
    (我选择“使用 TCP/IP 和命名管道”选项)
  2. 启用 SQL Server Browser 服务
  3. 通过 SQL Server 配置管理器工具将所有 IP 的侦听 TCP 端口设置为 1433
  4. 享受正在运行的 Web 应用程序 :-)

为什么这一切当 .NET 3.5 Web 应用程序已经可以从 .NET 2.0 运行时,成功地从 .NET 3.5 Web 应用程序连接到 SQL Server 所需的必要条件超出了我的范围!

As there was never an actual solution provided for this problem, here is the one that helped me:

I had the exact same problem when I upgraded a perfectly running ASP.NET 2.0 application to .NET 3.5. The web app is being hosted by IIS 5.1 on Windows XP and I'm using SQL Server Express 2005. I'm using the following connection string in my web.config:

<add name="myDbConnection" providerName="System.Data.SqlClient" connectionString="Data Source=http://localhost;Server=.\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True"/>

So as you see, I'm doing no tcp connection here or connecting to a remote server. I'm just connecting the SQLEXPRESS instance on the localhost thru named pipes. That's why I thought, it will have absolutely nothing to do with allowing remote connections on SQL Server 2005. But as I found out, it does! (I don't know the actual reason for this!) I spent a few hours to find that out, so maybe this helps someone...

What you have to do (please use google for the details):

  1. Allow Local and Remote Connections via SQL Server 2005 Surface Area Configuration Tool
    (I choose the option "Using both TCP/IP and named pipes")
  2. Enable the SQL Server Browser service
  3. Set the listening TCP port to 1433 for all IPs via SQL Server Configuration Manager Tool
  4. Enjoy your running web app :-)

Why all this is necessary in order to succesfully connect from an .NET 3.5 Web App to SQL Server when it works already from .NET 2.0 is beyond me!

胡渣熟男 2024-08-06 02:10:35

编写一个控制台应用程序,该应用程序不执行任何操作,只是尝试使用您发布的代码连接到数据库。 在控制台程序中对连接字符串进行硬编码。

换句话说,消除所有干扰,以确保您尝试运行的核心代码确实正常工作。

Write a console application that does nothing but try to connect to the database using the code you posted. Hard-code the connection string in the console program.

In other words, remove all the distractions to make sure that the core code you are trying to run actually works correctly.

要走就滚别墨迹 2024-08-06 02:10:35

根据 这个,SQL 2005 并未配置为允许传入的本地或远程连接,您必须自己启用它。

According to this, SQL 2005 does not come configured to allow incoming local or remote connections and you have to enable it yourself.

尴尬癌患者 2024-08-06 02:10:35

我确信您已经完成了这些步骤,但是...尝试启用 SQL Server 的远程连接并启用 SQL Server Browser 服务。 两者的演练可以在此处找到。

I'm sure you have gone through these steps already but...try enabling remote connection for SQL Server and enabling the SQL Server Browser service. A walkthrough for both can be found here.

抱猫软卧 2024-08-06 02:10:35

您可以尝试使用其他类型的连接字符串。 我的连接字符串的格式通常如下:

<add name="DataFrom" connectionString="SERVER=[servername];Database=[dbname];UID=[userid];PWD=[password];"/>

您所说的其他应用程序也可以在 .NET 3.5 应用程序中运行吗?

我也很好奇你是如何让这段代码工作的,

using (var = new SqlConnection(strConnectionString))
{
    objConn.Open();
    ...
}

我想你的意思是

using (objConn = new SqlConnection(strConnectionString)) 

对的吗?

You could try using another type of connection string. My connection strings are usually formatted like:

<add name="DataFrom" connectionString="SERVER=[servername];Database=[dbname];UID=[userid];PWD=[password];"/>

Are the other aplication you said that are working also .NET 3.5 applications?

I am also curious how you managed to get this piece of code working

using (var = new SqlConnection(strConnectionString))
{
    objConn.Open();
    ...
}

I think you mean

using (objConn = new SqlConnection(strConnectionString)) 

right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文