连接无法登录

时间:2015-04-06 20:48:45

标签: c# asp.net

我正在连接到sql数据库.net 4.5并且它失败我启用了tcpip并设置为端口1433但我仍然得到连接错误。

string connectionString = @"Data Source=DAVIDBUCKLEY-PC\SQLEXPRESS2012;Database=School;Trusted_Connection=True;";
using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("Validate_User"))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Username", Login1.UserName);
        cmd.Parameters.AddWithValue("@Password", Login1.Password);
        cmd.Connection = con;
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        userId = Convert.ToInt32(reader["UserId"]);
        roles = reader["Roles"].ToString();
        con.Close();
    }
    switch (userId)
    {
        case -1:
            Login1.FailureText = "Username and/or password is incorrect.";
            break;
        case -2:
            Login1.FailureText = "Account has not been activated.";
            break;
        default:
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Login1.UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), Login1.RememberMeSet, roles, FormsAuthentication.FormsCookiePath);
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            Response.Cookies.Add(cookie);
            Response.Redirect(FormsAuthentication.GetRedirectUrl(Login1.UserName, Login1.RememberMeSet));
            break;
        }
    }
}

但我仍然收到以下错误

  '/'应用程序中的服务器错误。网络相关或   建立连接时发生特定于实例的错误   SQL Server。服务器未找到或无法访问。校验   实例名称正确且SQL Server配置为   允许远程连接。 (提供者:SQL网络接口,错误:26    - 查找指定的服务器/实例时出错)描述:执行当前Web请求期间发生未处理的异常。   请查看堆栈跟踪以获取有关错误的更多信息   它起源于代码。

SQLExpress数据库文件自动创建错误:

连接字符串使用应用程序的App_Data目录中的数据库位置指定本地Sql Server Express实例。提供程序尝试自动创建应用程序服务数据库,因为提供程序确定数据库不存在。要成功检查应用程序服务数据库是否存在并自动创建应用程序服务数据库,必须满足以下配置要求:

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.
If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database.
Sql Server Express must be installed on the machine.
The proc

1 个答案:

答案 0 :(得分:-1)

连接到SQL Server时遇到问题? 尝试断开防火墙连接。如果您可以断开防火墙连接,可能是因为您错过了一些输入规则,例如“ sql service broker”,请将此输入规则添加到防火墙中: “ SQL ADMIN连接” TCP端口1434 “ SQL ADMIN连接” UDP端口1434 “ SQL分析服务” TCP端口2383 “ SQL浏览分析服务” TCP端口2382 “ SQL DEBUGGER / RPC” TCP端口135 如果您有动态端口,则“ SQL SERVER” TCP PORT 1433和其他端口 “ SQL服务代理” TCP PORT 4022

问候。 波城。