How to use SqlDataAdapter and INSERTefficiently?

Hi,

I need to select a set of records from Table A and insert the records into
another table Table B.

How to efficiently do that with dataset, SqlDataAdapter? Or maybe
there are other methods?

Thanks a lot.

Jennifer
jfz2004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jj819430Commented:
ok I assume you are talking about doing it in the database.
There are a number of ways to do this... one is just
insert into TableB (fields) values (Select from TableA)
jj819430Commented:
so if you are selecting firstName from TableA and putting it into Name in TableB
insert into TableB (Name) values (Select firstName from TableA)
SweetsGreenCommented:
//the most effecient way is to do it all on the server with one insert statement
//no need for datasets or adapters

SqlConnection m_cn;  //maybe OleDBConnection instead of  SqlConnection if not SQL Server
SqlCommand m_cmd;//maybe OleDBCommand instead of  SqlCommand
string strSQL;

strSQL= "INSERT TableB (field1,field2) VALUES (Select field1,field2 from TableA)"
m_cn = new SqlConnection(DataConnectionString);       //where DataConnectionString is your connection string
m_cn.Open();
m_cmd = new SqlCommand(strSQL, m_cn);
m_cmd.ExecuteNonQuery();
m_cn.Close();
m_cmd=null;
m_cn=null;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

jfz2004Author Commented:
Thanks, SweetsGreen,

I think your solution is the most appropriate one. I will try yours first.

If it works, I will accept it as the right answer.

Thanks,

Jennifer
jfz2004Author Commented:
Hi,

I did the INSERT as :

INSERT INTO TABLEB (B_Date, B_Id) SELECT Top 21 TABLEA.Field1,
TABLEA.field2 from TABLEA

I can insert the 2nd field, which is B_ID, but the first field is empty.
I wonder what's wrong. The first field B_Date is of DateTime format.

Thanks,

Jennifer
SweetsGreenCommented:
What datatype is TABLEA.Field1????

try
INSERT INTO TABLEB (B_Date, B_Id) SELECT Top 21  cast( TABLEA.Field1 as datetime),
TABLEA.field2 from TABLEA
SweetsGreenCommented:
you should try

SELECT Top 21  cast( TABLEA.Field1 as datetime),
TABLEA.field2 from TABLEA

in query analyzer first to see if the select statement will insert the correct rows.....much easier to get the statement working first then put it into code
jfz2004Author Commented:
Thanks,

The datatype of TABLEA.Field1 is Datetime as well.

I am try to do the insert by hand now in Access SQL view to see how to make it work.

Jennifer
jfz2004Author Commented:
Oops, my TABLEA.Field1 is of Type Text.
jfz2004Author Commented:
I changed TABLEB.Field1 to be Text too and it works.
Thanks.

Jennifer
SweetsGreenCommented:
just FYI.

If a field is a date then you really should store it as a datetime....storing it as a text will not allow you to correctly sort (and other operations).

Glad I could help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.