DataAdapter의 기능
- 데이터베이스의 Connection으로부터 DataSet을 생성하는 중간 역할 담당
- DataSet을 채우고 데이터 소스를 업데이트하는 데 사용되는 SQL 명령 집합 및 데이터베이스 연결을 지원하는 클래스
- DataTable와 같은 비연결 지향적 클래스들이 데이터베이스와 연결
- Command로 작업할 때에는 데이터베이스와 연결된 상태에서 작업
- DataAdapter로 작업을 할 경우에는 DataSet이라는 결과를 얻은 후에 데이터베이스와의 연결을 끊고, DataSet 자체만으로도 작업을 수행한다.
- 작업이 끝난 후에는 DatatAdapter를 통해 DataSet의 변경된 부분이 실제 데이터베이스에 반영 가능
SelectCommand 속성
- SelectCommand 속성을 이용하여 원본 DB의 데이터 소스에서 레코드를 검색하는데 사용하는 SQL문이나 Proc를 설정할 수 있다.
- SelectCommand 에 설정된 SQL문을 실행했을때 검색결과가 없어서 SelectCommamd가 행을 반환하지 않으면 데이터 셋에 테이블이 추가되지 않는다.
- SelectCommand 문을 실행해서 나온 결과에 대해서 SqlDataAdapter클래스의 Fill메소드가 데이터 셋에 테이블을 추가하게 된다.
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand();
adapter.SelectCommand.Connection = sConnection;
adapter.SelectCommand.CommandText = Query;
InsertCommand 속성
- InsertCommand 속성을 이용하면 새로운 레코드를 데이터 소스에 삽입할 SQL 문이나 Proc를 설정할 수 있다.
- InsertCommand를 이용해서 데이터 셋에 자료를 추가하는 등의 처리를 할 수 있다
adapter.InsertCommand = new SqlCommand();
adapter.InsertCommand.Connection = sConnection;
adapter.InsertCommand.CommandText = Query;
adapter.InsertCommand.Parameters.Add("@name",SqlDbType.VarChar,10,"name");
adapter.Update(ds, "tb_insert");
DeleteCommad 속성
- DeleteCommand는 행을 삭제할때 사용되는 속성이다.
- DeleteCommand 속성을 이용해서 행(레코드)를 데이터 집합으로부터 삭제할 SQL 문이나 Proc를 가져오거나 설정할 수 있다.
adapter.DeleteCommand = new SqlCommand();
adapter.DeleteCommand.Connection=(SqlConnection)adapter.SelectCommand.Connection;
adapter.DeleteCommand = new SqlCommand();
adapter.DeleteCommand.CommandText = strUpdateQurey;
adapter.DeleteCommand.CommandType = CommandType.Text;
- 작업을 하다보면 여러가지 이유로 입력된 데이터를 수정해야 할 때가 있다.
- UpdateCommand 속성을 이용하면 데이터 소스에서 레코드를 업데이트 하는데 사용하는 SQL 문이나 Proc를 가져오거나 설정할 수 있다
adapter.UpdateCommand = new SqlCommand();
adapter.UpdateCommand.Connection=(SqlConnection)adapter.SelectCommand.Connection;
adapter.UpdateCommand.CommandText = strUpdateQuery;
adapter.UpdateCommand.CommandType = CommandType.Text;
adapter.UpdateCommand.Parameters.Add(myParam);
메소드 |
설명 |
Fill |
데이터 셋의 행을 데이터 소스와 일치하도록 한다. |
FillSchema |
데이터 셋에 DataTable을 추가하고 데이터 소스의 스키마와 일치하도록 스키마를 구성한다. |
Update |
데이터 셋과 동일하게 데이터 소스를 업데이트한다. |
string connString = "server = 504-24\\SQL2005;database=tempdb;";
connString = connString + "uid=Hello;password=dhtlak;";
SqlConnection sConnection = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand();
adapter.SelectCommand.Connection = sConnection;
String Query = "select * from titile";
adapter.SelectCommand.CommandText = Query;
DataSet ds = new DataSet("Mytable");
adapter.FillSchema(ds, SchemaType.Source, "titile");
adapter.Fill(ds, "titile");
DataRow[] r3 = ds.Tables["titile"].Select();
foreach (DataRow ro in r3)
{
Console.WriteLine(ro["titile_id"].ToString() + ro["type"].ToString());
}
'.NET > ADO.NET' 카테고리의 다른 글
DB에 이미지 저장 / 로드 하기 (0) | 2010.06.18 |
---|---|
DB모델링 (0) | 2010.05.28 |
DataSet (0) | 2010.04.02 |
DataView (0) | 2010.04.02 |
SqlDataReader (1) | 2010.04.02 |