DataRow 객체를 이용해서 CRUD를 처리하면,
구현과 유지 보수 모두 쉬울 것 같다.
Effective C#에서도 개인 자료 구조로 만든 소스보다
DataSet을 이용하라고 되어 있다.
1]
http://ww.google.com/codesearch 찾은 소스
private void CreateNewDataRow()
{
// Use the MakeTable function below to create a new table.
DataTable myTable;
myTable = MakeNamesTable();
// Once a table has been created, use the NewRow to create a DataRow.
DataRow myRow;
myRow = myTable.NewRow();
// Then add the new row to the collection.
myRow["fName"] = "John";
myRow["lName"] = "Smith";
myTable.Rows.Add(myRow);
foreach(DataColumn dc in myTable.Columns)
Console.WriteLine(dc.ColumnName);
dataGrid1.DataSource=myTable;
}
private DataTable MakeNamesTable()
{
// Create a new DataTable titled 'Names.'
DataTable namesTable = new DataTable("Names");
// Add three column objects to the table.
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "id";
idColumn.AutoIncrement = true;
namesTable.Columns.Add(idColumn);
DataColumn fNameColumn = new DataColumn();
fNameColumn.DataType = System.Type.GetType("System.String");
fNameColumn.ColumnName = "Fname";
fNameColumn.DefaultValue = "Fname";
namesTable.Columns.Add(fNameColumn);
DataColumn lNameColumn = new DataColumn();
lNameColumn.DataType = System.Type.GetType("System.String");
lNameColumn.ColumnName = "LName";
namesTable.Columns.Add(lNameColumn);
// Create an array for DataColumn objects.
DataColumn [] keys = new DataColumn [1];
keys[0] = idColumn;
namesTable.PrimaryKey = keys;
// Return the new DataTable.
return namesTable;
}
DataTable GetData()
{
// This method creates a DataTable with four rows. Each row has the
// following schema:
// PictureID int
// PictureURL string
// Title string
// DateAdded datetime
DataTable dt = new DataTable();
// define the table's schema
dt.Columns.Add(new DataColumn("PictureID", typeof(int)));
dt.Columns.Add(new DataColumn("PictureURL", typeof(string)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("DateAdded", typeof(DateTime)));
// Create the four records
DataRow dr = dt.NewRow();
dr["PictureID"] = 1;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/Blue hills.jpg");
dr["Title"] = "Blue Hills";
dr["DateAdded"] = new DateTime(2005, 1, 15);
dt.Rows.Add(dr); dr = dt.NewRow();
dr["PictureID"] = 2;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/Sunset.jpg");
dr["Title"] = "Sunset";
dr["DateAdded"] = new DateTime(2005, 1, 21);
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["PictureID"] = 3;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/Water lilies.jpg");
dr["Title"] = "Water Lilies";
dr["DateAdded"] = new DateTime(2005, 2, 1);
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["PictureID"] = 4;
dr["PictureURL"] = ResolveUrl("~/DisplayingImages/Images/Winter.jpg");
dr["Title"] = "Winter";
dr["DateAdded"] = new DateTime(2005, 2, 18);
dt.Rows.Add(dr);
return dt;
}
2]
The various objects in consideration under the disconnected model of ADO.NET are asfollows:?
DataSet: The DataSet is at the central core of the disconnected mode of ADO.NET dataaccess.
The best way to think of a DataSet is like having your own very mini relationaldatabase
management system (RDBMS) completely represented in memory. While itisn’t quite an RDBMS
and should never be thought to replace an RDBMS, it helps tounderstand a DataSet
if its various components are connected on a one-to-one basiswith most major RDBMS objects.
Also, it is important to realize that DataSets are availableat System.Data.DataSet, i.e.,
above any .NET provider, thus making them .NET dataprovider?independent
(more about .NET data providers in the next section).
A DataSetcan also be thought of as a logical collection of DataTables and DataRelations.?
DataTable: A DataTable is most similar to a table in a database.
It consists of DataColumns,DataRows, and various constraints set upon them.
It stores data in a row/column format.Starting with ADO.NET 2.0, a DataTable is fully convertible
to XML and can be serializedjust like a DataSet. For data access needs where your DataSet might
contain only oneDataTable, it may make more sense to use a DataTable instead.
As you’ll see in futurechapters, this is not only more convenient,
but it’s also better performing.DataRow: One of the properties of
DataTable is Rows of DataRowCollection type, whichrepresents an enumerable collection of
DataRow objects. As data is filled into a DataTable,the DataRowCollection gets new DataRow objects
added to itself. The best logical equivalentof a DataRow in a database is a row in a table.?
DataColumn: A DataTable also contains a Columns property of DataColumnCollection type.Essentially,
this represents the structure of a DataTable. The best logical equivalent ofa DataColumn object
in a database is an individual column in a given table in a database.?
DataView: A DataView is most similar to a view in a database. A DataView allows you to createa “view” on a DataTable and view a subset of the data based on a preset conditionspecified in its Filter property.
You could also use the Sort property to sort the filteredsubset of the DataTable’s data.
One DataTable can have multiple views defined on it.?
Constraint: A DataTable contains yet another property called Constraints of ConstraintsCollection type.
This lets you create ForeignKeyConstraint or UniqueConstraintobjects and associate various columns to certain conditions based on which data in theDataTable must pass for it to exist in the DataTable.
The most logical equivalent ofa ForeignKeyConstraint is a foreign key in a database,
and UniqueConstraint specifiesa Unique condition on a given column in a database.?
DataRelation: A DataSet, like a database, might contain various interrelated tables.
A DataRelation object lets you specify relations between various tables that allowyou to
both validate data across tables and browse parent and child rows in variousDataTables.
Its most logical equivalent is a foreign key specified between two tablesin a database.
The difference between a ForeignKeyConstraint and a DataRelation isthat a DataRelation, in addition to validating data, gives you a convenient mechanismto browse parent and child rows in a DataSet.
3]
http://blog.naver.com/greeniiii?Redirect=Log&logNo=50003866200
http://www.scitech.co.kr/upload/book_image/s_017/ASP.NET-FF.pdf
http://support.microsoft.com/default.aspx?scid=kb;KO;308909
http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx
http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part2.aspx 야근금지 책
구현(action) 후
참 편하고 싶더라. 타이핑이 늘어나는 게 아쉽지만,
code generation 기법을 사용하면 이것도 절반의 노력으로으로 줄일 수 있지
않을까! 하튼 구현 완료! dt 함수 중에 AcceptChange 등등도 알아 둬야 될 method!
dt.Rows.Add(dr);
dt.AcceptChanges();if (string.IsNullOrEmpty(hfEmpno.Value))
{
dr.SetAdded();
}
else
{
dr.SetModified();
}
return dt;
'컴퓨터(InfoTech)' 카테고리의 다른 글
| code snippet, code generation , xml 조작함수...... (0) | 2007/10/11 |
|---|---|
| JAVA 쪽 눈돌리기 (0) | 2007/10/08 |
| DataRow 객체의 DataRowState, DataRowVersion.Original (0) | 2007/10/04 |
| TextBox ReadOnly=true일 경우 postback시 값을 가지고 못하는 경우 (0) | 2007/10/03 |
| sql 문 - 모르는 것은 모른다고 하자! 그리고 모르는 것은 배워서 정확히 알자 (1) | 2007/10/02 |
| p-camp 2회 ... 10/10 평일이라 참석 못한당 *_* (0) | 2007/10/02 |



