Приветствую Вас, Гость

Меню

Форма входа

Войти через соцсеть:

Уроки
Урок 1. Как написать программу на C#
Урок 2. Что такое элементы, свойства и события и как с ними работать
Урок 3. Условный оператор if (ЕСЛИ). Операторы & (И) и | (ИЛИ)
---
Урок 1. Как написать приложение Android на C#

Статьи
Как быстро перейти с C++ на C#

Друзья сайта
  • Создайте сайт на uCoz бесплатно
  • MSDN - Руководство C#
  • MSDN - C# для школьников
  • PInvoke.Net - C# под WinAPI (DllImport'ы)

  • Статистика
    Онлайн всего: 1
    Гостей: 1
    Пользователей: 0

     Файлы 
    Главная » Файлы » Программы под Winforms (классические программы C#) » Базы данных (Access, MSSQL, sqlite, MySQL) [ Добавить пример ]

    БД Access (mdb). Update, Insert, Select, Delete. ООП [1]
    25.03.2014, 21:27

    Код формы.



    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.OleDb;

    //using Session;

    //using Description;



    namespace СУБД_ООП_Cs_2008

    {

        public partial class Form1 : Form

        {

            Session session = new Session(); 



            public Form1()

            {

                InitializeComponent();

            }



            private void buttonInsert_Click(object sender, EventArgs e)

            {



                Description description = new Description();

                description.id_p = textBox1.Text;

                description.name = textBox2.Text;

                description.myvalue = textBox3.Text;

                description.id_t = textBox4.Text;

                description.stage = textBox5.Text;

                description.diagnosis = textBox6.Text;

                string CommandText = "INSERT INTO temp (id_p, name, value, id_t, stage, diagnosis) VALUES(' " +

                        description.id_p + " ', ' " + description.name + " ', ' " +

                        description.myvalue + " ', ' " + description.id_t + " ', ' " +

                        description.stage + " ', ' " + description.diagnosis + " ' )";

                textBox7.Text = CommandText;

                session.Insert(description);

            }



            private void buttonSelect_Click(object sender, EventArgs e)

            {



                comboBox1.DataSource = session.FillComboBox();

            }



            private void buttonUpdate_Click(object sender, EventArgs e)

            {

                Description oldDescription = new Description();

                Description newDescription = new Description();

                oldDescription = comboBox1.SelectedItem as Description;



                newDescription.id_p = textBox1.Text;

                newDescription.name = textBox2.Text;

                newDescription.myvalue = textBox3.Text;

                newDescription.id_t = textBox4.Text;

                newDescription.stage = textBox5.Text;

                newDescription.diagnosis = textBox6.Text;



                string CommandText = "UPDATE temp SET id_p = " + newDescription.id_p +

           ", name = " + newDescription.name + ", [value] = " + newDescription.myvalue +

           ", id_t = " + newDescription.id_t + ", stage = " + newDescription.stage +

           ", diagnosis = " + newDescription.diagnosis + " WHERE ID = " + oldDescription.id;

                textBox7.Text = CommandText;



                session.Update(oldDescription, newDescription);

            }



            private void buttonDelete_Click(object sender, EventArgs e)

            {

                Description description = new Description();

                description = comboBox1.SelectedItem as Description;

                session.Delete(description);

            }

        }

    }



    Код класса Description.



    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;



    namespace СУБД_ООП_Cs_2008

    {

        public class Description

        {

            public int id;

            public string id_p;

            public string name;

            public string myvalue;

            public string id_t;

            public string stage;

            public string diagnosis;



            public override string ToString()

            {

                return Convert.ToString(id) + "  " + id_p + "  " + name

                    + "  " + myvalue + "  " + id_t + "  " + stage + "  " + diagnosis;

            }



        }

    }



    Код класса Session.



    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Data.OleDb;



    namespace СУБД_ООП_Cs_2008

    {

        public class Session

        {

            OleDbConnection connection;

            OleDbCommand command;





            public void ConnectTo()

            {

                connection = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=" + System.Windows.Forms.Application.StartupPath + "\\dbTest.mdb");

                command = connection.CreateCommand();

            }



            public Session()

            {

                ConnectTo();

            }





            public void Insert(Description description)

            {

                try

                {

                    command.CommandText = "INSERT INTO temp (id_p, name, [value], id_t, stage, diagnosis) VALUES(' " +

                        description.id_p + " ', ' " + description.name  + " ', ' " +

                        description.myvalue + " ', ' " + description.id_t + " ', ' " +

                        description.stage + " ', ' " + description.diagnosis + " ' );";

                   //command.CommandType =  command.CommandText;

                   //command.CommandType = CommandType.Text;

                    

                    connection.Open();



                    command.ExecuteNonQuery();

                }



                catch (Exception)

        

                {

                    throw;

                }





                finally

                {

                    if (connection != null)

                    {

                        connection.Close();

                    }

                }

            }

            //завершился метод



            public List<Description> FillComboBox()

            {

                List<Description> descriptionsList = new List<Description>();



                try

                {

                    command.CommandText = "SELECT * FROM temp;" ;

                    //(id_p, name, [value], id_t, stage, diagnosis) VALUES(' " + 

                    //    description.id_p + " ', ' " + description.name  + " ', ' " +

                    //    description.myvalue + " ', ' " + description.id_t + " ', ' " +

                    //    description.stage + " ', ' " + description.diagnosis + " ' );";

                    connection.Open();



                    OleDbDataReader reader = command.ExecuteReader();



                    while (reader.Read())

                    {

                        Description description = new Description();

                        description.id = Convert.ToInt32(reader["id"].ToString());

                        description.id_p = reader["id_p"].ToString();

                        //MessageBox.Show(description.id_p);

                        description.name = reader["name"].ToString();

                        description.myvalue = reader["value"].ToString();

                        description.id_t = reader["id_t"].ToString();

                        description.stage = reader["stage"].ToString();

                        description.diagnosis = reader["diagnosis"].ToString();



                        descriptionsList.Add(description);

                    }



                    return descriptionsList;

                }



                catch (Exception)

                {

                    throw;

                }





                finally

                {

                    if (connection != null)

                    {

                        connection.Close();

                    }

                }



            }



            //---------------------------------

             public void Update(Description oldDescription, Description newDescription)

     {



       try

       {

       command.CommandText = "UPDATE temp SET id_p = "+newDescription.id_p + 

           ", name = "+newDescription.name + ", [value] = "+newDescription.myvalue + 

           ", id_t = "+newDescription.id_t + ", stage = "+newDescription.stage + 

           ", diagnosis = "+newDescription.diagnosis + " WHERE ID = " + oldDescription.id;



       connection.Open();



       command.ExecuteNonQuery();

        }



        catch (Exception)

                {

                    throw;

                }





                finally

                {

                    if (connection != null)

                    {

                        connection.Close();

                    }

                }



            }

            //-----------------------------------------------

            public void Delete(Description description)

     {



       try

       {

       command.CommandText = "DELETE FROM temp WHERE ID = "+description.id;



       connection.Open();



       command.ExecuteNonQuery();

        }

       catch (Exception)

                {

                    throw;

                }





                finally

                {

                    if (connection != null)

                    {

                        connection.Close();

                    }

                }



            }

    //------------------------------------------------

        }

    }

    Категория: Базы данных (Access, MSSQL, sqlite, MySQL) | Добавил: Iren_Nietzsche | Теги: c# access mdb, c# update sql, c# sql, c# зачем нужны классы, c# delete sql, c# ооп, c# insert sql, c# классы
    Просмотров: 1264 | Загрузок: 0 | Комментарии: 3 | Рейтинг: 1.0/1
    Всего комментариев: 3
    3 SmqXA   (01.04.2022 08:36) [Материал]
    Buy Plaquenil Online from
    <a href="https://plaquenil4people.top">buy cheap plaquenil no prescription</a>

    2 pharmacepticacom   (19.06.2021 19:19) [Материал]

    1 WayEngato   (11.02.2017 10:30) [Материал]
    Proscar Order Best On Line Pharmacy http://call4ph.com - viagra Propecia Hairy Back Buy Accutane Uk Online http://clomid.rx-cs17.com/generic-clomid-pills.php - Generic Clomid Pills Viagra E Cialis Prezzo Propecia En Farmacias Comprar http://nolvadex.rx-cs17.com/best-nolvadex.php - Best Nolvadex Cialis Et Autre Amoxicillin 500mg Ingredients http://drugslr.com - online pharmacy Lioresal Intrathecal Effets Secondaires Levitra A Vendre http://doxycycline.inpills.com/order-vibramycin.php - Order Vibramycin Propecia Androgenos Cual Es El Precio De Viagra En Mexicali http://drugs2k.net - cialis price Amoxicillin And Diarrhea Viagra E Prostata http://amoxil.inpills.com/buy-amoxil-online-nopw.php - Buy Amoxil Online Nopw Venta De Viagra Internet Levitra Canada http://doxycycline.rx-cs17.com/doxycycline-tablets.php - Doxycycline Tablets Vardenafil Oral Viagra Ansia http://accutane.mdsmeds.com/accutane-10mg.php - Accutane 10mg Lasix Online Uk Viagra Generika Frau http://cialis.mdsmeds.com/cialis-free-trial.php - Cialis Free Trial Viagra Zum Kaufen Ohne Rezept Viagra Levitra Together http://levitra.inpills.com/map.php - Purchase Levitra Levitra Strips Seroflo http://corzide.com - viagra Buy Tadalafil Viagra Oder Levitra Erfahrungen http://byrxbox.com - buy viagra Costo Finasteride Propecia Propecia Generika 1mg http://lasix.mdsmeds.com/cheap-lasix-no-rx.php - Cheap Lasix No Rx Dog Side Effects Of Amoxicillin Levitra Filmtabletten Schmelztabletten http://propecia.inpills.com/comprar-propecia.php - Comprar Propecia Lasix 20 Mg Worldwide Levaquin In Internet http://cidovir.com - cialis Overseas Prescription Drugs Online Canadian Pharmacies Online http://visdbs.com - buy viagra Legally Zentel Valbazen Internet Visa Accepted

    Имя *:
    Email *:
    Код *:
    Copyright vZ © 2024