Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
hi frnds,
 
am working on asp.net project.using C# SqlServer 2005
 
I have 2 tables in SqlServer database
 
I want to move all the data from table A to Table B when submit button clicks on my asp.net webpage.
 
Please can u help me how to do this ?
 
Thanks in advance
Posted 12-Apr-13 21:09pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

query="INSERT INTO Table2(Tb2Column1, Tb2Column2)
  SELECT Tb1Column1, Tb1Column2 FROM Table1"
SqlCommand cmd = new SqlCommand(query, con);
 
NOTE:
1. Data Type of column should match with data type of column.
2. You can select desired number of columns.
3. Column with Primary key can not be copied.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

CREATE TABLE YOUTABLE_B(ID BIGINT,NAME VARCHAR(200),ADDRESS VARCHAR(500))
CREATE TABLE YOUTABLE_A(ID BIGINT,NAME VARCHAR(200),ADDRESS VARCHAR(500),
					DELETED_TIME SMALLDATETIME NOT NULL DEFAULT GETDATE())
 
INSERT INTO YOUTABLE_B VALUES(1,'YOUR_NAME','YOUR_ADDRESS')
INSERT INTO YOUTABLE_B VALUES(2,'YOUR_NAME','YOUR_ADDRESS')
 
Create TRIGGER dbo.TRG_YOU_TRG_NAME
ON dbo.YOUTABLE_B
FOR DELETE
AS
BEGIN
   INSERT INTO dbo.YOUTABLE_A
   (ID,NAME,ADDRESS,DELETED_TIME)
   SELECT ID,NAME,ADDRESS,getdate()
   FROM deleted;
END;
GO
 
DELETE FROM YOUTABLE_B WHERE ID = '1' -- or delete all. 
--on submit button you write ^ above Query 
-- data of table B will be inserted in TAble B automatically when trigger fired on it

select * from YOUTABLE_B
select * from YOUTABLE_A
 
Hope so it will be usefull for you
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100