private void ReadFields(BookAuthorComposition result, DataRow row) { result.Id = Utilities.GetIntFromRow(row, tableFieldID).Value; result.IdBook = Utilities.GetIntFromRow(row, tableFieldID_BOOK); result.IdAuthor = Utilities.GetIntFromRow(row, tableFieldID_AUTHOR); }
private void bNAuthorsDelete_Click(object sender, EventArgs e) { BookAuthorComposition obj = GetCurrentBookAuthorComposition(); if (obj != null) { if (MessageBox.Show(this, "Вы действительно хотите удалить информацию об авторе?", "Предупреждение", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK) { Config.Session.BookAuthorCompositionUtils.Delete(obj); RefreshAuthorsList(); } } }
private BookAuthorComposition GetCurrentBookAuthorComposition() { BookAuthorComposition result = null; int?id = GetCurrentBookAuthorCompositionId(); if (id.HasValue) { result = Config.Session.BookAuthorCompositionUtils.GetById(id.Value); } return(result); }
public BookAuthorComposition GetById(int id) { BookAuthorComposition result = null; StringBuilder sqlStr = new StringBuilder(); sqlStr.AppendLine("select *"); sqlStr.AppendLine("from " + TableNameWithScheme); sqlStr.AppendLine(string.Format("where {0} = @p_id", tableFieldID)); if (this.session != null && this.session.Connection != null && this.session.Connection.State == ConnectionState.Open) { SqlCommand command = new SqlCommand(sqlStr.ToString(), this.session.Connection); command.Parameters.AddWithValue("p_id", id); DataSet dataSet = new DataSet(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable table; try { dataAdapter.Fill(dataSet); if (dataSet.Tables.Count > 0) { table = dataSet.Tables[0]; if (table.Rows.Count == 1) { DataRow row = table.Rows[0]; result = new BookAuthorComposition(); ReadFields(result, row); } } } catch (Exception) { } } return(result); }
private bool Add(BookAuthorComposition obj, out string message) { message = string.Empty; bool result = false; if (this.session != null && this.session.Connection != null && this.session.Connection.State == ConnectionState.Open) { SqlCommand command = new SqlCommand(string.Empty, this.session.Connection); SQLCommandBuilder builder = new SQLCommandBuilder(TableNameWithScheme); FillCommandBuilder(builder, command, obj); builder.FinishString = "SELECT @@IDENTITY AS 'Identity'"; command.CommandText = builder.GenerateInsertText(); try { object value = command.ExecuteScalar(); if (value != null && value != DBNull.Value) { try { int id = Convert.ToInt32(value); obj.Id = id; result = true; } catch (Exception ex) { result = false; message = ex.Message; } } } catch (Exception ex) { result = false; message = ex.Message; } } return(result); }
public void Delete(BookAuthorComposition obj) { if (obj != null && obj.Id.HasValue) { StringBuilder sqlStr = new StringBuilder(); sqlStr.AppendLine("delete from " + TableNameWithScheme); sqlStr.AppendLine(string.Format("where {0} = @p_id", tableFieldID)); if (this.session != null && this.session.Connection != null && this.session.Connection.State == ConnectionState.Open) { try { SqlCommand command = new SqlCommand(sqlStr.ToString(), this.session.Connection); command.Parameters.AddWithValue("p_id", obj.Id.Value); command.ExecuteNonQuery(); } catch (Exception) { } } } }
public bool Save(BookAuthorComposition obj, out string message) { message = string.Empty; if (obj != null) { if (obj.Id.HasValue) { //return Update(obj, out message); message = "Невозможно изменение."; return(false); } else { return(Add(obj, out message)); } } else { message = "Отсутствует объект для сохранения."; return(false); } }
private void bNAuthorsAdd_Click(object sender, EventArgs e) { FormAuthorList form = new FormAuthorList(); form.IsSelect = true; if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (form.SelectedAuthor != null) { BookAuthorComposition obj = new BookAuthorComposition(); obj.IdBook = this.Book.Id; obj.IdAuthor = form.SelectedAuthor.Id; string message = string.Empty; if (Config.Session.BookAuthorCompositionUtils.Save(obj, out message)) { } else { StringBuilder tmpBld = new StringBuilder(); tmpBld.AppendLine("Возникла ошибка при сохранении в базу."); tmpBld.AppendLine(); tmpBld.Append(message); MessageBox.Show(this, tmpBld.ToString(), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } RefreshAuthorsList(); } } form.Dispose(); }
//private bool Update(City obj, out string message) //{ // bool result = false; // message = string.Empty; // if (this.session != null && this.session.Connection != null && this.session.Connection.State == ConnectionState.Open) // { // SqlCommand command = new SqlCommand(string.Empty, this.session.Connection); // SQLCommandBuilder builder = new SQLCommandBuilder(TableNameWithScheme); // FillCommandBuilder(builder, command, obj); // builder.FinishString = string.Format("where {0} = @p_id", tableFieldID); // command.Parameters.AddWithValue("p_id", obj.Id.Value); // command.CommandText = builder.GenerateUpdateText(); // try // { // int numberOfRowsAffected = command.ExecuteNonQuery(); // if (numberOfRowsAffected == 1) // { // result = true; // } // } // catch (Exception ex) // { // result = false; // message = ex.Message; // } // } // return result; //} private void FillCommandBuilder(SQLCommandBuilder builder, SqlCommand command, BookAuthorComposition obj) { if (builder == null || command == null || obj == null) { return; } builder.AddParameter(tableFieldID_BOOK, "@p_id_book"); command.Parameters.AddWithValue("p_id_book", obj.IdBook); builder.AddParameter(tableFieldID_AUTHOR, "@p_id_author"); command.Parameters.AddWithValue("p_id_author", obj.IdAuthor); }