본문 바로가기

   
Programming/ADO.NET

Winform 디비 연결

반응형


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;

using System.Data.SqlClient;

 

namespace WInform

{

       public partial class Form1 : Form

       {

             private SqlConnection con;

 

             public Form1()

             {

                    InitializeComponent();

                    con = new SqlConnection("server=localhost;database=ADONET;uid=sa;pwd=net401$!"); ;

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    if (con.State != ConnectionState.Open)

                    {

                           con.Open();

                           this.Text = con.State.ToString();

                    }

             }

             private void Form1_Load(object sender, EventArgs e)

             {

                    this.Text = con.State.ToString();

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    con.Close();

                    this.Text = con.State.ToString();

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    this.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;

using System.Data.SqlClient;

 

namespace WInform

{

       public partial class Connection : Form

       {

             private SqlConnection con;

 

             public Connection()

             {

                    InitializeComponent();

                    con = new SqlConnection("server=localhost;database=ADONET;uid=sa;pwd=net401$!"); ;

             }

 

             private void Connection_Load(object sender, EventArgs e)

             {

                    //로드 할때 open(); 버튼을 누를때마다 open(); 차이가 있다.

                    //로드 : 계속 연결상태 버튼 : 누를때 마다.

                    // 2항목은 모두 DB에게 무리가 간다.

                    //장기적으로 연결해야 한다면 로드나 다른 이벤트에서 OPEN 하도록 하는것이 좋다.

                    con.Open();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //추가하기

                    SqlCommand cmd = new SqlCommand();

                    cmd.Connection = con;

                    cmd.CommandText = "INSERT INTO tblMelon(title, singer) VALUES(@title, @singer)";

 

                    cmd.Parameters.Add(new SqlParameter("@title", SqlDbType.VarChar, 100));

                    cmd.Parameters.Add(new SqlParameter("@singer", SqlDbType.VarChar, 100));

 

                    cmd.Parameters["@title"].Value = textBox1.Text;

                    cmd.Parameters["@singer"].Value = textBox2.Text;

 

                    cmd.ExecuteNonQuery();

 

                    con.Close();

 

                    textBox1.Text = textBox2.Text = "";

                    MessageBox.Show("데이타 추가가 완료 되었습니다.");

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    this.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;

using System.Data.SqlClient;

 

namespace WInform

{

       public partial class Select : Form

       {

             public Select()

             {

                    InitializeComponent();

             }

 

             private void Select_Load(object sender, EventArgs e)

             {

                    //1. 연결 객체 생성 - 완료

                    //2. 연결 문자열 - 완료

                    //3. 연결

                    sqlConnection1.Open();

 

                    //4. 명령 객체 생성 - 완료

                    //5. 명령 - 연결 짝 - 완료

                    //6. 명령 CommendText

                    //7. 실행

                    SqlDataReader reader = sqlCommand1.ExecuteReader();

 

                    while (reader.Read())

                    {

                           comboBox1.Items.Add(reader["title"].ToString());

                    }

 

                    if (reader.NextResult())

                    {

                           if (reader.Read())

                           {

                                this.Text += " - 도서 권수 : " + reader[0].ToString() + "";

                           }

                    }

 

                    reader.Close();

                    sqlConnection1.Close();

             }

       }

}

 

 

 



 1. 도구상자 항목 선택에서 코넥션, 커맨드, 어댑터 항목 추가


2. 코넥션 디자인에서 던져서 속성창에서











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;

using System.Data.SqlClient;

 

namespace WInform

{

       public partial class SELECT02 : Form

       {

             public SELECT02()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    sqlConnection1.Open();

 

                    //입력 매개변수 처리

                    sqlCommand1.Parameters["@name"].Value = textBox1.Text;

                    sqlCommand1.Parameters["@age"].Value = textBox2.Text;

                    sqlCommand1.Parameters["@email"].Value = textBox3.Text;

                    sqlCommand1.Parameters["@address"].Value = textBox4.Text;

                    sqlCommand1.Parameters["@tel"].Value = textBox5.Text;

 

                    sqlCommand1.ExecuteNonQuery();

 

                    sqlConnection1.Close();

             }

       }

}

 

 

>                     this.Text = con.State.ToString();

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    this.Close();

             }

       }

}



 

 


반응형