Solved

Creating a flat file from a datatable using c#

Posted on 2012-04-03
5
681 Views
Last Modified: 2012-04-04
I am working on creating a flat file from a datatable. I have not been able to get this to work. Attached is my specification and dotnet code. Also could you take a look at my sql and see what am doing wrong. The text file is generated but the columns are not in the right position.

Here is the sql

select
substring(Year, 1,4) as "AWARD-YEAR",
 substring(Social_Security_Number,1,9)as SSNO ,
 substring("Alternate ID",1, 10)as "id-alt",
 substring(Last_Name_Applicant,1,16)as "NAME-L",
substring(First_Name_Applicant,1,11)as "NAME-F",
substring(Middle_Initial_Applicant,1, 1)as "NAME-M",
 substring("Fund External Name", 1,35)as "fund_external_name",
 substring("Award Amount",1,5)as AWARDAMT
 from studentinfo
code.txt
Specification.JPG
0
Comment
Question by:Sirdots
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 37804257
Unfortunately without knowing the schema for your table with all the data types it is difficult to know for sure why you "have not been able to get this to work"

SELECT  LEFT([Year], 4) [AWARD-YEAR],
        LEFT([Social_Security_Number], 9) [SSNO],
        Left([Alternate ID], 10) [id-alt],
        LEFT([Lt_Name_Applicant], 16) [NAME-L],
        LEFT([First_Name_Applicant], 11) [NAME-F],
        LEFT([Middle_Initial_Applicant], 1) [NAME-M],
        LEFT([Fund External Name], 35) [fund_external_name],
        LEFT([Award Amount], 5) [AWARDAMT],
        SPACE(909) Filler
FROM    studentinfo

Open in new window


It might be helpful if you could share with us what you mean by 'not been able to get this to work".
0
 

Author Comment

by:Sirdots
ID: 37804457
Thanks acperkins. Attached is the structure of the table. what I mean by "have not been able to get this to work" is that the columns did not align in the appropriate places. The output should follow what is stated in the attached specification document. There were so many spaces after each column.
I also dont want the field names to show if the flat file is generated. The field names appear on the first line.
tablestructure.JPG
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37804521
It looks like all the columns are character data so it should return the right value.  As to the column headers, I don't do C#, but I suspect this is your culprit:
for (i = 0; i < dt.Columns.Count - 1; i++)                
        {  sw.Write(dt.Columns[i].ColumnName + " \t "); 
        }                
sw.Write(dt.Columns[i].ColumnName);                
sw.WriteLine();

Open in new window

0
 

Author Comment

by:Sirdots
ID: 37806237
I think I got it to work now. I will test now.
0
 

Author Comment

by:Sirdots
ID: 37807586
I will close this now. Thanks.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now