Example #1
0
        public static void AddRecord(string name, string url, string user, string pswd, string note)
        {
            SQLiteConnection conn = new SQLiteConnection(String.Format("Data Source={0};Version=3;", App.DB_PATH));

            conn.Open();

            using (DbCommand command = conn.CreateCommand())
            {
                command.CommandText = "INSERT INTO services (name, url) VALUES(@Name, @URL)";
                command.Parameters.Add(new SQLiteParameter("Name", name));
                command.Parameters.Add(new SQLiteParameter("URL", url));

                try
                {
                    command.ExecuteNonQuery();
                }
                catch (SQLiteException e)
                {
                    throw new DBInsertServiceException(e.Message);
                }
            }
            using (DbCommand command = conn.CreateCommand())
            {
                string secret = SecretHelper.Encrypt(pswd);
                if (String.IsNullOrWhiteSpace(note))
                {
                    note = null;
                }

                command.CommandText = "INSERT INTO keypairs (service, user, secret, note) " +
                                      "SELECT id, @Username, @Secret, @Note FROM services WHERE name LIKE @Name LIMIT 1";
                command.Parameters.Add(new SQLiteParameter("Name", name));
                command.Parameters.Add(new SQLiteParameter("Username", user));
                command.Parameters.Add(new SQLiteParameter("Secret", secret));
                command.Parameters.Add(new SQLiteParameter("Note", note));

                try
                {
                    command.ExecuteNonQuery();
                }
                catch (SQLiteException e)
                {
                    throw new DBInsertServiceException(e.Message);
                }
            }
            conn.Close();
        }