Create data for Image column in SQL Server - sql-server

I need to create data to test the limit of Image Datatype in SQL Server. It is able to hold variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes. I am using the following directly in SQL Server:
INSERT INTO Employees (Id, Name, Photo)
SELECT 10, 'John', BulkColumn FROM Openrowset( Bulk 'E:\photo.jpeg', Single_Blob) as EmployeePicture
My question is, how do I ensure that the max size (2,147,483,647 bytes) is being inserted into the column? Do I just find an image of size 2G and insert it into the column? Is there another way to do this?
Thanks in advance.

Most JPEG viewers completely ignore the bytes after the image data. The following code will tack on enough bytes to make the file whatever size you want. I tested it and the resulting image opened in the Windows image viewer, paint.net, and Photoshop. You should be able to create test files with it...
long desiredSize = 200000;
byte[] buffer = new byte[1];
string filename = #"c:\temp\sample.jpg";
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
{
fs.Seek(desiredSize - fs.Length - 1, SeekOrigin.End);
fs.Write(buffer, 0, 1);
fs.Close();
}

Related

pyodbc - Image from Microsoft SQL Server writing in binary mode error

Hopefully I'm missing something obvious here...
Whenever I try and use pyodbc to convert and save images from a Microsoft SQL Server, I generate files that are not recognised as images files.
Python code:
cursor = conn.cursor()
q = "SELECT top 1 * from reactorA.dbo.ProcessedImageData"
records = cursor.execute(q).fetchall()
for row in records:
data = row.AnalysedImage
with open('test.bmp','wb') as f:
f.write(data)
When I open the newly created image file it's just repeating ΓΏ symbols
When I view the record it seems to be in the correct bytes format
b'\xff\xff\xff .......
Any help on fixing this will be greatly appreciated.

Save base64 encoded string image data in Image type column in SQL Server with vbscript

I have a column named IMGDATA of type Image in my MyImages in SQL Server server
I have a string variable named MyImageStringBase64Encoded that contains a base64 encoded image. I m trying to save this image to the database
Set Command1 = Server.CreateObject ("ADODB.Command")
Command1.ActiveConnection = MyConnection_STRING
Command1.CommandText = "INSERT INTO MyImages (IMGNAME,IMGDATA) VALUES (?,?)"
Command1.Parameters(0) = "My Image Name"
Command1.Parameters(1) = MyImageStringBase64Encoded
Command1.CommandType = 1
Command1.Execute()
The code above will save corrupted image data in the db. Maybe I have to save bytes?
possibly related but not sure how to apply them
https://stackoverflow.com/a/24925145/934967
MS Access: Sending image as varbinary to stored procedure
I 'm new to vbscript

C# Filestream to SQL Server database

I want to create a file in SQL Server from a string. I can't figure out how to put it into the database. After reading it seems it has someting to do with filestream. If so then once the stream is created then how do I put that to my DB as a file?
FileStream fs1 = new FileStream("somefilename", FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.WriteLine("file content line 1");
writer.Close();
What I am trying to achieve is create a file from a string. I believe that my db is already set up for files. As we have a savefile method that works:
HttpPostedFile file = uploadedFiles[i];
if (file.ContentLength < 30000000)
{
//DOFileUpload File = CurrentBRJob.SaveFile(CurrentSessionContext.Owner.ContactID, Job.JobID, fileNew.PostedFile);
DOFileUpload File = CurrentBRJob.SaveFile(CurrentSessionContext.Owner.ContactID, Job.JobID, file, file.ContentLength, CurrentSessionContext.CurrentContact.ContactID);
DOJobFile jf = CurrentBRJob.CreateJobFile(CurrentSessionContext.Owner.ContactID, Job.JobID, File.FileID);
CurrentBRJob.SaveJobFile(jf);
}
What I want to do is: Instead of the user selecting a file for us to save to the DB. I want to instead create that file internally with strings and then save it to the db.
Create a a column type of any one below. Use ADO.NET SqlCommand write it to database.
varbinary(max) - to write binary data
nvarchar(max) - for unicode text data (i mean if text involves UNICODE chars)
varchar(max) - for non unicode text data

How can i retrive my image from sql server 2008 using matlab

i had inserted image in sql server using matlab but when i am retrieving that image i get this
[845941x1 int8] as my output so please tell me how will i get my original image using this output........ or any other way to retrieve that image....
this is my code to insert image:
// conn = database('test','prashant','');
//pes = exec(conn,'insert into filee select ''jpg'' as filetype,* from
//openrowset (bulk ''D:\file\index.jpg'' , single_blob) as x ');
//close(conn);
code to retrieve is:
// conn = database('test','prashant','');
// pess = exec(conn,'select * from filee');
//pess = fetch(pess);
// pess.data[1,3];
// close(conn);
please tell me the code to retrieve my image back.....
It is very unclear what you want, if you want to get the information from a table, perhaps something like this can do the trick:
exec(conn,'select * from filee')
I think this vector is the raw database representation of the image... Try to reshape it to it's original size and then to draw it:
image(reshape(int8array,height,width));
But since I just see that your dimension of the int8 array is a prime number I wonder whether it can be reshaped in an image...

How to find the size of data returned from a table

I have a table the holds snapshots of data. These snapshots are all tagged with 'jan2010' or 'april2011'. All the snapshots will grow exponentially over time and I wanted to see if I could forecast when we'd need to upgrade our storage.
Is there any way to
select monthlysnapshot, sum(size)
from tblclaims_liberty
group by monthlysnapshot
order by monthlysnapshot desc
What am I missing to get the size of the data returned? Is there a system function I can call?
EXEC sp_spaceused 'tablename'
This will return a single result set that provides the following information:
Name - the name of the table
Rows - the number of rows in the table
Reserved - amount of total reserved space for the table
Data - amount of space used by the data for the table
Index_Size - amount of space used by the table's indexes
Unused - amount of usused space in the table
Is it what you are looking for?
C# Getting the size of the data returned from and SQL query
Changed:
EXEC sp_spaceused 'tablename'
If you can do this in your code then in C# (Change code to whatever lang you are using)
long size = 0;
object o = new object();
using (Stream s = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(s, o);
size = s.Length;
Code copied from: How to get object size in memory?
I think you can Insert the selected data into a table via "select * into newtable from table" and get the size of that newly created table.

Resources