I am developing windows project using c#. I have to import text file records into sqlserver 2005 db. My question is I want to read records from text file using student Idno.
I have an idea of read exact value from text file using strings that is finding student no:1001 value . I want to read every student record base开发者_运维技巧d on particular student no? not for 1001
so I want standered code to read particular records based on student number as my choice. plz help me.......
my text file like this
|-----------------------------------------------| | student no:1001 | | address:ongole,A.P, | | hyderabad,india. | | ----------------------------------------------| | stdid stdname class | | 1 raheem mca | | 2 sudheer mca | | ----------------------------------------------| | student no:1001 | | address:ongole,A.P, | | hyderabad,india. | -|-------------------------------------------- | | stdid stdname class | | 1 raheem mca | | 2 sudheer mca | | ----------------------------------------------| | student no:1002 | | address:ongole,A.P, | | hyderabad,india. | ------------------------------------------------|- | student no:1003 | | address:ongole,A.P, | | hyderabad,india. | -|---------------------------------------------- |
My code is
private void BtnImpstrm_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string f = openFileDialog1.FileName;
StreamReader sr = new StreamReader(f);
string s = sr.ReadToEnd();
MessageBox.Show(s);
sr.Close();
str =s.Split('\n');
for (int i = 0; i < str.Length; i++)
{
if (str[i].ToString().Trim().ToUpper().Contains("Student NO : 1001"))
{
string[] words = str[i].Split(' ');
string output = words[word no].ToString().Trim();
string rec1 =str[line no].ToString().Trim();
string rec2 = str[line no].ToString().Trim();
MessageBox.Show(output);
MessageBox.Show(rec1);
MessageBox.Show(rec2);
}
}
}
I think your best bet would be to use StreamReader to read line-by-line and if you encounter "student ..." or something like it, read the next 7 lines to complete your record. After doing this, you will have a collection of strings, each being a unique record in the file.
Then you can parse each record individually, by splitting the string on Environment.NewLine.
精彩评论