Solved

Insert byte array into SQLServer

Posted on 2006-06-27
2
6,366 Views
Last Modified: 2007-12-19
I have the following problem:

I have a file which I need to insert into the database in a binary field (Image field is also a possibilty, but the file I need to insert is not an image) through a plain query with C#.

At the moment I got this:
INSERT INTO table(ID,File) VALUES('{0}','{1}');

where 0 is a Unique Identifier and 1, the binary data. But that isnt working, because it is giving me back errors. What am I doing wrong/is the correct syntax?

Ps. I can use either a byte[] or a memorystream for the data, but I prefer a byte[].
0
Comment
Question by:existenz2
2 Comments
 
LVL 4

Accepted Solution

by:
Nazermohideeen earned 250 total points
Comment Utility
Hi,


private int SaveToDB(string imgName, byte[] imgbin)
{
SqlConnection connection = new SqlConnection(yourConnectionString);
SqlCommand command = new SqlCommand( "INSERT INTO yourTable (img_name,img_data) VALUES ( @img_name, @img_data)", connection );

SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 );

SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );

connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();

return numRowsAffected;
}



NM
0
 
LVL 14

Author Comment

by:existenz2
Comment Utility
Not exactly what I was hoping for, but it solved my problem. Could have known that I should fix it this way ;) Thnx
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

8 Experts available now in Live!

Get 1:1 Help Now