private void ReadFields(BookTagComposition result, DataRow row)
        {
            result.Id = Utilities.GetIntFromRow(row, tableFieldID).Value;

            result.IdBook = Utilities.GetIntFromRow(row, tableFieldID_BOOK);
            result.IdTag  = Utilities.GetIntFromRow(row, tableFieldID_TAG);
        }
Esempio n. 2
0
        private void bNTagsDelete_Click(object sender, EventArgs e)
        {
            BookTagComposition obj = GetCurrentBookTagComposition();

            if (obj != null)
            {
                if (MessageBox.Show(this, "Вы действительно хотите удалить информацию о тэге?", "Предупреждение", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                {
                    Config.Session.BookTagCompositionUtils.Delete(obj);
                    RefreshTagsList();
                }
            }
        }
Esempio n. 3
0
        private BookTagComposition GetCurrentBookTagComposition()
        {
            BookTagComposition result = null;

            int?id = GetCurrentBookTagCompositionId();

            if (id.HasValue)
            {
                result = Config.Session.BookTagCompositionUtils.GetById(id.Value);
            }

            return(result);
        }
        public BookTagComposition GetById(int id)
        {
            BookTagComposition 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 BookTagComposition();

                            ReadFields(result, row);
                        }
                    }
                }
                catch (Exception)
                {
                }
            }

            return(result);
        }
        private bool Add(BookTagComposition 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(BookTagComposition 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(BookTagComposition 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);
            }
        }
Esempio n. 8
0
        private void bNTagsAdd_Click(object sender, EventArgs e)
        {
            FormTagList form = new FormTagList();

            form.IsSelect = true;

            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (form.SelectedTag != null)
                {
                    BookTagComposition obj = new BookTagComposition();

                    obj.IdBook = this.Book.Id;
                    obj.IdTag  = form.SelectedTag.Id;

                    string message = string.Empty;

                    if (Config.Session.BookTagCompositionUtils.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);
                    }

                    RefreshTagsList();
                }
            }

            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, BookTagComposition 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_TAG, "@p_id_tag");
            command.Parameters.AddWithValue("p_id_tag", obj.IdTag);
        }