这是我的问题。我想获得一个用户页面列表,我不想使用limit.then我可以这样使用sql:

SELECT * FROM (SELECT @rowNum:=@rowNum+1 AS rowNum,User FROM (SELECT
@rowNum:=0) r,User where User.Name=@Name ORDER BY User.Name) AS a
WHERE rowNum>=1  AND rowNum<=10


.net代码如下:

connection.Query<T>(sql, new{Name="name"}, transaction, true,commandTimeout);


当我运行该程序时,它会引发异常:

message: "Object reference not set to an instance of an object.",
data: { },
innerException: null,
stackTrace: " at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread() at MySql.Data.MySqlClient.MySqlConnection.Abort() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) at Dapper.SqlMapper.<QueryImpl>d__124`1.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) at Infrastructure.Dapper.MysqlMapperExtension.QueryPaged[T](IDbConnection connection, Object condition, String table, String orderBy, Int32 pageIndex, Int32 pageSize, String columns, Boolean isOr, IDbTransaction transaction, Nullable`1 commandTimeout) in E:\Lofter\Code Source\NetCore\MermaidLoft.Alchemy\MermaidLoft.Alchemy\src\Extensions\Infrastructure.Dapper\MysqlMapperExtension.cs:line 312 at MermaidLoft.Alchemy.BaseDomain.UserDomain.UserQueryService.FindUsersForPage(String userName, Int32 pageIndex, Int32 pageSize) in E:\Lofter\Code Source\NetCore\MermaidLoft.Alchemy\MermaidLoft.Alchemy\src\Domains\MermaidLoft.Alchemy.BaseDomain\UserDomain\UserQueryService.cs:line 30 at MermaidLoft.Alchemy.QuickWeb.Controllers.HomeController.Mysql() in E:\Lofter\Code Source\NetCore\MermaidLoft.Alchemy\MermaidLoft.Alchemy\src\MermaidLoft.Alchemy.QuickWeb\Controllers\HomeController.cs:line 40",
helpLink: null,
source: "SapientGuardian.MySql.Data",
hResult: -2147467261


有解决这个问题的主意吗?希望有人能帮助我,谢谢!

最佳答案

如果您要检查用户名不是特殊字符或类似>>的内容,那么可以在字符串中为每个字符进行循环并将char转换为int,然后将char转换为ascii并通过if检查

string x="aaaa";
foreach(char a in x)
{
     int s=(int)a;
    //(s>=65&&s<=90) capital
    //(s>=97&&s<=122) small
    if((s>=65&&s<=90) || (s>=97&&s<=122))
               //do your code

}

关于c# - Dapper for MySQL Query <T>(字符串sql)sql具有自定义变量(@rowNum)包含特殊字符@,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39461739/

10-14 21:00
查看更多