ehsan
ehsan

Reputation: 787

when sqlchars and when sqlstring?

I m developing a CLR application in c#. I encounter SqlChars type and SqlString. but i don't find the difference between them. When use SqlChars and when use SqlString?

thanks

Upvotes: 4

Views: 4357

Answers (3)

Bennett Dill
Bennett Dill

Reputation: 2915

I apologize for duplicating some of the data in the currently selected answer, but I wanted to try to provide a little clarification.

First, the short answer:

My recommendations for when to use SqlChars vs SqlString have to do with your intent. If you do not plan to modify the input string, use the SqlString. If you need to manipulate the input, use SqlChars.

And now some details about how I got there:

We can see from the documentation that SqlChars is a class and SqlString is a structure. Class vs structure is beyond the scope of this response, but suffice it to say structures are lighter weight. You can also see that SqlChars is mutable (you can change it without necessarily requiring a new memory allocation). I cannot find documentation referring to a size limitation to SqlString and I have not had any problems using data larger than VARCHAR(8000)/NVARCHAR(4000) with a SqlString. If you are having issues, please make sure your CLR code has MaxByteSize = -1.

The information below comes from the MSDN documentation of the SQL Types.

Classes

SqlChars is a mutable reference type that wraps a Char array or a SqlString instance.

Structures

SqlString - Represents a variable-length stream of characters to be stored in or retrieved from the database. SqlString has a different underlying data structure from its corresponding .NET Framework String data type.

Upvotes: 5

Costea
Costea

Reputation: 1

allow me to cite from mr. alastair: sqlchar is less demanding on system memory resources, because it doesn't require a block of contiguos nature, as sqlstring does.

Upvotes: -2

prema
prema

Reputation: 427

-- SQL String is a sqltype in .net framework.It represents a variable-length stream of characters to be stored in or retrieved from the database.

-- SQLChar is a is a mutable reference type that wraps a Char array or a SqlString instance. SqlChar is a class. And what u found is right that sqlstring is limited to nvarchar(4000) and sqlchars is nvarchar(max). (But we can define the size, precision and scale of parameters and return types by using SqlFacet attribute.)

Upvotes: 4

Related Questions