Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help me to solve this problm
---------------------------------
i have to read string text file and split it by comma and finaly save it into database.....
ex:--In text file : suhas,kumar,55555,male,22.
i have to store this data into database...
then again read next line....


private void split()
{

    StreamReader rd1 = new StreamReader("c:\\temp1.txt");//string is suhas,kumar,8888888888,male.
    string str12 = rd1.ReadLine();
    string[] str1 = str12.Split(',');
    // splitmsg(str1);
    foreach (string part in str1)
    {
       //display array string
        MessageBox.Show(part.ToString());

    }
}


how can i use values in insert query..
Posted
Updated 11-Jul-11 3:45am
v2
Comments
#realJSOP 11-Jul-11 9:57am    
DO NOT REPOST YOUR QUESTION!

Easy! Assuming you want to put each string that you have split on a separate row, then you just need a loop:
foreach (string s in str1)
   {
   using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumnName) VALUES (@STR)", con))
      {
      cmd.Parameters.AddWithValue("@STR", s);
      cmd.ExecuteNonQuery();
      }
   }
You could do this with the SqlCommand outside the loop, and it would be a lot more efficient, but that is an exercise for the reader - this just shows the general principle.

BTW: you don't need to call ToString on objects you already know are strings: it doesn't do anything useful and just looks like you don't care...
 
Share this answer
 
But i need that in different columns...
like---> Fname Lname Phno Gender
suhas kumar 55555 male
ravi kumar 5555 male
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900