I’m getting a string as an output parameter, and need to know what to set for the Size argument in the call to AddOutParameter.
I know I could just use some huge number, like int.MaxValue, but want to know best practices.
In SQL Server, the column is actually a uniqueidentifier type. The T-SQL statement being executed inserts a record, and then sets some output variables to the ID and GUID of the newly inserted records. This is the actual code I’m using, but with variable names changed.
database.AddOutParameter(cmd, "@someInt", DbType.Int32, 0);
database.AddOutParameter(cmd, "@someString", DbType.String, 0);
database.ExecuteNonQuery(cmd);
someInt = (int)database.GetParameterValue(cmd, "@someInt");
someString = database.GetParameterValue(cmd, "@someString").ToString();
When executed, I get the following error…
System.InvalidOperationException: String[2]: the Size property has an invalid size of 0.
So it’s obvious to me that you can’t just use a size of 0 with a string output parameter. You can do that with an Int32 output parameter, but I guess a string needs a valid size. So what is the best practice for setting the size? Can it just be a huge size without affecting performance at all? Can I just set it to int.MaxValue or something? Is there any constant that can be used here; (didn’t see any String.MaxValue – you can probably tell I’m new to C#, with a Java background).
Should I find out what the max size of a uniqueidentifier column is and set the size to that? What about if I’m doing the same thing for a VARCHAR or NVARCHAR column?
I wish the framework would just do it for me; I don’t want to specify a size for every string that I get as output. Anyone have any suggestions here for best practice?
I’ve read the posts below, as well as MSDN documentation, but there’s not really a best practices answer to this that I’ve found yet.
AddOutParameter – non-magic number way of finding length of DBType.Int32
As we found out you were using the wrong type for a UniqueIdentifer. You should use
DbType.Guidinstead of a string, but you raised other questions in the comments that I couldn’t answer in a comment and I wasn’t sure of so I needed to test.They are
I started by using
SqlCommandBuilder.DeriveParametersto find out what ADO.NET and SQL Server think it should be and then executed the Stored Procedure to see what our return values were.As expected the derived sizes matched the length field on the all the character types except for the max and the return values where the expected length. However looking at the max types and DbTypes we had some new questions to go with our first three.
What’s up with that AnsiString type and if we set it to DbType.String instead does it affect the output if we keep the same size? Answer: No it doesn’t, probably because .NET strings are unicode
Does increasing the
Paramater.Sizeaffect any of the non-max values? Answer: Yes but only char(10). It increases the output size by adding empty spaces.Does decreasing the
Paramater.Sizeaffect any of the non-max values? Yes it truncates the return valuesIs a size of -1 magic? Answer: Yes if you set the size to -1 it will return the values as though you had set them correctly
Test Code .NET 4.0 SQL Server 2008
SQL Code
C# Code
Outputs