部署到windows azure的Asp.net网站没有读取数据库

时间:2015-10-17 22:05:08

标签: c# asp.net sql-server azure

我使用visual studio中的SQL server express数据库实例将一个asp.net网站部署到windows azure。该网站工作正常,我可以在页面之间浏览但网页没有阅读数据库。

这是我的webconfig文件:

<configuration>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <connectionStrings>
    <add name="MyDBConnectionString" connectionString="Data Source=tcp:jpla04vr2g.database.windows.net,1433;Initial Catalog=DBNAME;User Id=myUser@jpla04vr2g;Password=myPass" providerName="System.Data.SqlClient"/>
</connectionStrings> 

该网站运行正常但未从数据库中读取,该数据库同时也在应用数据中与网站同时部署的windows azure中。

以下是我用于登录的登录代码。当连接字符串指向应用程序数据文件夹中的本地数据库时,此代码在部署之前工作正常:

try
{
    string con = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
    connection = new SqlConnection(con);
    command = new SqlCommand("SELECT AdminId, Firstname, Lastname, Username FROM ADMINISTRATOR WHERE  (Username=@UN OR Email=@EM) AND AdminPassword=@AP");
    command.Parameters.AddWithValue("@UN", (string)txtUserEmail.Text );
    command.Parameters.AddWithValue("@EM", (string)txtUserEmail.Text);
    command.Parameters.AddWithValue("@AP", FixanythingSecrecy.HashPassword(txtUserPassword.Text));
    command.Connection = connection;
    connection.Open();
    command.ExecuteNonQuery();
    reader = command.ExecuteReader(CommandBehavior.CloseConnection);
    if (reader.HasRows)
    {
        while(reader.Read())
        {
            Session.Add("AdminId", reader["AdminId"]);
            Session.Add("Firstname", reader["Firstname"]);
            Session.Add("Lastname", reader["Lastname"]);                        
        }
        Response.Redirect("companies.aspx");
    }
    else
    {
         lblStatus.Text = "User name or password incorrect.";
    }
}
catch (Exception ex)
{
     Response.Redirect("ErrorPage.aspx");
}
finally
{
    command.Connection.Close();
    connection.Dispose();
    command.Dispose();
}

我收到错误:

  

System.Data.SqlClient.SqlException(0x80131904):与网络相关或   建立连接时发生特定于实例的错误   SQL Server。服务器未找到或无法访问。校验   实例名称正确且SQL Server配置为   允许远程连接。 (提供者:命名管道提供商,错误:40 -   无法打开与SQL Server的连接)

0 个答案:

没有答案