views:

77

answers:

2

This should be a simple one, but I'm a beginner with C#.

Given a glossary list in the following format:

aptitude  
ability, skill, gift, talent

aqueous  
watery

arguably  
maybe, perhaps, possibly, could be

How can I parse this, and insert into a database table in the format:

TABLE: Term_Glossary
================================================
Term_Name  | Term_Definition                   |
================================================
aptitude   | ability, skill, gift, talent      |
------------------------------------------------
aqueous    | watery                            |
------------------------------------------------
arguably   | maybe, perhaps, possibly, could be|
================================================

Any help would be appreciated - thanks.

Update I realize the database structure is simple/inefficient - but really, the point of my question is the code to parse the kind of text found in the first example, using C#. Thanks.

+1  A: 

It looks to me like you would read the first line, save it to a variable, read the second line, save it to a second variable, then insert into the table where Term_Name = first variable, and Term_Definition = second variable.

So your logic would be like:

StreamReader SR;
string Term_Name;
string Term_Definition

SR = File.OpenText(filename);
Term_Name = SR.ReadLine();
while(Term_Name != null)
{
    Term_Definition = SR.ReadLine();
    // make your database call here to insert with these two variables.  I don't know what DB you are using.
    Term_Name = SR.ReadLine();
}
SR.Close();
Bryan Denny
Bingo. That's an efficient way of doing it. Thanks.
Yaaqov
+2  A: 

It may seem more complex at first, but you'll find it a lot easier in the long-term to think in terms of two tables:

===========================================
Term_ID     | Term_Name                   |
===========================================
1           | aptitude                    |
2           | aqueous                     |
3           | arguably                    |
===========================================

===============================================
Definition_ID  | Term_ID   | Definition_Name  |
===============================================
1              | 1         | ability          |
2              | 1         | skill            |
3              | 1         | gift             |
4              | 1         | talent           |
5              | 2         | watery           |
6              | 3         | maybe            |
7              | etc.etc.etc

Perhaps even think if you can normalise this further by having one table of words with IDs and a table of associations.

pdr
- 1 The OP asked help on how he should parse the text not on how he should design the DB. The DB schema could be out of his control and is entirely irrelevant to the question.
AZ
Eitherway, I'll keep this in mind for my database design - thanks nonetheless.
Yaaqov
@AZ: Be kind... _Before_ he recently edited the question, the OP didn't acknowledge his/her understanding of the simplicity of the schema, nor stress the focus on file i/o and parsing. pdr's suggestion was therefore meant to be a helpful _hint_ to someone who appeared rather inexperienced (in C#, certainly, and, in general, possibly). Typically such hints are better place as a comment to the question (or to an answer, if applicable), but given the graphic nature and size of this hint, using an answer is fair enough.
mjv