#Softdrinks
Cola 2
Sprote 3
Fant 4
Redbull 2
#Pide-Lahmacun
Pide Mozarella 12
Pide Hackfleisch 12
Pide Feta-Hackfleisch 14
Pide Spinat 13
Pide Spinat-Ei 14
above is the text file format. here # define the category name of product.'Cola 2' define the product name and the price. Where product name is Cola and price is 2. now how to add this data into my product table.I am using c# and SQL. Thanks.
Use following code to parse text file and then use string to put into database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication108
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string line = "";
string category = "";
string pattern = @"(?'name'.*)\s+(?'price'\d+)";
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (line.StartsWith("#"))
{
category = line.Substring(1);
}
else
{
Match match = Regex.Match(line, pattern);
string name = match.Groups["name"].Value.Trim();
string price = match.Groups["price"].Value.Trim();
Console.WriteLine("category : '{0}', name : '{1}', price : '{2}'", category, name, price);
}
}
}
Console.ReadLine();
}
}
}