Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL list Console
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 2-May-13 1:02am
Abhik03368

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

create a table in sql with the columns you need. For example:
 
 
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:
 
insert into myTableName values( nameVariable, ageVariable, placeVariable)
  Permalink  
v2
Comments
Abhik03 at 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 at 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)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 2 May 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