private void testDb_Click(object sender, EventArgs ea) {
typeTab.Enabled = false;
Cursor = Cursors.WaitCursor;
dbTestStatus.Text = "Testing Now... Please Wait...";
// update UI
Application.DoEvents();
var lw_mappings = get_db_mappings();
var fields = util.concatenate(lw_mappings.Select(x => x.Item1), ", ");
string sql = "select " + fields + " from " + dbTableName.Text;
// sort by date/time if possible
var date_mapping = lw_mappings.FirstOrDefault(x => x.Item2 == info_type.date);
var time_mapping = lw_mappings.FirstOrDefault(x => x.Item2 == info_type.time);
if (date_mapping != null)
sql += " order by " + date_mapping.Item1;
if (time_mapping != null) {
sql += sql.Contains(" order by ") ? ", " : " order by ";
sql += time_mapping.Item1;
}
logger.Debug("executing db command [" + sql + "]" + " on [" + dbConnectionString.Text + "]");
// test connecting to database + and reading 1 record + read everything we need from that record (according to the fields)
string status = "Congratulations! Successful connection.";
try {
using (var conn = db_util.create_db_connection(db_provider_index_to_string(), dbConnectionString.Text))
using (var cmd = conn.CreateCommand()) {
conn.Open();
cmd.CommandText = sql;
using (var rs = cmd.ExecuteReader()) {
if (rs.Read()) {
// at this point, I will read all fields
for (int i = 0; i < lw_mappings.Count; ++i) {
bool is_time = lw_mappings[i].Item2 == info_type.date || lw_mappings[i].Item2 == info_type.time;
// here, I'm just testing for exceptions
if (is_time) {
// http://stackoverflow.com/questions/11414399/sqlite-throwing-a-string-not-recognized-as-a-valid-datetime
// normally, GetDateTime should work. But just in case it doesn't, read it as a string, and then,
// later, we'll parse the date/time with util.normalize_* functions
bool ok = true;
try {
rs.GetDateTime(i);
} catch {
ok = false;
}
if (!ok)
rs.GetString(i);
} else
rs.GetString(i);
}
} else
status = "ERROR: Log Table is empty.";
}
}
} catch (Exception e) {
status = "ERROR: " + e.Message;
}
dbTestStatus.ForeColor = status.StartsWith("ERROR") ? Color.Red : Color.LightSeaGreen;
dbTestStatus.Text = status;
typeTab.Enabled = true;
Cursor = Cursors.Default;
}