void SqlFullTextSearch(string text, char c) { string[] words = text.Split(' '); StringBuilder textToSql = new StringBuilder(); string str; if (c == '!') { str = "& !"; textToSql.Append("'!"); } else { str = "& "; textToSql.Append("'"); } textToSql.Append(words[0]); for (int i = 1; i < words.Length; i++) { textToSql.Append(str); textToSql.Append(words[i]); } textToSql.Append("'"); try { List <Tour> tours = db.Tours.FromSql($@"select tour.* FROM tour WHERE to_tsvector(tour.description) @@ to_tsquery({textToSql.ToString()})").ToList(); FillTableGridHeader(typeof(Tour).GetProperties() .Select(property => property.Name) .ToList()); int i = 0; foreach (Tour tour in tours) { TableDataGrid.Rows.Add(); TableDataGrid.Rows[i].Cells[0].Value = tour.Id; TableDataGrid.Rows[i].Cells[1].Value = tour.Price; TableDataGrid.Rows[i].Cells[2].Value = tour.Tourdate; TableDataGrid.Rows[i].Cells[3].Value = tour.City; TableDataGrid.Rows[i].Cells[4].Value = tour.IdTA; TableDataGrid.Rows[i].Cells[5].Value = tour.Description; i++; } } catch (Exception ex) { Console.WriteLine(ex.Message); ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); //ErrorForm errorForm = new ErrorForm(ex.Message); //errorForm.Show(); } }
private void btnAdd_Click(object sender, EventArgs e) { //if (!f) return; AddWindow addWindow; DataGridViewRow newRow; switch (curTable) { case "Clients": addWindow = new AddWindow(typeof(Client).GetProperties() .Select(property => property.Name) .ToList()); addWindow.ShowDialog(); if (addWindow.DialogResult == DialogResult.OK && addWindow.save) { try { newRow = addWindow.newRow; Client client = new Client(); client.Firstname = newRow.Cells[0].Value.ToString(); client.Lastname = newRow.Cells[1].Value.ToString(); client.Birthdate = Convert.ToDateTime(newRow.Cells[2].Value.ToString()); client.Gender = Convert.ToBoolean(newRow.Cells[3].Value.ToString()); db.Clients.Add(client); db.SaveChanges(); FillTableGridInfo(curTable); } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } } break; case "Tours": addWindow = new AddWindow(typeof(Tour).GetProperties() .Select(property => property.Name) .ToList()); addWindow.ShowDialog(); if (addWindow.DialogResult == DialogResult.OK && addWindow.save) { try { newRow = addWindow.newRow; Tour tour = new Tour(); tour.Price = decimal.Parse(newRow.Cells[0].Value.ToString()); tour.Tourdate = Convert.ToDateTime(newRow.Cells[1].Value.ToString()); tour.City = newRow.Cells[2].Value.ToString(); tour.IdTA = Int32.Parse(newRow.Cells[3].Value.ToString()); tour.Description = newRow.Cells[4].Value.ToString(); db.Tours.Add(tour); db.SaveChanges(); FillTableGridInfo(curTable); } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } } break; case "TravelAgencies": addWindow = new AddWindow(typeof(TravelAgency).GetProperties() .Select(property => property.Name) .ToList()); addWindow.ShowDialog(); if (addWindow.DialogResult == DialogResult.OK && addWindow.save) { try { newRow = addWindow.newRow; TravelAgency travelAgency = new TravelAgency(); travelAgency.Name = newRow.Cells[0].Value.ToString(); travelAgency.Adress = newRow.Cells[1].Value.ToString(); db.TravelAgencies.Add(travelAgency); db.SaveChanges(); FillTableGridInfo(curTable); } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } } break; } }
private void TableDataView_Click(object sender, DataGridViewCellEventArgs e) { //if (!f) return; if (e.ColumnIndex == TableDataGrid.ColumnCount - 2) { EditWindow editWindow = new EditWindow(TableDataGrid.Rows[e.RowIndex], typeof(Client).GetProperties() .Select(property => property.Name) .ToList()); editWindow.ShowDialog(); if (editWindow.save) { DataGridViewRow newRow = editWindow.editedRow; switch (curTable) { case "Clients": try { Client client = db.Clients .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString())) .FirstOrDefault(); client.Firstname = newRow.Cells[0].Value.ToString(); client.Lastname = newRow.Cells[1].Value.ToString(); client.Birthdate = Convert.ToDateTime(newRow.Cells[2].Value.ToString()); client.Gender = Convert.ToBoolean(newRow.Cells[3].Value.ToString()); db.SaveChanges(); FillTableGridInfo("Clients"); } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } break; case "Tours": try { Tour tour = db.Tours .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString())) .FirstOrDefault(); tour.Price = decimal.Parse(newRow.Cells[0].Value.ToString()); tour.Tourdate = Convert.ToDateTime(newRow.Cells[1].Value.ToString()); tour.City = newRow.Cells[2].Value.ToString(); tour.IdTA = Int32.Parse(newRow.Cells[3].Value.ToString()); tour.Description = newRow.Cells[4].Value.ToString(); db.SaveChanges(); FillTableGridInfo("Tours"); } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } break; case "TravelAgencies": try { TravelAgency travelAgency = db.TravelAgencies .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString())) .FirstOrDefault(); travelAgency.Name = newRow.Cells[0].Value.ToString(); travelAgency.Adress = newRow.Cells[1].Value.ToString(); db.SaveChanges(); FillTableGridInfo("TravelAgencies"); } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } break; } } } else if (e.ColumnIndex == TableDataGrid.ColumnCount - 1) { DeleteWindow deleteWindow = new DeleteWindow(); if (deleteWindow.ShowDialog() == DialogResult.OK && deleteWindow.delete) { try { switch (curTable) { case "Clients": Client client = db.Clients .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString())) .FirstOrDefault(); db.Clients.Remove(client); db.SaveChanges(); FillTableGridInfo("Clients"); break; case "Tours": db.Tours.Remove(db.Tours .Where(t => t.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString())) .FirstOrDefault()); db.SaveChanges(); FillTableGridInfo("Tours"); break; case "TravelAgencies": db.TravelAgencies.Remove(db.TravelAgencies .Where(ta => ta.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString())) .FirstOrDefault()); db.SaveChanges(); FillTableGridInfo("TravelAgencies"); break; } } catch (Exception ex) { ErrorWindow errorWindow = new ErrorWindow(ex.Message); errorWindow.Show(); } } } }