Spyros Prantalos

When we use the SqlParameter.SqlParameter(string, object) constructor in order to create parameters for an SqlCommand, the SqlDBType for the object value is inferred from Microsoft .NET Framework type of the object. This mechanism is pretty clear and makes sense. However, is there any particular reason for which ADO.NET will always infer nvarchar for all object values of type string

Thank you




Re: .NET Framework Data Access and Storage String conversion in SqlParameter

ericrtodd

Are you manually creating the parameters or are you deriving them with SqlCommandBuilder.DeriveParameters



Re: .NET Framework Data Access and Storage String conversion in SqlParameter

Spyros Prantalos

Manually creating them : SqlParameter param = new SqlParameter("@XXX", thisVariable). So the question is why ADO.NET by default converts .NET strings to SQL nvarchar




Re: .NET Framework Data Access and Storage String conversion in SqlParameter

ericrtodd

I've used SqlParameters with a variety of string data types (e.g. nvarchar, varchar, char) and I've found that the values are marshaled from the System.String data type to whatever the required string data type is for the command. I've had the underlying table schema change on me many times without having to modify any code.



Re: .NET Framework Data Access and Storage String conversion in SqlParameter

Spyros Prantalos

After some search I found the following, which makes sense 100%: " The SqlParameter class will infer the SqlDbType from the object value specified if it is not specified explicitly. A System.String is always interpreted as SqlDbType.NVarChar as all .NET strings are Unicode. "