An SQL
query that I'm executing from my application is returning different results than SQL Management Studio, specifically missing entire rows of data.
I've tried stripping the query way down to just "SELECT *
" from the relevant table with a single WHERE clause, this achieved the same result of the C#
version missing the same row each time.
A stripped down SQL
query (that still has the issue) looks like this
SELECT Token, Last_update, repeatno, Maxrepeatno, Dispwindowstart
FROM v_eps_prescriptions
WHERE Token='F69F4D-C82043-70379I'
The C#
version is
SqlCommand command = new SqlCommand(
$@"SELECT Token, Last_update, messages, repeatno, Maxrepeatno
FROM v_eps_prescriptions
WHERE Token='{UUID}'",
conn);
The SQL
version for this query will return 2 rows, C#
will only return 1 row.
I've been putting the data into a dataGridView to debug which is where I'm seeing just the single row.
Edit: As requested the full chunk of C# that performs the query and what outputs to a datagrid (which is temporary for debugging)
try
{
conn.ConnectionString = $"Data Source=[source];Initial Catalog=[Cat];Integrated Security=True";
conn.Open();
foreach (string UUID in ValidUUIDList)
{
try
{
DateTime dt = DateTime.Now.AddDays(1);
DateTime dtpast = dt.AddMonths(-6);
SqlCommand command = new SqlCommand(string.Format($@"SELECT Token, Last_update, messages, repeatno, Maxrepeatno
FROM [Table]
WHERE Token='{UUID}'
AND Dispwindowstart
BETWEEN '{dtpast.ToString("yyyy - MM - dd")} 00:00:00.000'
AND '{dt.ToString("yyyy-MM-dd")} 00:00:00.000'
OR Token='{UUID}'
AND Dispwindowstart IS NULL"),
conn);
using (SqlDataReader dataReader = command.ExecuteReader())
{
if (dataReader.Read())
{
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
dataGridView1.DataSource = dataTable;
}
}
}
catch (Exception err)
{
MessageBox.Show($"An error occured while processing UUID {UUID}. \n\n{err.Message}");
}
}
}
catch (Exception err)
{
MessageBox.Show($"An error occured while connecting to the Nexphase database. \n\n{err.Message}");
}