Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, 4 April 2014

Lesson learnt on nvarchar v.s SqlString

I had gone through an issue that related to nvarchar(max) data type. During resolving the issue, I had learnt below:

  1. The maximum length we can specify in an nvarchar type is nvarchar(4000) whereas the maximum for varchar is varchar(8000)
  2. As for nvarchar(max) the maximum data it can hold is 2GB chars.
  3. In C# for managed stored procedure, if to specify nvarchar(max), we need to declare the SqlString as [SqlFacet(MaxSize = -1)]SqlString. If only use SqlString, it will automatically default to nvarchar(4000) instead.
It was tricky to figure this out but the resolution on the issue has benefited me to learn more.

Monday, 5 November 2007

How do I convert a string to a byte array and vica-versa in VB.NET and C#?

Convert String to Byte[]

' VB.NET to convert a string to a byte array
Public Shared Function StrToByteArray(str As String) As Byte()

Dim encoding As New System.Text.ASCIIEncoding()
Return encoding.GetBytes(str)
End Function 'StrToByteArray

// C# to convert a string to a byte array.

public static byte[] StrToByteArray(string str){
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}

Convert Byte[] to String

' VB.NET to convert a byte array to a string:
Dim dBytes As Byte() = ...Dim str As StringDim
enc As New System.Text.ASCIIEncoding()
str = enc.GetString(dBytes)

// C# to convert a byte array to a string.

byte [] dBytes = ...string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);

Ref.: http://www.chilkatsoft.com/faq/DotNetStrToBytes.html