简体   繁体   English

.NET / .NET Core和SQL Server连接不匹配的问题

[英].NET / .NET Core and SQL Server connection mismatch issue

I am developing C# application which is executing ~30 sql queries every second... I have multiple threads which do that. 我正在开发C#应用程序,它每秒执行约30个sql查询...我有多个线程可以做到这一点。 My problem is that when I try to query the sql server with SELECT .... it returns different results. 我的问题是,当我尝试使用SELECT ....查询SQL Server时,它返回不同的结果。

Example: If run both SELECT * FROM Users and SELECT * FROM Jobs at the same time from different threads i get responses like: 示例:如果从不同的线程同时运行SELECT * FROM UsersSELECT * FROM Jobs ,我将得到如下响应:

SELECT * FROM Users -> Job colums with Job values SELECT * FROM Users Job colums with Job values > Job colums with Job values

SELECT * FROM Jobs -> User colums with User values SELECT * FROM Jobs User colums with User values > User colums with User values

I am using Dapper like this: 我正在像这样使用Dapper:

using (var connection = new SqlConnection(_msSqlProvider.ConnectionString))
            {
                connection.Open();
                return connection.Query<User>(
                    @"SELECT JobId 
                        FROM Users
                        WHERE Id = @userId
                        ORDER BY Id ASC",
                    new {userId});
            }

My application logs look like (ie the query is returning not User model data): 我的应用程序日志如下(即查询未返回用户模型数据):

An exception occured while getting user's job: A parameterless default constructor or one matching signature (System.Int32 Id, System.Int32 UserId, System.Decimal Salary, System.DateTime UpdatedAt) is required for SampleApp.User materialization 实现用户作业时发生异常:SampleApp.User实现需要一个无参数的默认构造函数或一个匹配的签名(System.Int32 Id,System.Int32 UserId,System.Decimal Salary,System.DateTime UpdatedAt)。

My application is very different from this and complex but this should be good as example... 我的应用程序与此非常不同,而且很复杂,但这应该很好地作为示例。

Solution: If anyone reading this question is interested in what was the solution - enabling MARS and connection pooling resolved my issue... 解决方案:如果阅读此问题的任何人对解决方案感兴趣-启用MARS和连接池解决了我的问题...

Must use different sql reader or sql command for right solution. 正确的解决方案必须使用其他sql阅读器或sql命令。 Try this; 尝试这个;

        double vId;
        SqlDataReader dr = null;
        if (db.OpenDR(ref dr, string.Format("select JobId from dbo.Users where Id={0}", vUSERID)))
        {
            if (dr.Read()) vId = dr["JobId"].dToDouble();
            dr.Close();
        }
        return vId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM