mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 17:23:27 -05:00
Fire connection changed event when USE statements are executed
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -145,6 +146,13 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
{
|
||||
await conn.OpenAsync();
|
||||
|
||||
if (conn.GetType() == typeof(SqlConnection))
|
||||
{
|
||||
// Subscribe to database informational messages
|
||||
SqlConnection sqlConn = conn as SqlConnection;
|
||||
sqlConn.InfoMessage += OnInfoMessage;
|
||||
}
|
||||
|
||||
// We need these to execute synchronously, otherwise the user will be very unhappy
|
||||
foreach (Batch b in Batches)
|
||||
{
|
||||
@@ -153,6 +161,28 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// "Error" code produced by SQL Server when the database context (name) for a connection changes.
|
||||
/// </summary>
|
||||
private const int DatabaseContextChangeErrorNumber = 5701;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for database messages during query execution
|
||||
/// </summary>
|
||||
private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
|
||||
{
|
||||
SqlConnection conn = sender as SqlConnection;
|
||||
|
||||
foreach(SqlError error in args.Errors)
|
||||
{
|
||||
// Did the database context change (error code 5701)?
|
||||
if (error.Number == DatabaseContextChangeErrorNumber)
|
||||
{
|
||||
ConnectionService.Instance.ChangeConnectionDatabaseContext(EditorConnection.OwnerUri, conn.Database);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a subset of the result sets
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user