Let's do this properly, with parameters in the sql:
private void DatenInDatenbankSchreibenKalibrierung()
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + MyDataBase;
connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand cmd = new OleDbCommand();
int testValue = 1;
string values = "INSERT INTO DataTable (ID,[Date],Measurement) VALUES (?,?,?)";
cmd = new OleDbCommand(values, connection);
cmd.Parameters.AddWithValue("p1", testValue);
cmd.Parameters.AddWithValue("p2", DateTime.Now);
cmd.Parameters.AddWithValue("p3", 1234);
cmd.ExecuteNonQuery();
connection.Close();
}
Why we always use parameters
Now, for side info, also take a read of Can we stop using AddWithValue already - it doesn't matter so much for an insert, and AddWithValue is convenient to demo parameterization in an SO post, but there are improvements for production system SELECTs (not using access would also be an improvement) because if AddWithValue guesses the type wrong it could have performance or data precision implications
Always use parameters when writing SQLs. If you ever think you can't, because maybe you have the values in an array and you have to concatenate the values in to the sql using a loop (for example); DON'T concat the values in. It's perfectly possibly to concat parameter placeholders in and add a parameter for every value in the array. Never concat values, always concat parameter placeholders
If you want to make your database life easier but still write SQL, take a look at Dapper
With dapper your code would look more like:
using(var x = new OleDbConnection(...)){
x.Execute("INSERT INTO DataTable (ID,[Date],Measurement) VALUES (?id?,?dt?,?ms?)",
new { id = 1, dt = DateTime.Now, ms = 1234 }
);
}
Yup, that's it! Dapper will take care of all the connection opening, parameter adding, type guessing etc, all you need to do is execute the query on the connection and give it the sql and an anonymous object with properties whose names match the parameternames you put in the sql
Disclaimer: I've never used dapper to do this on Access/ole, I'm just parroting the words of Dapper's author