“ConnectionString属性无效。”当我知道它是有效的

时间:2010-11-30 19:12:03

标签: c# .net asp.net-mvc connection-string

我有一个ASP.NET MVC应用程序,其中数据库位于IBM i-Series服务器上。当我开始出现The ConnectionString property is invalid.错误时,我的应用程序开发接近完成:

  1. 仅在登录时
  2. 重建后第一次成功登录后
  3. 任何登录的人仍然可以正常工作
  4. 另请注意,此问题仅显示在我的解决方案中的一个项目。另一个项目使用完全相同的连接字符串,并没有这个问题(复制和粘贴是100%肯定)。我正在积极开发这些项目,但是在登录工作之后没有触及连接字符串,也没有使用AccountController和相关的模型类。

    我使用的是Visual Studio 2008和.NET 3.5版。

    连接字符串:

    <connectionStrings>
        <add name="IbmIConnectionString" connectionString="DataSource=192.168.50.200;DefaultCollection=QMFILES;Naming=sql;UserID=XXX;Password=XXXX;"/>
    </connectionStrings>
    

    帐户控制器登录方法:

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            string fullName = String.Empty;
            string employeeId = String.Empty;
    
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    EmployeeLoginModel elm = new EmployeeLoginModel();
                    elm.GetUserInfo(model.UserName, model.Password, out fullName, out employeeId);
                    // Update the AuthCookie to include the last 4 digits of the SSN.
                    string userDataString = String.Format("{0}|{1}|{2}", model.Password, fullName.Trim(), employeeId.Trim());
                    HttpCookie authCookie = FormsAuthentication.GetAuthCookie(model.UserName, model.RememberMe);
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                    FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userDataString);
                    authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                    Response.Cookies.Add(authCookie);
    
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
    
            // If we got this far, something failed, redisplay form
            return View(model);
        }
    

    员工登录模式:

    public class EmployeeLoginModel
    {
        public string UserName { set; get; }
        public string Password { set; get; }
    
        private iDB2Connection conn;
    
        /// <summary>
        /// Initializes a new instance of the <see cref="EmployeeLoginModel"/> class.
        /// </summary>
        public EmployeeLoginModel()
        {
            conn = new iDB2Connection(ConfigurationManager.ConnectionStrings["IbmIConnectionString"].ConnectionString);
        }
    
        /// <summary>
        /// Determines whether [is valid user] [the specified username].
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>
        ///     <c>true</c> if [is valid user] [the specified username]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValidUser(string username, string password)
        {
            int count = 0;
    
            // Get the data from the iSeries
            using (conn)
            {
                string sqlStatement = "SELECT COUNT(XXXXX) FROM XXXXX WHERE UPPER(XXXXXX) = @1 AND XXXXXX = @2";
    
                iDB2Command cmd = new iDB2Command(sqlStatement, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@1", username.ToUpper());
                cmd.Parameters.Add("@2", password);
                conn.Open();
                count = (Int32)cmd.ExecuteScalar();
                conn.Close();
            }
    
            return ((count == 0) ? false : true);
        }
    

3 个答案:

答案 0 :(得分:3)

获得此错误的另一个非常重要的原因是,未安装所需的DB2驱动程序。

内部异常陈述

  

无法加载DLL'cwbdc.dll':找不到指定的模块。 (来自HRESULT的异常:0x8007007E)   键入:System.DllNotFoundException

答案 1 :(得分:2)

发布后,我有一个理论。我在浏览器之间切换为演示设置了东西。我把方法改为:

    public bool IsValidUser(string username, string password)
    {
        int count = 0;

        // Get the data from the iSeries
        using (iDB2Connection conn = new iDB2Connection(ConfigurationManager.ConnectionStrings["IbmIConnectionString"].ConnectionString))
        {
            string sqlStatement = "SELECT COUNT(XXXXXX) FROM XXXXXX WHERE UPPER(XXXXXX) = @1 AND XXXXXX = @2";

            iDB2Command cmd = new iDB2Command(sqlStatement, conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@1", username.ToUpper());
            cmd.Parameters.Add("@2", password);
            conn.Open();
            count = (Int32)cmd.ExecuteScalar();
            conn.Close();
        }

        return ((count == 0) ? false : true);
    }

现在似乎正在运作。我想知道这是不是问题。

答案 2 :(得分:1)

我认为因为你使用了using语句之外的连接,所以在转到其他函数后它会被关闭,所以当你调用IsValidUser时,它会抛出异常。在第二个代码中,您在using语句中使用它,在调用之后它将被Garbage Collection释放。这是工作。