Solved

Insert byte array into SQLServer

Posted on 2006-06-27
2
6,392 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
ID: 16990591
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
ID: 16990781
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

910 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

19 Experts available now in Live!

Get 1:1 Help Now