none
비주얼 스튜디오 OLE RRS feed

  • 질문

  • //데이타그리드뷰 출력
    OleDbDataAdapter viewadapter = new OleDbDataAdapter();
                viewadapter.SelectCommand = new OleDbCommand(query, UserDB.conn);
                DataSet viewset = new DataSet();
                viewadapter.Fill(viewset, "null");
                dataGridView1.DataSource = viewset.Tables["null"].DefaultView;

    //삭제 string delStr = "delete from 사원 where 사원ID=" + label4.Text + "";
    //수정 string restr = "update 사원 set 사원이름='" + textBox2.Text + "', 사원전화번호='" + textBox3.Text  + "' where 사원ID=" + label4.Text + "";
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Data.OleDb;        //데이터베이스


    namespace Access
    {
        public partial class Form1 : Form
        {
            Myindexer id = new Myindexer();             //인덱서 호출
            int index = 0;
            int store_index = 0;

            public Form1()
            {
                InitializeComponent();
               
            }

            private void 추가ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Dialog_Form2 Addinfo = new Dialog_Form2();
                Addinfo.ShowDialog();                                   //추가 대화상자 호출

                ADD_CARD info = new ADD_CARD();                 //추가 대화상자의 정보를 ADD_CARD로 저장
                info.Name = Addinfo.textBox1.Text;
                info.Sex = Addinfo.textBox2.Text;
                info.Phone = Addinfo.textBox3.Text;
                info.Address = Addinfo.textBox4.Text;

                id[index] = info;                           //ADD_CARD형태로 인덱서에 저장

                ListViewItem item = new ListViewItem(id[index].Name, 0);    //listView1에 인덱서에 있는 각각정보 저장
                item.SubItems.Add(id[index].Sex);
                item.SubItems.Add(id[index].Phone);
                item.SubItems.Add(id[index].Address);
                listView1.Items.Add(item);

                index++;
            }

            private void 삭제ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                bool Sameindex = false;
                try
                {
                    for (int i = 0; i < index; i++)         //인덱서의 인덱스
                    {
                        if (listView1.FocusedItem.Text == id[i].Name)       //선택된 item 의 이름과 인덱서의 name
                        {
                            listView1.FocusedItem.Remove();             //리스트뷰에서의 삭제
                            id.Remove(i);                   //인덱서에서의 삭제
                            index--;
                            Sameindex = true;
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    if (Sameindex == false)
                        MessageBox.Show("항목을 선택하세요.", "삭제");
                }
            }

            private void 정렬ToolStripMenuItem_Click(object sender, EventArgs e)
            {

                listView1.Sorting = SortOrder.Ascending;        //리스트뷰 정렬
                listView1.Sorting = SortOrder.None;
                id.Clear();                                 //배열 모두 삭제

                for (int i = 0; i < index; i++)             //ADD_CARD에
                {
                    ADD_CARD info = new ADD_CARD();
                    info.Name = listView1.Items[i].Text;
                    info.Sex = listView1.Items[i].SubItems[1].Text;
                    info.Phone = listView1.Items[i].SubItems[2].Text;
                    info.Address = listView1.Items[i].SubItems[3].Text;

                    id[i] = info;
                }
            }

            private void 저장ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                FileStream fs = new FileStream("R_KBJ.bin", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, id);
                store_index = index;
                fs.Close();

                MessageBox.Show("리스트의 내용을 저장하였습니다.", "저장");
            }

            private void 열기ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();

                listView1.Items.Clear();
                id.Clear();

                FileStream fs1 = new FileStream(openFileDialog1.FileName, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                id = (Myindexer)bf.Deserialize(fs1);

                for (int i = 0; i < store_index; i++)
                {
                    ListViewItem item = new ListViewItem(id[i].Name, 0);
                    item.SubItems.Add(id[i].Sex);
                    item.SubItems.Add(id[i].Phone);
                    item.SubItems.Add(id[i].Address);
                    listView1.Items.Add(item);
                }
                fs1.Close();
                index = store_index;

                MessageBox.Show("파일의 내용을 리스트로 불러왔습니다.", "열기");
            }

            private void 배열보기ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                for (int j = 0; j < index; j++)
                {
                    MessageBox.Show("[이름]:" + id[j].Name + "  [성별]:" + id[j].Sex +
                        "  [전화번호]:" + id[j].Phone + "  [주소]:" + id[j].Address, "배열");
                }
            }
           
           
            // Access 저장//
            private void 저장ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                UserDB.openCon();
            
                string query = "insert into address values(@이름, @성별, @전화번호,@주소)";
               
                OleDbCommand comm = new OleDbCommand(query, UserDB.conn);
                comm.Parameters.Add("@이름", OleDbType.Char);
                comm.Parameters.Add("@성별", OleDbType.Char);
                comm.Parameters.Add("@전화번호", OleDbType.Char);
                comm.Parameters.Add("@주소", OleDbType.Char);

                for (int i = 0; i < index; i++)
                {
                    comm.Parameters["@이름"].Value = listView1.Items[i].Text;
                    comm.Parameters["@성별"].Value = listView1.Items[i].SubItems[1].Text;
                    comm.Parameters["@전화번호"].Value = listView1.Items[i].SubItems[2].Text;
                    comm.Parameters["@주소"].Value = listView1.Items[i].SubItems[3].Text;
                    comm.ExecuteNonQuery();
                }
                UserDB.conn.Close();
            }

            private void 열기ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                UserDB.openCon();

                listView1.Items.Clear();
                string query = "select * from address";
                OleDbCommand comm = new OleDbCommand(query, UserDB.conn);
                OleDbDataReader sr = comm.ExecuteReader();
                while (sr.Read())
                {
                    ListViewItem item = new ListViewItem(sr.GetString(0).Trim());
                    item.SubItems.Add(sr.GetString(1).Trim());
                    item.SubItems.Add(sr.GetString(2).Trim());
                    item.SubItems.Add(sr.GetString(3).Trim());
                    listView1.Items.Add(item);
                }
            }

            private void 검색ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                UserDB.openCon();

                string query = "select * from address";
                OleDbCommand comm = new OleDbCommand(query, UserDB.conn);
                OleDbDataReader sr = comm.ExecuteReader();

                Dialog_Form3 df3 = new Dialog_Form3();
                df3.ShowDialog();

                while (sr.Read())
                {
                    if (df3.textBox1.Text == sr.GetString(0).Trim())
                    {
                        listView1.Items.Clear();
                        ListViewItem item = new ListViewItem(sr.GetString(0).Trim());
                        item.SubItems.Add(sr.GetString(1).Trim());
                        item.SubItems.Add(sr.GetString(2).Trim());
                        item.SubItems.Add(sr.GetString(3).Trim());
                        listView1.Items.Add(item);
                    }
                }
            }

            private void 수정ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                string query = "select * from address";
                OleDbCommand comm = new OleDbCommand(query, UserDB.conn);
                OleDbDataReader sr = comm.ExecuteReader();

                Dialog_Form4 df4 = new Dialog_Form4();
                df4.ShowDialog();
            }
        }

        [Serializable]
        class ADD_CARD
        {
            private string name;
            private string sex;
            private string phone;
            private string address;


            public string Name
            {
                set
                {
                    name = value;
                }
                get
                {
                    return name;
                }
            }
            public string Sex
            {
                set
                {
                    sex = value;
                }
                get
                {
                    return sex;
                }
            }
            public string Phone
            {
                set
                {
                    phone = value;
                }
                get
                {
                    return phone;
                }
            }
            public string Address
            {
                set
                {
                    address = value;
                }
                get
                {
                    return address;
                }
            }

            public void Show()
            {
                MessageBox.Show("[이름] : " + name + "  [성별] : " + sex +
                    "  [전화번호] : " + phone + "  [주소] : " + address, "추가");
            }
        }

        [Serializable]
        class Myindexer
        {
            ArrayList al = new ArrayList();
            public ADD_CARD this[int index]
            {
                get
                {
                    if (index > -1 & index < al.Count)
                        return (ADD_CARD)al[index];
                    else
                        return null;
                }
                set
                {
                    if (index > -1 & index < al.Count)
                        al[index] = value;
                    else if (index == al.Count)
                        al.Add(value);
                    else
                        MessageBox.Show("인덱스 범위 초과!! [index] = " + index);
                }
            }

            public void Remove(int i)
            {
                al.RemoveAt(i);
            }

            public void Clear()
            {
                al.Clear();
            }
        }
    }



    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;

    namespace Access
    {
        public partial class Dialog_Form2 : Form
        {
            public Dialog_Form2()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }


    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;

    namespace Access
    {
        public partial class Dialog_Form4 : Form
        {
            private string name = "";
            private string sex = "";
            private string phone = "";
            private string address = "";

            private Form1 dig;

            public Dialog_Form4()
            {
                InitializeComponent();
                UserDB.openCon();
            }

            public Dialog_Form4(Form1 a)
            {
                InitializeComponent();
                dig = a;
            }

            private void button1_Click(object sender, EventArgs e)
            {

                string query = "select * from address";
                bool search = false;
                OleDbCommand comm = new OleDbCommand(query, UserDB.conn);
                OleDbDataReader sr = comm.ExecuteReader();

                while (sr.Read())
                {
                    if (textBox1.Text == sr.GetString(0).Trim())
                    {
                        textBox1.Text = sr.GetString(0).Trim();
                        textBox2.Text = sr.GetString(1).Trim();
                        textBox3.Text = sr.GetString(2).Trim();
                        textBox4.Text = sr.GetString(3).Trim();
                        search = true;
                    }
                }
                if (search == false)
                    MessageBox.Show("검색결과가 없습니다.", "알림");
                else
                {
                    name = textBox1.Text;
                    sex = textBox2.Text;
                    phone = textBox3.Text;
                    address = textBox4.Text;
                    MessageBox.Show("수정할 이름, 성별, 전화번호, 주소를 입력 하십시오.", "알림");
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {

                string update1 = "update address SET 이름 = " + "'" + textBox1.Text + "'" + "Where 이름 =" + "'" + name + "'";
                string update2 = "update address SET 성별 = " + "'" + textBox2.Text + "'" + "Where 성별 =" + "'" + sex + "'";
                string update3 = "update address SET 전화번호 = " + "'" + textBox3.Text + "'" + "Where 전화번호 =" + "'" + phone + "'";
                string update4 = "update address SET 주소 = " + "'" + textBox4.Text + "'" + "Where 주소 =" + "'" + address + "'";


                OleDbCommand comm1 = new OleDbCommand(update1, UserDB.conn);
                OleDbCommand comm2 = new OleDbCommand(update2, UserDB.conn);
                OleDbCommand comm3 = new OleDbCommand(update3, UserDB.conn);
                OleDbCommand comm4 = new OleDbCommand(update4, UserDB.conn);
                comm1.ExecuteNonQuery();
                comm2.ExecuteNonQuery();
                comm3.ExecuteNonQuery();
                comm4.ExecuteNonQuery();

                UserDB.conn.Close();
            }
        }

    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb;

    namespace Access
    {
        class UserDB
        {
            public static OleDbConnection conn = null;

            public static OleDbConnection openCon()
            {
                string conStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = access.accdb";
                conn = new OleDbConnection(conStr);
                conn.Open();
                return conn;
            }

            public static void closeCon()
            {
                conn.Close();
            }


        }
    }

    • 편집됨 gogoterran 2009년 12월 9일 수요일 오전 12:55
    2009년 12월 8일 화요일 오후 9:01

모든 응답

  • 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;

    namespace Access
    {
        public partial class Dialog_Form2 : Form
        {
            public Dialog_Form2()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }

    2009년 12월 8일 화요일 오후 9:01
  • 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;

    namespace Access
    {
        public partial class Dialog_Form4 : Form
        {
            private string name = "";
            private string sex = "";
            private string phone = "";
            private string address = "";

            private Form1 dig;

            public Dialog_Form4()
            {
                InitializeComponent();
                UserDB.openCon();
            }

            public Dialog_Form4(Form1 a)
            {
                InitializeComponent();
                dig = a;
            }

            private void button1_Click(object sender, EventArgs e)
            {

                string query = "select * from address";
                bool search = false;
                OleDbCommand comm = new OleDbCommand(query, UserDB.conn);
                OleDbDataReader sr = comm.ExecuteReader();

                while (sr.Read())
                {
                    if (textBox1.Text == sr.GetString(0).Trim())
                    {
                        textBox1.Text = sr.GetString(0).Trim();
                        textBox2.Text = sr.GetString(1).Trim();
                        textBox3.Text = sr.GetString(2).Trim();
                        textBox4.Text = sr.GetString(3).Trim();
                        search = true;
                    }
                }
                if (search == false)
                    MessageBox.Show("검색결과가 없습니다.", "알림");
                else
                {
                    name = textBox1.Text;
                    sex = textBox2.Text;
                    phone = textBox3.Text;
                    address = textBox4.Text;
                    MessageBox.Show("수정할 이름, 성별, 전화번호, 주소를 입력 하십시오.", "알림");
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {

                string update1 = "update address SET 이름 = " + "'" + textBox1.Text + "'" + "Where 이름 =" + "'" + name + "'";
                string update2 = "update address SET 성별 = " + "'" + textBox2.Text + "'" + "Where 성별 =" + "'" + sex + "'";
                string update3 = "update address SET 전화번호 = " + "'" + textBox3.Text + "'" + "Where 전화번호 =" + "'" + phone + "'";
                string update4 = "update address SET 주소 = " + "'" + textBox4.Text + "'" + "Where 주소 =" + "'" + address + "'";


                OleDbCommand comm1 = new OleDbCommand(update1, UserDB.conn);
                OleDbCommand comm2 = new OleDbCommand(update2, UserDB.conn);
                OleDbCommand comm3 = new OleDbCommand(update3, UserDB.conn);
                OleDbCommand comm4 = new OleDbCommand(update4, UserDB.conn);
                comm1.ExecuteNonQuery();
                comm2.ExecuteNonQuery();
                comm3.ExecuteNonQuery();
                comm4.ExecuteNonQuery();

                UserDB.conn.Close();
            }
        }

    }

    2009년 12월 8일 화요일 오후 9:02
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb;

    namespace Access
    {
        class UserDB
        {
            public static OleDbConnection conn = null;

            public static OleDbConnection openCon()
            {
                string conStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = access.accdb";
                conn = new OleDbConnection(conStr);
                conn.Open();
                return conn;
            }

            public static void closeCon()
            {
                conn.Close();
            }


        }
    }

    2009년 12월 8일 화요일 오후 9:02