Click here to Skip to main content
14,640,670 members
Rate this:
Please Sign up or sign in to 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!
Rate this:
Please Sign up or sign in to vote.

Solution 1

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...
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

But i need that in different columns...
like---> Fname Lname Phno Gender
suhas kumar 55555 male
ravi kumar 5555 male
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100