anilleuss
anilleuss

Reputation: 47

I want to insert data without a few words from text file

I would like to save certain text to my database. Instead of entries containing the text name=joe, it should simply read joe.

this is my text file words like that

name=joe|surname=clark|phone=23132131|address=jdsakldjakldja|date=11.02.2015 14:30:45
name=betty|surname=ugly|phone=32112121|address=dsadaewqeqrsa|date=11.02.2015 14:30:45

This is my codes

string connStr = @"Data Source=ANLZ\SQLEXPRESS;Initial Catalog=testdb; Trusted_Connection=True;";
string createQuery = "INSERT INTO tbl_test(name,surname,phone,address,date) VALUES(@name,@surname,@phone,@address,@date)";

SqlConnection conn;
SqlCommand cmd;
string[] importfiles = Directory.GetFiles(@"C:\Users\An\Desktop\", "test.txt");

            using (conn)
            {
                using (cmd = new SqlCommand(createQuery, conn))
                {

                    cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@surname", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 50);
                    cmd.Parameters.Add("@address", SqlDbType.NVarChar, 200);
                    cmd.Parameters.Add("@date", SqlDbType.DateTime);

                    foreach (string importfile in importfiles)
                    {
                        string[] allLines = File.ReadAllLines(importfile);
                        baglanti.Open();

                        for (int index = 0; index < allLines.Length; index++)
                        {
                            string[] items = allLines[index].Split(new char[] { '|' });
                            //new char[] {'|'}
                            cmd.Parameters["@name"].Value = items[0];
                            cmd.Parameters["@surname"].Value = items[1];
                            cmd.Parameters["@phone"].Value = items[2];
                            cmd.Parameters["@address"].Value = items[3];
                            cmd.Parameters["@date"].Value = items[4];
                            cmd.ExecuteNonQuery();
                        }
                        conn.Close();
                    }
                }
            }

enter image description here

I dont want to insert like that "name=joe". How can i save without "name=" words?

Upvotes: 1

Views: 53

Answers (4)

Micke
Micke

Reputation: 2309

After your first split you are ending up with items like "name=joe". You need to do another Split() on '=' and take the second part, i.e. the value joe.

Put this immediately after your split, and you'll be fine:

items = items.Select(x => x.Split('=')[1]).ToArray();

Upvotes: 0

Jonesopolis
Jonesopolis

Reputation: 25370

use another Split:

string[] items = allLines[index]
                   .Split(new []{ '|' })
                   .Select(i => i.Split(new []{ '=' })[1])
                   .ToArray();

//items[0] = "joe"
//items[1] = "clark"
//etc

Select will act on each item, splitting it again on =, and taking the second item it returns

Upvotes: 2

Vahlkron
Vahlkron

Reputation: 464

You will first split on '|', and then on '='. That will give you the ability to get those values.

You can do the split on your items[] array like so:

string[] val = items[0].Split(new char[] { '=' });
cmd.Parameters["@name"].Value = val[1];

Upvotes: 0

dimlucas
dimlucas

Reputation: 5141

You can use .split() in the same way as you did to remove the '|' from the input

Upvotes: 0

Related Questions