Click here to Skip to main content
14,836,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C#,,VS 2005 and sql server 2005

i am using the following insert query to insert image file into sql server 2005.

SqlCommand command = new SqlCommand("insert into MyImageTable(ImageName,ImageFile,MIMEType,ImageId,MYID) values (@img_name,@img_file,@img_type,@img_id,@my_ID)", connection);

Can anybody help how i can insert the same image file into another table by modifying the above query. i want to insert the image file and i have given datatype as image in sql server 2005.

thanks in advance.
with regards
Updated 2-Feb-21 7:45am

Repeat the command and with new table name.
INSERT statement cannot insert to more then 1 table. The other option is to stored procedure and have stored procedure insert into 2 tables.
karan joshua 16-Jun-11 4:43am
oh Simple it is..i didn't get this idea..

Thank you very much..

any other posssibility using single query.?
Kim Togo 16-Jun-11 5:05am
No other possibility with single query. INSERT statement only support 1 table. In a stored procedure you can do statements and only call 1 procedure.
Uday P.Singh 16-Jun-11 5:41am
you can also use inert trigger on first table to insert record in second table
Kim Togo 16-Jun-11 6:18am
Yes you are correct Uday :-)
Why do you want to store the same image in two places? It is a big waste of space, and leads to the possiblity of the two getting out of step. Instead, why not create a table of images, with just the image and related info, and an ID, and refer back to that from both tables?
karan joshua 16-Jun-11 4:42am
actually it is application requirement..
Try this

let me explain suppose i have two tables 'abc' and 'xyz'

First of all i have created the following storedprocedure.

Create PROCEDURE procemp
@Empcode numeric,
@FName nvarchar(100),
@LName nvarchar(100),
@sex nvarchar(100),
@age numeric
Insert into abc(Empcode,FName, LName) values (@Empcode,@FName, @LName)
Insert into xyz(Empcode,sex,age) values (@Empcode, @sex,@age)

after creating the above procedure i executed this procedure by providing parameters as under

execute procemp 1,'a', 'b','male',28
karan joshua 16-Jun-11 6:29am
Thank you
if you want insert data into two table then
repeat your insert query

means your insert query will be fire two times on the single event.


on button click

insert into table1(stu_id,names,emailid) values (@stu_id,@names,@emailid)
insert into table2(stu_id,class,age) values (@stu_id,@class,@age)

i hope its help u.
Stored procedures, IMHO really fits in this scenario. My suggestion is to create a stored procedure where you can put the 2 insert statements and then call it from your SQLCommand. One advantage of doing such over executing the command twice is you are able to rollback the script incase something goes wrong in the process.
karan joshua 16-Jun-11 4:45am
i am not familiar with stored procedure...
walterhevedeich 16-Jun-11 4:51am
Learning how to use stored procedures wont take much time since you already have a background in creating SQL scripts. Its just a matter of getting to know the right syntax. There are plenty of examples available on the Internet if you only take advantage of Google's power. :)
karan joshua 16-Jun-11 4:52am
yes. sure. thank u for your valuable suggestions.
create triger after insert on the table MyImageTable
and write query in the trigger

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900