I am studying about the BLOB in SQL Server. The data types allowed for BLOB storage in SQL Server are VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX).
As I know, VARCHAR(MAX) or NVARCHAR(MAX) is suitable to store text data whereas images can be stored in NVARCHAR(MAX) columns.
Based on the theory, is it possible for me to store both text data and images in one BLOB?
As Dale K said, you would need to define some kind of "schema" for it, but you could store both in VARCHAR(MAX) by converting the images to a base64 encoded string, then storing as:
Text=<text>
Image=<base64 string>
Then, when reading the data, you can parse for "Text=" or "Image="
Related
Can i store image and text on one data type (one column) in sql server. if yes, how can i retrieve based on content type image or text. i want use same column to store image or text (one at a time).
You can use Varbinary Datatype to Store image as a byte datatype and it can use as a name
If you encode your image to some plain text format this should work -- BASE64 maybe as a starting point. Encapsulating your data with some kind of start string and end string would allow your application to split them again later.
I would recommend not to do that. On most of DBMS there are special column types available for storing large binary data (I assume you are referring to jpg, png, bmp etc not svg).
I plan to use JSON.NET to serialize and deserialize objects to and with the json string store it in the database.
What datatype of the SQL Server 2008 should be the best to store it?
nvarchar(max)?
varchar(max)?
Will your json be multilingual? If yes - go with nvarchar. If not - go with varchar, it uses less storage space and is more efficient in terms of performance.
You should probably use nvarchar(max).
http://www.ietf.org/rfc/rfc4627.txt
The preferred encoding for JSON is UTF-8, but UTF-16 and UTF-32 are also possible, so you may be getting data which would include wide characters.
On the other hand, you could also use varbinary(max), store the actual UTF-8 stream and save some space and processing since the UTF-8 stream wouldn't need to be converted to an actual string in .NET (i.e. no UTF-8 to Unicode internal .NET string).
I would not use varchar(max) because if you do have wide characters coming in then they could be misinterpreted by any queries which are not converting the UTF-8 which happens to be stored in varchar properly (assuming 8-bit varchar because you can also store multi-byte in varchar depending upon your database settings), and you wouldn't have to worry about converting the UTF-8 to the particular code page for your varchar. Again, nvarchar is Unicode so you wouldn't have to worry about any of that mess.
Are you going to do any operations on the JSON in SQL Server or just storage?
varchar by 10 times. Faster and better.
I am using FMDB wrapper to store data in sqlite. I am facing problem when I am trying to store the images which comes from server to the database. the image data is in byte array format and I am storing the bytearray into NSData and storing the NSData into the sqlite database.the column that I am storing it into is of type blob. when I do select * from tablename --I get the byte array which should not be the case.
Just to test I created a sample project and in that I tried to store an image which is in my resources folder into the sqlite database. when I did select * from tablename I got "firtname|?PNG" which means the image is stored in png format. now I need to know how I can convert the byte array that comes from server into an image format.
I strongly recommend you to store the images outside the database. Store only file names there.
I'm making a blog, article website so i decide to use NTEXT data type for blog and article contents. Until i see this
Important
ntext, text, and image data types will
be removed in a future version of
MicrosoftSQL Server. Avoid using these
data types in new development work,
and plan to modify applications that
currently use them. Use nvarchar(max),
varchar(max), and varbinary(max)
instead.
Fixed and variable-length data types
for storing large non-Unicode and
Unicode character and binary data.
Unicode data uses the UNICODE UCS-2
character set.
(http://msdn.microsoft.com/en-us/library/ms187993.aspx)
Im sure that blog and article contents gonna reach 4000 character limit if i use nvarchar(max).
What data type should i use at this case?
You should use nvarchar(max)/varchar(max) - that's current pair of text types.
When using these types you have no limit for field size (well, actually the limit is 2 Gb, but I don't think you'll hit it).
See MSDN for more details:
data types in SQL Server
nchar and nvarchar types
Can I safely store a .Net compressed memory stream (System.IO.Compression) in an SQLServer 2005 NVARCHAR(MAX) field? SQLServer 2008 is not an alternative.
Use VARBINARY(MAX) for binary data - VARCHAR(MAX) and NVARCHAR(MAX) are for character-data (strings).
You'd be better off using varbinary(max)
I would think varbinary(max) is more suitable. Remember that there is a max size of 2GB.
A Stream is just a pointer to data, so you cannot store the stream into SQL Server, you can store the data that this stream points to. As you mention the System.IO.Compression namespace I suppose you mean either DeflateStream or GZipStream which both contain binary data. The appropriate type to store binary data in SQL is VARBINARY(MAX).