public static void ToText(DefaultDB db, string outputPath) { var fileContent = GetFileContent(db); if (File.Exists(outputPath)) { File.Delete(outputPath); } using (var stream = File.CreateText(outputPath)) { stream.Write(fileContent); } }
private void ImportData(string path) { using (var connection = new SqlCeConnection("Data Source = " + path)) { DefaultDB db = new DefaultDB(connection); txtSurveyName.Text = db.SurveyInfo.First().SurveyName; txtSurveyDescription.Text = db.SurveyInfo.First().Description; _questions = new ObservableCollection <Questions>(); _questions.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_questions_CollectionChanged); foreach (var question in db.Questions) { Questions q = new Questions() { Instruction = question.Instruction, NumAnswers = question.NumAnswers, Options = question.Options, Order = question.Order, Question = question.Question, Type = question.Type }; foreach (var option in question.Options) { q.Options.Add(new Options() { Option = option.Option, Order = option.Order, }); } _questions.Add(q); } _interviewers = new ObservableCollection <Interviewers>(db.Interviewers); _interviewers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_interviewers_CollectionChanged); lvInterviewers.ItemsSource = _interviewers; CollectionView view1 = (CollectionView)CollectionViewSource.GetDefaultView(lvInterviewers.ItemsSource); view1.SortDescriptions.Add(new System.ComponentModel.SortDescription("Id", System.ComponentModel.ListSortDirection.Ascending)); lvQuestions.ItemsSource = _questions; if (db.Interview.Count() > 0) { SetViewMode(ViewMode.Export, db.Interview.Count()); } else { SetViewMode(ViewMode.Edit); } } }
private void ExportData(string path) { using (var connection = new SqlCeConnection("Data Source = " + path)) { DefaultDB db = new DefaultDB(connection); SurveyInfo info = new SurveyInfo() { SurveyName = txtSurveyName.Text, Description = txtSurveyDescription.Text }; db.SurveyInfo.InsertOnSubmit(info); db.Interviewers.InsertAllOnSubmit(_interviewers); db.Questions.InsertAllOnSubmit(_questions); db.SubmitChanges(); } }
private void menuExport_Click(object sender, RoutedEventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.InitialDirectory = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Zeus"); dlg.DefaultExt = ".txt"; dlg.Filter = "Arquivos de texto (.txt)|*.txt"; bool?result = dlg.ShowDialog(); if (result == true) { using (var connection = new SqlCeConnection("Data Source = " + _filename)) { DefaultDB db = new DefaultDB(connection); Export.ToText(db, dlg.FileName); //MessageBox.Show("Questionário exportado com sucesso", "Mensagem", MessageBoxButton.OK, MessageBoxImage.Information); } } }
private static string GetFileContent(DefaultDB db) { var fileContent = new StringBuilder(); foreach (var interview in db.Interview) { var text = new StringBuilder(); text.Append(interview.StartTime.ToString("s") + ";"); if (interview.EndTime.HasValue) { var duration = Convert.ToInt32(interview.EndTime.Value.Subtract(interview.StartTime).TotalMinutes); text.Append(duration + ";"); } else { text.Append(";"); } text.Append(interview.Interviewer_Id + ";"); var questions = from o in db.Questions orderby o.Order select o; foreach (var question in questions) { var answers = from a in question.Answers where a.Interview_Id == interview.Id select a; switch (question.Type) { case 1: if (answers.Count() == 1) { text.Append(answers.ElementAt(0).OpenEnded); } text.Append(";"); break; case 2: for (int i = 0; i < question.NumAnswers; i++) { var x = answers.Where(a => a.CloseEnded == i + 1); if (x.Count() == 1) { text.Append(x.Single().CloseEnded); } text.Append(";"); } break; case 3: if (answers.Count() == 1) { text.Append(answers.ElementAt(0).CloseEnded); } text.Append(";"); break; case 4: if (answers.Count() == 1) { text.Append(answers.ElementAt(0).CloseEnded + ";"); text.Append(answers.ElementAt(0).OpenEnded + ";"); } else { text.Append(";;"); } break; } } text.Remove(text.Length - 1, 1); fileContent.AppendLine(text.ToString()); } return(fileContent.ToString()); }