【问题标题】:"The ConnectionString property is invalid." when I know it is valid“ConnectionString 属性无效。”当我知道它是有效的
【发布时间】:2010-11-30 19:12:03
【问题描述】:

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

  1. 仅在登录时
  2. 重建后首次成功登录后
  3. 任何人登录后仍可正常工作

另外,请注意,此问题仅出现在我的解决方案中的一个项目中。另一个项目使用完全相同的连接字符串并且没有这个问题(复制和粘贴是 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);
    }

【问题讨论】:

  • 更多详情?也许是连接字符串,你如何检索连接字符串,你的帐户控制器是什么样的?也发布您的 SVN 日志,我再也不会相信“我没有碰它”了。 ;)
  • 顺便说一句,您确定要在此处列出您的密码吗?如果那是您的真实密码,我建议您立即更改它...(仅在此处编辑您的帖子无济于事,因为人们可以看到编辑的历史记录。)
  • 我没有 SVN 日志,因为它还没有进行版本控制。
  • 废话!幸运的是,服务器是内部的。
  • 我已标记为版主注意,以便他们可以删除其中包含密码的修订。

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


【解决方案1】:

获得此错误的另一个非常微不足道的原因是未安装所需的 DB2 驱动程序。

内部异常声明

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

【讨论】:

    【解决方案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);
        }
    

    它现在似乎正在工作。我想知道这是否是问题所在。

    【讨论】:

    • @jfar,内联 SQL 有什么问题?对我来说它看起来很合适,它甚至使用参数来避免 SQL 注入,所以这是一个完全有效的代码。
    • @Darin:参数:好。内联 Sql:不好。问题在于在数据模型更改的情况下问题的可发现性。假设他们重命名了其中一个字段名称,这通常比人们想象的更常见。找出这是一个问题的唯一方法是是否调用了在该字段名称上选择的特定方法。意思是,这是一个运行时错误。
    • @Chris,你建议做什么,请不要告诉我使用 ORM,因为重命名列会在运行时破坏这个 ORM,就像它会破坏这段代码一样。
    • 您在原始问题中的using 将处理conn 对象,然后您将尝试再次使用它。您在这段代码中使用它的方式是正确的。
    • @Chris Lively 但这无助于更新任何 db->代码映射,现在您必须在两个地方适应这种变化。我想达林就在这里。当数据库表发生变化时,您始终是 SOL。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多