Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pigiax opened this issue Jun 8, 2021 · 4 comments · Fixed by #1115
Assignees

Comments

@pigiax
Copy link

pigiax commented Jun 8, 2021

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

@Wraith2
Copy link
Contributor

Wraith2 commented Jun 8, 2021

@cheenamalhotra

@cheenamalhotra
Copy link
Member

Hi @pigiax

Would appreciate your feedback on the changes from PR #1115.
Please try and let us know if you have any more concerns.

Build Artifacts

@pigiax
Copy link
Author

pigiax commented Jun 22, 2021

Hi @cheenamalhotra ,
on next days I will give it a try. Looking at the code seems that it's going to work.
I will let you know.
Thanks

@pigiax
Copy link
Author

pigiax commented Jul 5, 2021

Hi @cheenamalhotra , I've just tested it and it works like expected.
We're waiting for next release when it's going to be released.
Thanks

@pigiax pigiax closed this as completed Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants