Skip to content

Commit aa56756

Browse files
Fix | Fix issue with SqlCommand OnStatementCompleted event not being triggered (#216)
1 parent e1e3be4 commit aa56756

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,11 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
27922792
cmd.InternalRecordsAffected = count;
27932793
}
27942794
}
2795+
// Skip the bogus DONE counts sent by the server
2796+
if (stateObj._receivedColMetaData || (curCmd != TdsEnums.SELECT))
2797+
{
2798+
cmd.OnStatementCompleted(count);
2799+
}
27952800
}
27962801

27972802
stateObj._receivedColMetaData = false;

src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTesting.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@
7676
<Compile Include="DDBasics\DDMARSTest\DDMARSTest.cs" />
7777
<Compile Include="ProviderAgnostic\MultipleResultsTest\MultipleResultsTest.cs" />
7878
<Compile Include="ProviderAgnostic\ReaderTest\ReaderTest.cs" />
79-
<Compile Include="SQL\AsyncTest\AsyncTest.cs" />
80-
<Compile Include="SQL\CommandCancelTest\CommandCancelTest.cs" />
79+
<Compile Include="SQL\AsyncTest\AsyncTest.cs" />
80+
<Compile Include="SQL\SqlCommand\SqlCommandCompletedTest.cs" />
81+
<Compile Include="SQL\SqlCommand\SqlCommandCancelTest.cs" />
8182
<Compile Include="SQL\ConnectionPoolTest\ConnectionPoolTest.cs" />
8283
<Compile Include="SQL\ConnectivityTests\ConnectivityTest.cs" />
8384
<Compile Include="SQL\DataBaseSchemaTest\ConnectionSchemaTest.cs" />
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
1212
{
13-
public static class CommandCancelTest
13+
public static class SqlCommandCancelTest
1414
{
1515
// Shrink the packet size - this should make timeouts more likely
1616
private static readonly string s_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { PacketSize = 512 }).ConnectionString;
@@ -139,7 +139,7 @@ private static void MultiThreadedCancel(string constr, bool async)
139139

140140
Task.WaitAll(tasks, 15 * 1000);
141141

142-
CommandCancelTest.VerifyConnection(command);
142+
SqlCommandCancelTest.VerifyConnection(command);
143143
}
144144
}
145145

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Data;
2+
using Xunit;
3+
4+
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
5+
{
6+
public static class SqlCommandCompletedTest
7+
{
8+
private static readonly string s_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { PacketSize = 512 }).ConnectionString;
9+
private static int completedHandlerExecuted = 0;
10+
11+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
12+
public static void VerifyStatmentCompletedCalled()
13+
{
14+
string tableName = DataTestUtility.GetUniqueNameForSqlServer("stmt");
15+
16+
using (var conn = new SqlConnection(s_connStr))
17+
using (var cmd = conn.CreateCommand())
18+
{
19+
try
20+
{
21+
cmd.StatementCompleted += StatementCompletedHandler;
22+
conn.Open();
23+
24+
cmd.CommandText = $"CREATE TABLE {tableName} (c1 int)";
25+
var res = cmd.ExecuteScalar();
26+
27+
cmd.CommandText = $"INSERT {tableName} VALUES(1)"; //DML (+1)
28+
res = cmd.ExecuteScalar();
29+
30+
cmd.CommandText = $"Update {tableName} set c1=2"; //DML (+1)
31+
res = cmd.ExecuteScalar();
32+
33+
cmd.CommandText = $"SELECT * from {tableName}"; //DQL (+1)
34+
res = cmd.ExecuteScalar();
35+
36+
cmd.CommandText = $"DELETE FROM {tableName}"; //DML (+1)
37+
res = cmd.ExecuteScalar();
38+
}
39+
finally
40+
{
41+
cmd.CommandText = $"DROP TABLE {tableName}";
42+
var res = cmd.ExecuteScalar();
43+
}
44+
}
45+
// DDL and DQL queries that return DoneRowCount are accounted here.
46+
Assert.True(completedHandlerExecuted == 4);
47+
}
48+
49+
private static void StatementCompletedHandler(object sender, StatementCompletedEventArgs args)
50+
{
51+
// Increment on event pass through
52+
completedHandlerExecuted++;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)