How to store image data into SQL Server 2005 - sql-server-2005

I have to store System.Drawing.Image ImageObject into the database. I have tried with byte[] like I do ImageObject convert to byte[] and than store to database but while storing the length of converted byte array is greater than max limit of binary data type of SQL.
How I store my data into database?

Change the datatype to varbinary(max). binary has a limit of 8000 bytes. Note: don't use the image datatype, unless you plan on using SQL Server 2005 forever (it is deprecated).

Related

Insert BLOB data into SQLite database field using Matlab and SQL query. Any idea?

I need to store binary data in single database field (DB is SQLite, it's important!). Binary data is a some one-dimensional array.One of the best way to do that is BLOB data insertion. As far as I know Matlab doesn't consist methods for BLOB data processing. How can I do that using Matlab environment? Maybe using raw SQL query?
In a raw SQLite query, a blob can be written as a x-prefixed string, with each byte representeded by a two-digit hexadecimal number:
INSERT INTO MyTable(BlobColumn) VALUES(x'0123AB');
To return a blob as text from a query, you can use the quote() function, which uses the same format:
SELECT quote(BlobColumn) FROM MyTable; --> returns the string "x'0123AB'"
It might be a better idea to use some other SQLite3 driver for Matlab, e.g., https://github.com/kyamagu/matlab-sqlite3-driver, http://jaewon.mine.nu/jaewon/2015/06/17/another-sqlite-interface-for-matlab/.

Moving Data From SQL Server To Oracle -- Character vs Bytes

I am in the process of moving data from SQL Server to Oracle. I am having to do this using C# code we have written. (Long story but has to do with corporate standards, so no SSIS or other utilities allowed)
The question I have is when I have a field that is NVARCHAR(200) on SQL Server and in oracle it is NVARCHAR(200). I understand that on Oracle the 200 represents 200 bytes. My question is how can I move data from the SQL Server where the field has all 200 characters populated. The problem is that the 200 character in SQL Server is more than 200 bytes.
In the process I am reading the data form SQL Server, storing it into a string array, and then using Array Binding (Oracle Data Access) to push the data to Oracle. It all works fine, however, when I have a field that is fully populated in SQL Server it has more than the max allowed bytes for the same field definition in Oracle.
Is there an easy way to check the byte size of the string from SQL Server and see if it is more than 200 bytes, and if so, truncate it so that only 200 bytes are moved across? (For what I am doing, truncation of the data is ok)
I understand that on Oracle the 200 represents 200 bytes.
That is true for VARCHAR but not for NVARCHAR
Quote from the manual
The NVARCHAR2 data type is a Unicode-only data type. When you create a table with an NVARCHAR2 column, you supply the maximum number of characters it can hold. [...] Width specifications of character data type NVARCHAR2 refer to the number of characters.
(Emphasis mine)
So for NVARCHAR you should be fine.
For VARCHAR2 you can indeed specify the length in bytes or characters - but even there the number denotes the default setting which can be changed anytime.

Keeping bytes in the SQL Server database

How can I keep a byte array in a SQL Server database?
I've tried putting it in a NVARCHAR, but it keeps the bytes as a string while I need to keep them as integers.
Is there a "Byte" type I can use?
varbinary

difference between varchar(500) vs varchar(max) in sql server

I want to know what are pros and cons while using varchar(500) vs varchar(max) in terms of performance, memory and anything else to consider?
Will both use same amount of storage space?
Is the answer differ in case of sql server 2000/2005/2008?
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.
Credit: http://www.teratrax.com/articles/varchar_max.html
In SQL Server 2005 and SQL Server 2008, The maximum storage size for VARCHAR(MAX) is 2^31-1 bytes (2,147,483,647 bytes or 2GB - 1 bytes). The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. Since each character in a VARCHAR data type uses one byte, the maximum length for a VARCHAR(MAX) data type is 2,147,483,645.
Full Interesting read for you: http://www.sql-server-helper.com/faq/sql-server-2005-varchar-max-p01.aspx
Reference: http://msdn.microsoft.com/en-us/library/ms143432.aspx
A VARCHAR(MAX) column will accept a value of 501 characters or more whereas a VARCHAR(500) column will not. So if you have a business rule that restricts a value to 500 characters, VARCHAR(500) will be more appropriate.

Why is maximum length of varchar less than 8,000 bytes?

So I have a stored procedure in a SQLServer 2005 database, which retrieves data from a table, format the data as a string and put it into a varchar(max) output variable.
However, I notice that although len(s) reports the string to be > 8,000, the actual string I receive (via SQLServer output window) is always truncated to < 8,000 bytes.
Does anybody know what the causes of this might be ? Many thanks.
The output window itself is truncating your data, most likely. The variable itself holds the data but the window is showing only the first X characters.
If you were to read that output variable from, for instance, a .NET application, you'd see the full value.
Are you talking about in SQL Server Management Studio? If so, there are some options to control how many characters are returned (I only have 2008 in front of me, but the settings are in Tools|Options|Query Results|SQL Server|Results to Grid|Maximum Characters Retrieved and Results to Text|Maximum number of characters displayed in each column.
The data is all there, but management studio isn't displaying all of the data.
In cases like this, I've used MS Access to link to the table and read the data. It's sad that you have to use Access to view the data instead of Management Studio or Query Analyzer, but that seems to be the case.
However, I notice that although len(s) reports the string to be > 8,000
I have fallen for the SQL Studio issue too :) but isn't the maximum length of varchar 8,000 bytes, or 4,000 for nvarchar (unicode).
Any chance the column data type is actually text or ntext and you're converting to varchar?