Although there is a lot of room for going wrong in this answer, here is a simple solution.
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace FileToDb
{
[Database]
public class MyDataDB : DataContext
{
public MyDataDB ( ) : base("Data Source=..." );
public Table<MyData> MyData;
}
[Table( Name = "MyData" )]
public class MyData
{
[Column] public int Number { set; get; }
[Column] public string Name { set; get; }
}
public class Program
{
public void Main()
{
var dbContext = new MyDataDB();
var lines = File.ReadAllLines("path");
foreach (var line in lines)
{
var tokens = line.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
var data = new MyData();
data.Number = int.Parse(tokens[0]);
data.Name = tokens[1];
dbContext.MyData.InsertOnSubmit(data);
}
dbContext.SubmitChanges();
}
}
}
didn't test, because I'd need to set up a DB.
and I did leave out validation code. you shouldn't! :-)
I heavily relied on LINQ tutorials by Abby Fichtner (Hacker Chick) (
1 and
2