Solved

What is the maximum size for a string in C#?

Posted on 2010-11-09
7
412 Views
Last Modified: 2012-06-22
I have a comment field which, in the database, is defined as varchar(MAX).

Is string the right type? Or should I use something different?

Thanks,
newbieweb
0
Comment
Question by:newbieweb
7 Comments
 
LVL 5

Expert Comment

by:federicomarinucci
ID: 34092791
varchar has 255 chars
0
 
LVL 5

Expert Comment

by:federicomarinucci
ID: 34092810
Sorry I was meaning up to 8000 bytes, but it can grow:

http://www.fotia.co.uk/fotia/DY.13.VarCharMax.aspx
0
 
LVL 5

Accepted Solution

by:
federicomarinucci earned 125 total points
ID: 34092829
In SQL Server 2000 and SQL Server 7, a row cannot exceed 8000 bytes in size. This means that a VARBINARY column can only store 8000 bytes (assuming it is the only column in a table), a VARCHAR column can store up to 8000 characters and an NVARCHAR column can store up to 4000 characters (2 bytes per unicode character). This limitation stems from the 8 KB internal page size SQL Server uses to save data to disk.
To store more data in a single column, you needed to use the TEXT, NTEXT, or IMAGE data types (BLOBs) which are stored in a collection of 8 KB data pages that are separate from the data pages that store the other data in the same table. These data pages are arranged in a B-tree structure. BLOBs are hard to work with and manipulate. They cannot be used as variables in a procedure or a function and they cannot be used inside string functions such as REPLACE, CHARINDEX or SUBSTRING. In most cases, you have to use READTEXT, WRITETEXT, and UPDATETEXT commands to manipulate BLOBs.
To solve this problem, Microsoft introduced the VARCHAR(MAX),  NVARCHAR(MAX), and VARBINARY(MAX) data types in SQL Server 2005. These data types can hold the same amount of data BLOBs can hold (2 GB) and they are stored in the same type of data pages used for other data types. When data in a MAX data type exceeds 8 KB, an over-flow page is used. SQL Server 2005 automatically assigns an over-flow indicator to the page and knows how to manipulate data rows the same way it manipulates other data types. You can declare variables of MAX data types inside a stored procedure or function and even pass them as variables. You can also use them inside string functions.

Microsoft recommend using MAX data types instead of BLOBs in SQL Server 2005. In fact, BLOBs are being deprecated in future releases of SQL Server.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 34092851
String is indeed the correct type. CHAR and VARCHAR based columns hold string data.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 34092877
String length is an Int, so maximum lenght is a little over 2.1 billion characters.
0
 
LVL 2

Assisted Solution

by:Hossy
Hossy earned 125 total points
ID: 34093061
I believe this article addresses the very thing you are asking.

http://www.pcreview.co.uk/forums/thread-2539908.php

In short, the limit is 1GB, but that also depends on your memory fragmentation.

So, I should say the "theoretical" limit is 1GB, given you have 1GB of continuous memory space to allocate.
0
 

Author Closing Comment

by:newbieweb
ID: 34093106
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in page 3 46
Error in JQuery 5 39
ASP.net Run 3 sliders vertically as opposed to horizontally 9 20
rest webservice call over https via c# 6 25
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now