I have an issue with this code in my MSSQL Data Access manager. Sometimes the returned records are not the expected ones. The DataReader columns come from the good table but the fields seems to become from another request.
This behavior appears when I got lot of simultaneous requests on the same table and the result is moreless like 'select A retrun B, select B return D, select C retrun Z,...'
public bool Read(string ID)
{
bool _return = false;
System.Data.SqlClient.SqlConnection Connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
try
{
string Query = "Select * From Sounds Where ID = '" + ID + "'";
System.Data.SqlClient.SqlDataReader DataReader;
using (System.Data.SqlClient.SqlCommand Command = new SqlCommand())
{
Command.Connection = Connection;
Command.CommandText = Query;
Connection.Open();
DataReader = Command.ExecuteReader();
}
if (DataReader.Read())
{
FillClass(DataReader);
_return = true;
}
else
{
_return = false;
}
if (! DataReader.IsClosed) DataReader.Close();
}
catch (Exception e)
{
_return = false;
}
finally
{
if ((Connection != null) && (Connection.State == ConnectionState.Open)) {
Connection.Close();
}
}
return _return;
}
I don't understand SQL 2012 return a bad record or the .Net CLR mismatch the SQL pipes...
Thank for help. -Alex
Exception; d) useusingstatements to dispose of the connection, command and reader.if (DataReader.Read())advances the reader to 1 record. Then you callFillClass(DataReader);. If you callDataReader.Read()again insideFillClass, you will miss the first record. Check that you don't have that mistake.