Search code examples
c#insertado.netsql-server-ce

ADO.NET proper INSERT of data


I am using a SQL CE 3.5 database in my C# project.

When I am adding new data to a table, let's say a simple row, I feel like I am doing it totally wrong. What I am doing is something like this:

const string commandStr = "INSERT INTO tableName (size, x, y) VALUES(@size, @x, @y)";
using (SqlCeCommand command = new SqlCeCommand(commandStr, _connection))
{
    command.Parameters.AddWithValue("@size", size);
    command.Parameters.AddWithValue("@x", x);
    command.Parameters.AddWithValue("@y", y);
    int affectedRows = command.ExecuteNonQuery();
}

Of course, my actual table that I am using in my project has a lot more collumns. Obviously, this is a lot of typing. This brings me to the point where I am thinking: "I am a .NET devoloper. There must be something that feels a lot more naturally."

So my questions are: Is there a nicer way to do this? If so, how?


Solution

  • Sometimes there are edge cases where doing it your way is required. However, for a full, verbose, ".NET developer" feel, try Entity Framework. It is awesome.

    Entity Framework is so nice for this in my opinion. I really enjoy using it.