简体   繁体   中英

.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. My problem is that when I try to query the sql server with SELECT .... it returns different results.

Example: If run both SELECT * FROM Users and SELECT * FROM Jobs at the same time from different threads i get responses like:

SELECT * FROM Users -> Job colums with Job values

SELECT * FROM Jobs -> User colums with User values

I am using Dapper like this:

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

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...

Must use different sql reader or sql command for right solution. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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