9

I have a problem, that I can't solve. I'm using SQL Server 2005, C# CLR for using outer dll. The problem is at length of parameter. I need to use as function parameter type varchar(max). If at C# code I use string, SqlSring, I can't use T-SQL type varchar(max), just varchar(4000) of nvarchar(4000). I need to say, that can be situations, when I need to use more then 4000 symbols, so I need know, what C# type I need to use for varchar(max).

I have read a lot of articles, and several of them say, that for this I can use SqlChars. But! I have manipulations with strings. How can I have actions with string or SqlString and then convert to SqlChars? (It is possible SqlChars.ToString() or SqlChars.ToSqlString()).

I didn't find any C# code for this.

4
  • A varchar(max) string that's using its maximum capacity will not be loadable into a CLR string, since it will exhaust the address space. Can you be more explicit about what actions you want to perform against these strings? Commented Sep 28, 2011 at 8:27
  • I convert any SQL type to varchar and send this value to the outer dll. Than, do manipulations with it and return to SQL Server. Now I use SQL/vchar(4000) and C#/string. I need more than 4000 symbols. Commented Sep 28, 2011 at 8:56
  • @Damien_The_Unbeliever: I was under the impression both SQL Server's varchar(max) as well as a .NET string instance could be up to 2 GB in size.... Commented Sep 28, 2011 at 9:41
  • 1
    @marc_s - as it turns out, a varchar(max) variable can contain more than 2GB. But I was mostly working from the other side. Certainly under 32 bit CLR, you can't actually allocate a 2GB object, and I think you can struggle even under 64 bit. Commented Sep 30, 2011 at 16:26

3 Answers 3

13

What you need to do is add SqlFacet(MaxSize = -1) attribute to parameter:

[return: SqlFacet(MaxSize = -1)]
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true)]
public static SqlString YourSqlFunction([SqlFacet(MaxSize = -1)]SqlString sourceSS)
{ return new }
Sign up to request clarification or add additional context in comments.

1 Comment

Correct! This will cause VS to create correct script automatically!
4

I found the answer. Not just only me asked about this question. I have to read posts more attentively...

Can read here http://social.msdn.microsoft.com/Forums/is/sqlnetfx/thread/391ebb98-e1b5-47fc-a72d-9f1e39829e3a The problem of CLR compatibility solves not very difficult.

All time I wanted use varchar(max) and tried to use C# types string, SqlString, SqlChars. Need to use T-SQL nvarchar(max), and you can use any of C# types string, SqlString, SqlChars!

Of course nvarchar taking up more space than varchar at two times.

1 Comment

SQLCLR does not allow for VARCHAR, whether it is 1 - 4000 or MAX. NVARCHAR is the only string type that the SQLCLR API passes through (well, I guess object / SQL_VARIANT can also pass a string of up to 4000). If you pass in VARCHAR values, they are implicitly converted to NVARCHAR. Please do not use, or suggest using, string as a SQLCLR input or output param type. For more info, please see my series, Stairway to SQLCLR, on SQL Server Central (free registration is required for that site).
0

Try using length -1. Something like this:

cmd.Parameters.Add("@param", SqlDbType.VarChar, -1).Value = "hdfiophdopigherog";

2 Comments

I can't try this, because I don't have SqlCommand... Just input parameter - manipulation with it - out parameter. And I need IN/OUT parameters will be SqlChars
While -1 is technically correct, this answer is still irrelevant to the question. This answer is dealing with a SqlConnection API and not the SQLCLR API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.