开发者

T-SQL (varchar(max) vs CLR (string, SqlString, SqlChars)?

开发者 https://www.devze.com 2023-04-09 03:39 出处:网络
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# co

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.


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 }


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.


Try using length -1. Something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号