Mono use DB to update Raspberry time







static String SqlStrGet = @"Data Source=192.16.16.2;Initial Catalog=Prod;User ID=sa;Password=sa";

private static DateTime GetDateNow() {
DateTime now = DateTime.Now;
try {
using (SqlConnection conn = new SqlConnection(SqlStrGet )) {
conn.Open();
String sql = "select GETDATE()";
SqlCommand command = new SqlCommand(sql, conn);
using (SqlDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {

now = (DateTime)reader[0];
}
}
}
RunLinuxCmd("/bin/date --set '"+now.ToString("yyyy-MM-dd HH:mm:ss")+"'");

} catch (Exception ex) {
Log("GetDateNow Exception "+ex.ToString());
}
return now;
}



private static bool RunLinuxCmd(string arg)

{

Process process = new Process();
process.StartInfo.FileName = "/usr/bin/sudo";
process.StartInfo.Arguments = arg;
process.Start();
return true;

}




public static void Log(string log) {
Console.WriteLine(DateTime.Now.ToString()+":Program:"+log);
}








留言

熱門文章