gpt4 book ai didi

c# - 使用 MySql 的 Webmatrix.WebData.WebSecurity 不工作。在 WebSecurity.UserExists() 检查中抛出 sql 语法错误

转载 作者:行者123 更新时间:2023-11-28 23:40:23 25 4
gpt4 key购买 nike

我在 MSSQL 中完成了一个项目。我将 MSSQL 迁移到 MySql,因为我认为 Webmatrix.WebData 也将支持 MySql。 WebSecurity 初始化成功。

if (!WebSecurity.Initialized)
{
securityService.Initialize(
"sqlserver",
"UserProfile",
"UserId"
"UserName");
};`

以上代码执行成功。但是,如果无法执行以下部分,

 if (!WebSecurity.UserExists("user"))
{
WebSecurity.CreateUserAndAccount("user", "password");
}

检查 WebSecurity.UserExists("user") 失败。这是错误信息-

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code

Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[UserId] FROM [UserTable] WHERE (UPPER([UserName]) = UPPER('user'))' at line 1

请帮我解决这个问题。

最佳答案

ASP.Net 附带的 WebSecurity(上面代码中使用的那个)在代码中嵌入了一些 Microsoft T-SQL 语法。 MySQL 语法和 MSSQL 之间的差异很小,这可能是导致错误的原因。

从显示的错误中,我可以看到 WebSecurity 中的 CreateUserAndAccount 方法以某种方式尝试执行 T-SQL 语句 .... [UserId] FROM [UserTable] WHERE (UPPER ([UserName]) = UPPER('user')) 但是,虽然这在 MSSQL 中有效,但方括号 "[""]"会导致 MySQL 出错。

解决方法是使用 MySql.Web.Security.MySqlWebSecurity.CreateUserAndAccount("user", "password"),当然,您可以添加一个使用 声明或以这种方式使用它。

see link

关于c# - 使用 MySql 的 Webmatrix.WebData.WebSecurity 不工作。在 WebSecurity.UserExists() 检查中抛出 sql 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com