简体   繁体   English

无法从ASP.NET应用程序访问SQL Server

[英]SQL Server not reachable from ASP.NET application

I have an ASP.NET page which has a small form to handle enrollment into his course. 我有一个ASP.NET页,该页的格式很小,可以处理其课程的注册。

Obviously the data from this form has to be inserted into a database. 显然,此表单中的数据必须插入数据库中。

My problem lies in the actual connection to the database server, I have it on my local PC, for testing purposes. 我的问题在于与数据库服务器的实际连接,出于测试目的,我将其连接到本地PC上。

I can connect to the server just fine through my SQL Server Management Studio, however when I try to connect from my web application (uploaded it to a free host purely for testing), it gives me this error: 我可以通过SQL Server Management Studio很好地连接到服务器,但是,当我尝试从Web应用程序进行连接(纯粹将其上传到免费主机进行测试)时,却出现以下错误:

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: TCP Provider, error: 0 - An attempt was made to access a socket in a way forbidden by its access permissions.) (提供者:TCP提供程序,错误:0-尝试以其访问权限禁止的方式访问套接字。)

After extensive Google'ing and rewriting the connection string in my web.config file, I still can't find what's going wrong exactly. 经过广泛的Google搜索并在我的web.config文件中重写了连接字符串之后,我仍然找不到确切的问题所在。

I know for a fact that the DNS I'm using works, as a friend of mine has already tested it several days ago by connecting through his own SQL Server Management Studio. 我知道我正在使用的DNS可以正常工作,因为我的一个朋友几天前已经通过其自己的SQL Server Management Studio连接进行了测试。

Here's my web.config : (blanked out the password in the constring, obviously) 这是我的web.config :(很明显,空白处的密码是空白的)

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
    <customErrors mode="Off"/>
  </system.web>
  <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  <connectionStrings>
     <add name="SQLCS" 
          providerName="System.Data.SqlClient"   
          connectionString="Server=ydekempe.ddns.net,5555\Cocktails;Database=Website;Integrated Security=False;User ID=WebUser;Password=********;"/>
  </connectionStrings>
</configuration>

And just for being complete in providing you everything you may need, here's the code for the actual connection: 只是为了完整地为您提供可能需要的一切,以下是实际连接的代码:

protected void Inschrijven(object sender, EventArgs e)
{
    int uid = 0;
    string conStr = ConfigurationManager.ConnectionStrings["SQLCS"].ConnectionString;

    try
    {
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            using (SqlCommand cmd = new SqlCommand("Inschrijven", conn))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@naam", txtNaam.Text.Trim());
                    cmd.Parameters.AddWithValue("@voornaam", txtVoorNaam.Text.Trim());
                    cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                    cmd.Connection = conn;
                    conn.Open();
                    uid = Convert.ToInt32(cmd.ExecuteScalar());
                    conn.Close();
                }
            }
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    string msg = "";
    switch (uid)
    {
        case 0:
            msg = "Er ging iets mis.\\nProbeer later opnieuw";
            break;
        case -1:
            msg = "Dit e-mail adres is al in gebruik.";
            break;
        default:
            msg = "Inschrijving verwerkt.";
            break;
    }
    ClientScript.RegisterStartupScript(GetType(), "Boodschap", "Boodschap: '" + msg + "');", true);
}

PS: The actual connection info is IP: ydekempe.ddns.net & Port: 5555 Just in case you were wondering. PS:实际的连接信息是IP:ydekempe.ddns.net和端口:5555以防万一您想知道。

The database server is your local, and where is the Web server hosted? 数据库服务器是您的本地服务器,Web服务器托管在哪里? You mentioned it is a free host and I am guessing it is not on your local machine. 您提到这是一个免费主机,我想它不在您的本地计算机上。 Make sure your machine is accessible from wherever the web application is hosted. 确保可以从托管Web应用程序的任何位置访问您的计算机。 For instance, If its is in AWS, you have to verify both Inbound and Outbound rules. 例如,如果它在AWS中,则必须验证入站和出站规则。

Turns out, it was the webhost blocking the outgoing port. 原来,这是Web主机阻止传出端口。

Reply from tech support at SmarterASP : 来自SmarterASP的技术支持的回复

Dear Customer, 尊敬的客户,

Sorry, for performance reasons, we don't allow our customer to connect to remote database server. 抱歉,由于性能原因,我们不允许客户连接到远程数据库服务器。 We suggest you simply make a backup of your database and restore it to our server to test your application. 我们建议您仅备份数据库并将其还原到我们的服务器以测试您的应用程序。 You'll see a much faster performance and reliability using this method. 使用这种方法,您将看到更快的性能和可靠性。

However, if you must connect to a remote database server, please consider upgrading to our Premium Plan or Semi Dedi Plan. 但是,如果必须连接到远程数据库服务器,请考虑升级到我们的高级计划或Semi Dedi计划。 Once you upgrade to these hosting plans, you can enable any ports your want in your Control Panel -> Security Manager -> Outgoing Port Manager. 升级到这些托管计划后,可以在控制面板->安全管理器->传出端口管理器中启用所需的任何端口。

And don't worry, we provide a 60 days FULL Money Back for all of our plans. 不用担心,我们为所有计划提供60天全额退款。

Thank you. 谢谢。

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

相关问题 ASP.NET应用程序可以作为localhost访问,但不能访问127.0.0.1 - ASP.NET application is reachable as localhost but not 127.0.0.1 从asp.net应用程序连接到SQL Server数据库 - connect to sql server database from asp.net application ASP.NET应用程序生产服务器的SQL连接错误 - ASP.NET application SQL connection error from production server 从ASP.NET MVC应用程序连接到SQL Server - Connect to SQL Server from ASP.NET MVC application 从ASP.Net应用程序服务器打印 - Printing from an ASP.Net application server SQL查询可在SQL Server Management Studio中使用,但不适用于ASP.Net应用程序 - SQL query works in SQL Server Management Studio but not from ASP.Net application 如何从在容器中运行的 ASP.NET 核心应用程序连接到具有集成安全性的 Windows 服务器上的 SQL 服务器 - How to connect from ASP.NET Core application running in a container to SQL Server on Windows Server with Integrated Security 在ASP.NET应用程序中更改SQL Server连接字符串 - Changing a SQL Server Connection String in an ASP.NET Application 与我的ASP.NET Web应用程序的多个SQL Server连接 - Multiple SQL Server connection to my ASP.NET web application ASP.net应用程序无法连接到SQL Server命名实例 - ASP.net application unable to connect to SQL Server named instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM