Skip to content

Commit d528baa

Browse files
committed
Throw exception for misuse. Fixes #308
1 parent 9d74878 commit d528baa

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/MySqlConnector/MySqlClient/MySqlCommand.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,19 @@ public override void Prepare()
7878
}
7979

8080
public override string CommandText { get; set; }
81-
public new MySqlConnection Connection { get; set; }
8281
public new MySqlTransaction Transaction { get; set; }
8382

83+
public new MySqlConnection Connection
84+
{
85+
get => m_connection;
86+
set
87+
{
88+
if (m_connection?.HasActiveReader ?? false)
89+
throw new MySqlException("Cannot set MySqlCommand.Connection when there is an open DataReader for this command; it must be closed first.");
90+
m_connection = value;
91+
}
92+
}
93+
8494
public override int CommandTimeout
8595
{
8696
get => Math.Min(m_commandTimeout ?? Connection?.DefaultCommandTimeout ?? 0, int.MaxValue / 1000);
@@ -244,6 +254,7 @@ private bool IsValid(out Exception exception)
244254

245255
static int s_commandId = 1;
246256

257+
MySqlConnection m_connection;
247258
MySqlParameterCollection m_parameterCollection;
248259
int? m_commandTimeout;
249260
CommandType m_commandType;

src/MySqlConnector/MySqlClient/MySqlConnection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ internal async Task<CachedProcedure> GetCachedProcedure(IOBehavior ioBehavior, s
323323

324324
internal MySqlSslMode SslMode => m_connectionSettings.SslMode;
325325

326+
internal bool HasActiveReader => m_activeReader != null;
327+
326328
internal void SetActiveReader(MySqlDataReader dataReader)
327329
{
328330
if (dataReader == null)

0 commit comments

Comments
 (0)