Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a console application(C#) which returns dynamic number of 3 different results after running a foreach loop. Now, I want to store those in a SQL table. Say the console returns values like,
Name = John, Age= 23, Place= California
Name= Roy, Age= 24, Place= Calcutta
Name= Raul, Age=23, Place= Spain.

Can I stored this in SQL table like:

Name Age Place
John 23 California
Roy 24 Calcutta
Raul 23 Spain
Posted

1 solution

create a table in sql with the columns you need. For example:

SQL
CREATE TABLE [dbo].[myTableName](
    [s_serial] [int] IDENTITY(1,1) NOT NULL,
    [name] [char](15) NULL,
    [age] [int] NULL,
    [place] [char](50) NULL,
) ON [PRIMARY]

GO


on each iteration of the loop insert the values something like:

SQL
insert into myTableName values( nameVariable, ageVariable, placeVariable)
 
Share this answer
 
v2
Comments
Abhik03 2-May-13 7:07am    
@Lee, But if we insert inside the foreach loop, it'll connect to the DB for each loop. Is there any way, where I can store the result say in a List and the insert at a time?
Lee Chetwynd 2-May-13 7:17am    
You could put them in an array first and then loop the array, inserting into the table. I dont know if that would be any better.

Or you could hold the sql insert text in a string variable and add to the string on each loop iteration. When its finished you could then execute the sql insert statement using the string variable.

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