Question: This days I try to create an abstract base repository using some basic CRUD functions proposed by [DapperExtensions](https://github.com/tmsmith/Dapper-Extensions). But the code given as an exemple use a SqlConnection which is made to connect to a SQL Server database. I want to be able to connect to all kind of Database (SQL Server, MySql, etc...). Also their code sample is repeated for each CRUD function as the code below show

```
using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    //Code doing something here...
    cn.Close();
}

```

So i was thinking about to create a **DbContext** that can handle the creation, the opening and closing of the connection and also can create the right connection object depending on the database type I want to use (a kind of **database factory**).

Is there somebody that already done it and could share his code?

Thank you guys !

Answer: 
Something like this 

```
if (args.Length < 1) return;                // optional check if any args

string text = File.ReadAllText(args[0]);

string[] parts = text.Split(new[] { "Student" }, 0);

string[][] lines = Array.ConvertAll(parts, part => part.Split(new[] { '\r', '\n' }, 1));

Debug.Print(lines[1][1]);        // "Id...: 12345"

```
It is really vague what you are trying to do but here is a very basic example of what I think you are doing. 

Now the only thing you need to do is change the `fileName` param. 

```
static void Main(string[] args)
{
    string line = null;
    Student student = null;
    IList<Student> students = new List<Student>();
    using (var fileReader = new StreamReader(fileName))
    {
        while ((line = fileReader.ReadLine()) != null)
        {
            if (string.IsNullOrWhiteSpace(line))
                continue; //continue execution on extra lines
            if(line.StartsWith("Student...", StringComparison.CurrentCultureIgnoreCase))
            {
                student = new Student();
                students.Add(student);
            }

            if (student != null)
                student.Lines.Add(line);

        }

        fileReader.Close();
    }
}

class Student
{
    public IList<string> Lines { get; } = new List<string>();
}

```

In a nutshell all this little program does is starts reading a file line by line (note `while ((line = fileReader.ReadLine()) != null)` will set the value of `line` to the next line in the file or `null` when the file has been completely read.

Next it checks for empty lines (we dont care about empty lines so we move on)

We then check if the `line` startsith `Student...` now we are using `StringComparison.CurrentCultureIgnoreCase` so we can compare case insensitive.

If this is a Student line, we then create a new student and `Add()` it to our `students` collection. 

Finally as long as `student` is not null we can add the contents of `line` to the student `.Lines` property.