WinBinding(SqlDataAdapter클래스 내용에서 보충)
.NET프로그래밍/ADO.NET 2009. 9. 30. 15:54 |
* "윈폼"에서 '새 프로젝트 만들기'로 프로젝트 실행
==> Form1.cs [디자인]
-------------------------------------------------------------------------------------
==> Form1.cs 소스
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.SqlClient;
namespace WinBinding
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds; // 주소록 담을 그릇
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(
"server=.;database=AddressBook;uid=AddressBook;pwd=6750440;");
SqlCommand cmd = new SqlCommand("Select * From AddressBook", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "AddressBook"); // 채우기
//[!] 바인딩하기 : 컨트롤과 필드를 서로 연결?
// textBox1의 Text 속성을 ds에 담겨있는 AddressBook 테이블의 Name필드 연결
textBox1.DataBindings.Add("Text", ds, "AddressBook.Name");
textBox2.DataBindings.Add("Text", ds, "AddressBook.Mobile");
textBox3.DataBindings.Add("Text", ds, "AddressBook.Email");
}
private void button1_Click(object sender, EventArgs e)
{
BindingContext[ds, "AddressBook"].Position--; // 이전 레코드로 이동
}
private void button2_Click(object sender, EventArgs e)
{
BindingContext[ds, "AddressBook"].Position++; // 다음 레코드로 이동
}
}
}
// 지금 실습한 "바인딩하기"는 윈폼레벨이다.
-------------------------------------------------------------------------------------
[실행결과]
--> 실행(F5)하였을 때 첫 화면. "폼"이 로드되자마자 [SQL Server]에서 [dbo.AddressBook]테이블의 첫번째 레코드의 데이터('이효리', '010-1234-1234', 'aaa@aaa.com')가 "폼"의 각 "텍스트박스"에 연결되어 출력된다.
--> '다음'버튼을 눌렀을 때의 화면([dbo.AddressBook]테이블의 두번째 레코드의 데이터 출력)
--> '다음'버튼을 눌렀을 때의 화면([dbo.AddressBook]테이블의 세번째 레코드의 데이터 출력)
'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글
LINQ to SQL (0) | 2009.09.30 |
---|---|
LINQ to DataSet (0) | 2009.09.30 |
(교재실습) WinCommandBuilder(SqlCommandBuilder 클래스) (0) | 2009.09.30 |
(교재실습) (DataAdapter 연습) (0) | 2009.09.30 |
25. 관계와 제약조건걸기(DataRelation 클래스와 Constraints) (0) | 2009.09.30 |