Skip to content

Using SqlDataAdapter.Fill on parameter-less stored procedure command with always encrypted w. secure encalves gives NullReferenceException #1105

@pigiax

Description

@pigiax

Description

If you are using a SQL server connection with Always Encrypted with secure enclaves, if you try to use SqlDataAdapter.Fill to fill a DataSet, using an SqlCommand which is a stored procedure without parameters it goes in error with NullReferenceException on BuildStoredProcedureStatementForColumnEncryption

To reproduce

Tested with "Microsoft.Data.SqlClient" Version "2.1.3" and "Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" Version "2.0.0"

private static void SqlError()
{
	using (var dbConnection = new SqlConnection("Data Source=[server];Initial Catalog=[db];Column Encryption Setting=Enabled;Attestation Protocol=AAS;Enclave Attestation Url=[secureEnclaveUrl];User ID=[user];Password=[pwd]"))
	{
		dbConnection.Open();
		DataSet ds = new DataSet("Results");

		IDbCommand dbCommand = dbConnection.CreateCommand();
		dbCommand.CommandText = "[YourStoredProcedure]";
		dbCommand.CommandType = CommandType.StoredProcedure;
		
		if(dbCommand.Parameters != null)
		{
			//This is a workarond which triggers the parameters lazy creation
			//https://github.com/dotnet/SqlClient/blob/3df7de613aecee3a8b5229333c84142e6e83ae9d/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs#L889
		}
		using (var da = new SqlDataAdapter(dbCommand as SqlCommand))
		{
			//This works
			da.Fill(ds);
		}

		dbCommand = dbConnection.CreateCommand();
		dbCommand.CommandText = "[YourStoredProcedure]";
		dbCommand.CommandType = CommandType.StoredProcedure;

		using (var da = new SqlDataAdapter(dbCommand as SqlCommand))
		{
			//NullReferenceException on BuildStoredProcedureStatementForColumnEncryption
			//https://github.com/dotnet/SqlClient/blob/3df7de613aecee3a8b5229333c84142e6e83ae9d/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs#L5839
			da.Fill(ds);
		}
	}
}

Expected behavior

It shouldn't be necessary to force lazy creation of Parameters when the command has no parameters

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions