public static async void ImportDB(string connect) { SqlConnection con = new SqlConnection(@" Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename =" + connect + "; Integrated Security = True"); //Connection to second DB SqlDataReader reader = null; await con.OpenAsync(); SqlCommand SelectCommand = new SqlCommand("SELECT * FROM [Table]", con); Comps.compsList.Clear(); reader = await SelectCommand.ExecuteReaderAsync(); //Reading all DB and get list while (await reader.ReadAsync()) { Comps comp = new Comps(); comp.PcName = reader[1].ToString(); comp.Responsibility = reader[3].ToString(); try { comp.AdmPass = Crypter.RSADecrypt(reader[4].ToString()); comp.BiosPass = Crypter.RSADecrypt(reader[5].ToString()); comp.Date = reader[2].ToString(); } catch { comp.AdmPass = reader[4].ToString(); comp.BiosPass = reader[5].ToString(); comp.Date = null; } Comps.compsList.Add(comp); } reader.Close(); con.Close(); SqlCommand command = new SqlCommand("INSERT INTO [Table] (PcName, Date, Responsibility, AdmPass, BiosPass)VALUES(UPPER(@PcName), @Date, @Responsible, @AdmPass, @BiosPass)", connection); foreach (Comps comp in Comps.compsList) { command.Parameters.Clear(); command.Parameters.AddWithValue("PcName", comp.PcName); if (comp.Date != null && comp.Date.Length > 1) { command.Parameters.AddWithValue("Date", DateTime.Parse(comp.Date)); } else { command.Parameters.AddWithValue("Date", DBNull.Value); } command.Parameters.AddWithValue("Responsible", comp.Responsibility); command.Parameters.AddWithValue("AdmPass", Crypter.RSAEncrypt(comp.AdmPass)); command.Parameters.AddWithValue("BiosPass", Crypter.RSAEncrypt(comp.BiosPass)); command.ExecuteNonQuery(); } }
public static async void LoadDB() { Comps.compsList.Clear(); SqlDataReader reader = null; if (connection == null) { try { connection = new SqlConnection(Settings2.Default.dataSource); await connection.OpenAsync(); //Open connection with start programm and closing it with main window } catch { MessageBox.Show("Не удалось установить соединение с базой. Возможно местоположение изменилось. Укажите еще раз."); ChangePath(); return; } } SqlCommand command = new SqlCommand("SELECT * FROM [Table] ORDER BY PCname", connection); try { reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { new Comps(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), Crypter.RSADecrypt(reader[4].ToString()), Crypter.RSADecrypt(reader[5].ToString()), reader[6].ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), ex.Source.ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } finally { if (reader != null) { reader.Close(); } if (models == null) { models = new List <string>(); GetModels(); } } }
private static async void UpdateLastPC() { SqlDataReader reader = null; SqlCommand sqlCommand = new SqlCommand("SELECT MAX(Id) FROM [Table]"); while (connection.State == System.Data.ConnectionState.Open) { await Task.Delay(1000); } try { reader = await sqlCommand.ExecuteReaderAsync(); while (await reader.ReadAsync()) { Comps comp = new Comps(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), Crypter.RSADecrypt(reader[4].ToString()), Crypter.RSADecrypt(reader[5].ToString()), reader[6].ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), ex.Source.ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } }