What do you call "very large"? And what is the string? CLOB? BLOB? xml?
I suspect you should be using things like ExecuteReader()
, which (via IDataReader
) exposes methods for reading such columns in chunks:
using (var reader = cmd.ExecuteReader(
CommandBehavior.SequentialAccess)) {
char[] buffer = new char[8040]; // or some multiple (sql server page size)
while (reader.Read()) {
long dataOffset = 0, read;
while((read = reader.GetChars(colIndex, dataOffset, buffer, 0, buffer.Length)) > 0) {
// process "read"-many chars from "buffer"
dataOffset += read;
}
}
}
Obviously with xml you might want an XmlReader
via cmd.ExecuteXmlReader()
.
Updated re LINQ comment (now deleted):
To use IDataReader
directly from LINQ-to-SQL, I expect the closest you can get is ctx.GetCommand()
, passing it a query. You would then use ExecuteReader
or ExecuteXmlReader
as above. I don't know much about EF...
If you give an example of the type of query that is failing, there might be some tricks possible - for example, if you are filtering or selecting subsets of the xml, there are things you can do in SQL/XML - perhaps in a UDF called via LINQ-to-SQL.