Click here to Skip to main content
13,086,662 members (55,963 online)
Rate this:
Please Sign up or sign in to 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 2-May-13 0:02am

1 solution

Rate this: bad
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 each iteration of the loop insert the values something like:

insert into myTableName values( nameVariable, ageVariable, placeVariable)
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2017
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