private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Trim().Length == 0) { MessageBox.Show("Alias name cannot be empty."); return; } OpenFileDialog of = new OpenFileDialog(); if (of.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } using (SQLiteConnection conn = new SQLiteConnection(config.DataSource)) { using (SQLiteCommand cmd = new SQLiteCommand()) { conn.Open(); cmd.Connection = conn; SQLiteHelper sh = new SQLiteHelper(cmd); sh.AttachDatabase(of.FileName, textBox1.Text); ShowDatabase(sh); conn.Close(); } } }
/// <summary> /// 合并文件 /// </summary> private async void ExecuteMergeLibraryCommand() { IsEnabled = false; bool error = IsPathValid(SourceDirectory, MergePath); if (error) { List<String> fileList = new List<string>(); DirectoryInfo theFolder = new DirectoryInfo(SourceDirectory); FileInfo[] fileInfo = theFolder.GetFiles("*.lib", SearchOption.TopDirectoryOnly); foreach (FileInfo File in fileInfo) //遍历文件 { string _datasource = string.Format("data source={0}", File.FullName); if (ValidateDataBase.Validate(_datasource)) { fileList.Add(File.FullName); } else { MessageBoxPlus.Show(App.Current.MainWindow, string.Format("文件“{0}”已加密或者它不是一个有效的库文件", File.FullName), "错误", MessageBoxButton.OK, MessageBoxImage.Error); IsEnabled = true; return; } } if (fileList.Count==1) { MessageBoxPlus.Show(App.Current.MainWindow, "至少两个库才能合并", "提示", MessageBoxButton.OK, MessageBoxImage.Information); IsEnabled = true; return; } DataTable[] dts = new DataTable[fileList.Count]; for (int i = 0; i < fileList.Count; i++) { using (SQLiteConnection conn = new SQLiteConnection(string.Format("data source={0}", fileList[i]))) { using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = conn; conn.Open(); SQLiteHelper sh = new SQLiteHelper(cmd); dts[i] = sh.Select("select * from QuestionInfo"); conn.Close(); } } } bool isSame = false; int j; for (j = 1; j < dts.Length; j++) { isSame = CompareDataTable(dts[0], dts[j]); if (!isSame) { break; } } if (!isSame) { MessageBoxPlus.Show(App.Current.MainWindow, string.Format("“{0}”与“{1}”的问卷结构不一致,无法合并!", Path.GetFileName(fileList[0]), Path.GetFileName(fileList[j])), "错误", MessageBoxButton.OK, MessageBoxImage.Warning); } else { using (SQLiteConnection conn = new SQLiteConnection(string.Format("data source={0}", fileList[0]))) { using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = conn; conn.Open(); SQLiteHelper sh = new SQLiteHelper(cmd); if (File.Exists(MergePath)) { try { File.Delete(MergePath); Visibility = "Visible"; File.Copy(fileList[0], MergePath); Progress = 1 * 100.0 / fileList.Count; ProgressMessage = string.Format("已完成 {0:0.0}%", Progress); } catch (Exception e) { MessageBoxPlus.Show(App.Current.MainWindow, e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); LogWritter.Log(e, "合库错误"); IsEnabled = true; return; } } else { Visibility = "Visible"; File.Copy(fileList[0], MergePath); } } } await Task.Run(() => { using (SQLiteConnection conn = new SQLiteConnection(string.Format("data source={0}", MergePath))) { using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = conn; conn.Open(); SQLiteHelper sh = new SQLiteHelper(cmd); int recordCount; try { recordCount = sh.ExecuteScalar<int>("select max(A_Record) from QuestionAnwser"); } catch (Exception e) { MessageBoxPlus.Show(App.Current.MainWindow, "记录为空!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); LogWritter.Log(e, "合库时记录为空"); IsEnabled = true; return; } //创建临时表 sh.Execute("create table temp as select * from QuestionAnwser where 1=0"); for (int i = 1; i < fileList.Count; i++) { string dataBase = "db"; sh.AttachDatabase(fileList[i], dataBase); int _recordCount = sh.ExecuteScalar<int>("select max(A_Record) from db.QuestionAnwser"); sh.Execute("insert into temp select * from db.QuestionAnwser"); sh.Execute(string.Format("Update temp set A_record=A_record+{0}", recordCount)); sh.Execute("insert into QuestionAnwser select * from temp"); sh.Execute("delete from temp"); sh.DetachDatabase(dataBase); recordCount = recordCount + _recordCount; Progress = (i + 1) * 100.0 / fileList.Count; ProgressMessage = string.Format("已完成 {0:0.0}%", Progress); } sh.Execute("drop table temp"); conn.Close(); Visibility = "Hidden"; } } }); MessageBoxPlus.Show(App.Current.MainWindow, "合并成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } IsEnabled = true; }