Asp.net登录控制错误:数据库''不存在。确保正确输入名称

时间:2015-05-16 19:18:34

标签: c# asp.net sql-server

使用工具栏中的登录控件:我不确定当我尝试登录时出现此错误的原因。数据库确实存在:

{"Database 'Data Source=(LocalDB)\\v11' does not exist. Make sure that the name is entered correctly."}

Login.aspx.cs

 protected void ValidateUser(object sender, AuthenticateEventArgs e)
    {
        int userId = 0;
        string constr = ConfigurationManager.ConnectionStrings["LoginConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(constr))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Username", Login1.UserName);
                cmd.Parameters.AddWithValue("@Password", Login1.Password);
                cmd.Connection = con;
                con.Open();
                userId = Convert.ToInt32(cmd.ExecuteScalar());
                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:
                    FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
                    break;
            }
        }
    }

我有一个用户表,其中包含两列用户名和密码。我正在使用visual Studio 2014来编写c#和ASP.net Web表单以及SQL Sever Express 2012。

堆栈跟踪

 [SqlException (0x80131904): Database 'Data Source=(LocalDB)\v11' does not exist. Make sure that the name is entered correctly.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1789294
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340642
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +377
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteScalar() +152
   Login.ValidateUser(Object sender, AuthenticateEventArgs e) in c:\Users\Michelle\Desktop\COMF510_65300_HS_task_2\Login.aspx.cs:34
   System.Web.UI.WebControls.Login.OnAuthenticate(AuthenticateEventArgs e) +9614658
   System.Web.UI.WebControls.Login.AttemptLogin() +119
   System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) +75
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +159
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

来源错误:第34行

Line 32:                 cmd.Connection = con;
Line 33:                 con.Open();
Line 34:                 userId = Convert.ToInt32(cmd.ExecuteScalar());
Line 35:                 con.Close();
Line 36:             }

1 个答案:

答案 0 :(得分:0)

您的连接字符串看起来不正确。您应该遵循localdb的这种格式:

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />

| DataDirectory目录|是一个替换字符串。您可以单独配置数据库文件的位置。

作为选项,您可以设置它:

AppDomain.CurrentDomain.SetData("DataDirectory", "C:\myDB");