Solved

Insert byte array into SQLServer

Posted on 2006-06-27
2
6,421 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple SQL query from two tables 13 54
Querying data from 3 SQL tables 2 32
What's wrong with this T-SQL Foreign Key? 7 45
Return 0 on SQL count 24 30
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

773 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