ADO.NET - . , , ( DbDataAdapter), .
. . DataSet , . , DataSet , , .
, , , . (DataAdapter) DataSet. DataSet , DataTable, DataRow DataColumn.
ADO.NET DataSet . - DataSet .
DataSet . ADO.NET : SQL Server, Microsoft Access, XML-.
Tables. DataSet , Tables.
Tables (, , ) . , , DataSet . DataSet ADO.NET, ; , . , .
DataSet , , , , .
DataSet ( ). .
, DataSet. DataSet Merge, DataSet.
|
|
- DataSet ( ) . :
- ( );
- DataSet;
- DataSet ( ).
, DataSet .
DataSet . , .
DataSet , .
DataSet ( Table) Relation.
DataSet , , . , , , . "" "" , . , ( !). DataSet .
.
DataSet :
- (, ) DataTable ( , DataSet);
- DataRelations, DataSet.
DataSet , , , . , DataSet.
DataSet . () .
, , , DataSet DataAdapter, DataSet.
DataAdapter .
DataAdapter . ADO.NET. DataSet DataSet .
DataAdapter Fill(), DataSet. Fill() DataAdapter DataTable DataSet , .
|
|
, , Update() . Update() DataSet.
, DataAdapter .
DataAdapter .
, . DataAdapter DataSet .
1. SelectCommand ( sql) , . Fill() DataTable DataSet.
2. InsertCommand ( sql) , .
3. DeleteCommand ( sql) , .
4. UpdateCommand ( sql) , .
, (, , ). ADO.NET. PostgreSQL. : , .
1. PostgreSQL, . PostgreSQL univer, Groups Students. :
--
CREATE TABLE Groups(
id bigserial NOT NULL,
groupname varchar(20),
curatorname varchar(20),
headmanname varchar(20),
PRIMARY KEY (id));
--
CREATE TABLE Students(
id bigserial NOT NULL,
firstname varchar(20),
lastname varchar(20),
sex varchar(1) NOT NULL CHECK (sex = '' or sex = ''),
age integer,
groupid bigint,
PRIMARY KEY (id),
FOREIGN KEY (groupid) REFERENCES Groups ON UPDATE CASCADE ON DELETE CASCADE);
2. Windows Forms.
3. PostgreSQL. http://www.postgresql.org/. Npgsql2.0.10-bin-ms.net.zip. Npgsql.dll Mono.Security.dll.
4. Npgsql.dll ( , , , ).
3.2 Npgsql.dll
5. . MenuStrip, , , .
6. SplitConteiner , .
7. GoupBox. GoupBox Text , Text , GoupBox Dock Fill.
8. GoupBox DataGridView. Dock Fill. DataGridView , . DataGridView : ReadOnly True, MultiSelect False, RowHeadersVisible False, SelectionMode FullRowSelect, AllowUserToAddRows - False.
|
|
9. ContextMenuStrip, DataGridView, DataGridView ContextMenuStrip. , , , .
10. . , - , . , , , Windows Forms, Windows Forms.
11. . Form2 . TextBox Button. Button . TextBox , , , , . Button . Label. 3.3.
3.3
12. Form2 CSharp. :
private Form1 perent;
. :
public void setPerent(Form1 perent)
{
this.perent = perent;
}
13. :
//
private void button1_Click(object sender, EventArgs e)
{
string server = textBox1.Text;
int port = Int32.Parse(textBox2.Text);
string database = textBox3.Text;
string user = textBox4.Text;
string passwd = textBox5.Text;
NpgsqlConnection connection =
perent.Connect(server, port, database, user, passwd);
perent.setConnection(connection);
perent.FillDataGridView1ByGroups();
this.Visible = false;
}
Connect .
14. Form1 CSharp. :
//
private NpgsqlConnection connection = null;
// DataSet
private DataSet dataSet = null;
// DataAdapter
private NpgsqlDataAdapter groupDataAdapter = null;
private NpgsqlDataAdapter studentDataAdapter = null;
//
private Form2 form2 = null;
private Form3 form3 = null;
private Form4 form4 = null;
connection:
public void setConnection(NpgsqlConnection connection)
{
this.connection = connection;
}
15. DataSet Form2:
// DataSet
private DataSet getDataSet()
{
if (dataSet == null)
{
dataSet = new DataSet();
dataSet.Tables.Add("Groups");
dataSet.Tables.Add("Students");
}
return dataSet;
}
//
public Form2 getForm2()
{
if (form2 == null)
{
form2 = new Form2();
form2.setPerent(this);
}
return form2;
}
16. :
//
|
|
public NpgsqlConnection Connect(string host, int port, string database,
string user, string parol)
{
NpgsqlConnectionStringBuilder stringBuilder =
new NpgsqlConnectionStringBuilder();
stringBuilder.Host = host;
stringBuilder.Port = port;
stringBuilder.UserName = user;
stringBuilder.Password = parol;
stringBuilder.Database = database;
stringBuilder.Timeout = 30;
NpgsqlConnection connection =
new NpgsqlConnection(stringBuilder.ConnectionString);
connection.Open();
return connection;
}
17. dataGridView1 dataGridView2 :
// DataGridView1
public void FillDataGridView1ByGroups()
{
getDataSet().Tables["Groups"].Clear();
groupDataAdapter = new NpgsqlDataAdapter(
"SELECT * FROM Groups", connection);
new NpgsqlCommandBuilder(groupDataAdapter);
groupDataAdapter.Fill(getDataSet(), "Groups");
dataGridView1.DataSource = getDataSet().Tables["Groups"];
}
// DataGridView2
public void FillDataGridView2ByStudents(string groupName)
{
getDataSet().Tables["Students"].Clear();
studentDataAdapter = new NpgsqlDataAdapter(
"SELECT Students.id, firstname, lastname, sex, age, groupid " +
"FROM Groups, Students " +
"WHERE Groups.id = Students.groupid AND groupname = '" +
groupName + "'", connection);
new NpgsqlCommandBuilder(studentDataAdapter);
studentDataAdapter.Fill(dataSet, "Students");
dataGridView2.DataSource = getDataSet().Tables["Students"];
}
18. :
// " "
private void ToolStripMenuItem_Click(
object sender, EventArgs e)
{
getForm2().Visible = true;
}
// " "
private void ToolStripMenuItem_Click(
object sender, EventArgs e)
{
connection.Close();
}
, , .
19. Form3 . , TextBox Button. TextBox , , . , . Label. 3.4.
3.4
20. Form3 CSharp. :
private Form1 perent = null;
private int row;
, .
21. :
public void setPerent(Form1 perent)
{
this.perent = perent;
}
public void setRow(int row)
{
this.row = row;
}
22. :
public void setTextBox1Text(string text)
{
textBox1.Text = text;
}
public void setTextBox2Text(string text)
{
textBox2.Text = text;
}
public void setTextBox3Text(string text)
{
textBox3.Text = text;
}
public void setButton1Visible(bool value)
{
this.button1.Visible = value;
}
public void setButton2Visible(bool value)
{
this.button2.Visible = value;
}
23. :
//
private void button1_Click(object sender, EventArgs e)
{
perent.AddGroup(textBox1.Text, textBox2.Text, textBox3.Text);
perent.FillDataGridView1ByGroups();
this.Visible = false;
}
//
private void button2_Click(object sender, EventArgs e)
{
perent.UpdateGroup(row, textBox1.Text, textBox2.Text, textBox3.Text);
perent.FillDataGridView1ByGroups();
this.Visible = false;
}
AddGroup UpdateGroup .
24. Form1 ( ) CSharp. Form3 ( ):
//
|
|
public Form3 getForm3()
{
if (form3 == null)
{
form3 = new Form3();
form3.setPerent(this);
}
return form3;
}
25. :
//
public void AddGroup(string groupName,
string curatorName, string headmanName)
{
getDataSet().Tables["Groups"].Rows.Add(0, groupName,
curatorName, headmanName);
groupDataAdapter.Update(getDataSet(), "Groups");
}
//
public void UpdateGroup(int row, string groupName,
string curatorName, string headmanName)
{
getDataSet().Tables["Groups"].Rows[row]["groupname"] = groupName;
getDataSet().Tables["Groups"].Rows[row]["curatorname"] = curatorName;
getDataSet().Tables["Groups"].Rows[row]["headmanname"] = headmanName;
groupDataAdapter.Update(getDataSet(), "Groups");
}
26. , , :
// " "
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
getForm3().Visible = true;
getForm3().setButton1Visible(true);
getForm3().setButton2Visible(false);
getForm3().setTextBox1Text("");
getForm3().setTextBox2Text("");
getForm3().setTextBox3Text("");
}
// " "
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
DialogResult dr = MessageBox.Show(" ?", "",
MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
getDataSet().Tables["Groups"].Rows[selectedRow].Delete();
groupDataAdapter.Update(getDataSet(), "Groups");
getDataSet().Clear();
FillDataGridView1ByGroups();
}
}
// " "
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string groupName = (string)getDataSet().Tables["Groups"]
.Rows[selectedRow].ItemArray[1];
string curatorName = (string)getDataSet().Tables["Groups"]
.Rows[selectedRow].ItemArray[2];
string headmanName = (string)getDataSet().Tables["Groups"]
.Rows[selectedRow].ItemArray[3];
getForm3().Visible = true;
getForm3().setButton1Visible(false);
getForm3().setButton2Visible(true);
getForm3().setTextBox1Text(groupName);
getForm3().setTextBox2Text(curatorName);
getForm3().setTextBox3Text(headmanName);
getForm3().setRow(selectedRow);
}
27. dataGridView1, dataGridView2 :
// DataGridView1
private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key = (string)dataGridView1.Rows[selectedRow].Cells[1].Value;
FillDataGridView2ByStudents(key);
}
28. . Form4 ( ) . 4 TextBox Button. TextBox, , , , - . , . TextBox Label. 3.5.
3.5
29. Form4 CSharp. :
private Form1 perent = null;
private string groupName;
private long groupId;
private int row;
, , , .
30. :
public void setPerent(Form1 perent)
{
this.perent = perent;
}
public void setGroupName(string groupName)
{
this.groupName = groupName;
}
public void setGroupId(long groupId)
{
this.groupId = groupId;
}
public void setRow(int row)
{
this.row = row;
}
31. :
public void setTextBox1Text(string text)
{
textBox1.Text = text;
}
public void setTextBox2Text(string text)
{
textBox2.Text = text;
}
public void setTextBox3Text(string text)
{
textBox3.Text = text;
}
public void setTextBox4Text(string text)
{
textBox4.Text = text;
}
public void setButton1Visible(bool value)
{
this.button1.Visible = value;
}
public void setButton2Visible(bool value)
{
this.button2.Visible = value;
}
32. :
//
private void button1_Click(object sender, EventArgs e)
{
perent.AddStudent(textBox1.Text, textBox2.Text,
textBox3.Text, Int16.Parse(textBox4.Text), groupId);
perent.FillDataGridView2ByStudents(groupName);
this.Visible = false;
}
//
private void button2_Click(object sender, EventArgs e)
{
perent.UpdateStudent(row, textBox1.Text, textBox2.Text,
textBox3.Text, System.Convert.ToInt32(textBox4.Text));
perent.FillDataGridView2ByStudents(groupName);
this.Visible = false;
}
AddStudent, UpdateStudent .
33. Form1 ( ) CSharp. Form4:
//
public Form4 getForm4()
{
if (form4 == null)
{
form4 = new Form4();
form4.setPerent(this);
}
return form4;
}
34. :
//
public void AddStudent(string firstName,
string secondName, string sex, int age, long groupId)
{
getDataSet().Tables["Students"]
.Rows.Add(0, firstName, secondName, sex, age, groupId);
studentDataAdapter.Update(getDataSet(), "Students");
}
//
public void UpdateStudent(int row, string firstName,
string secondName, string sex, int age)
{
getDataSet().Tables["Students"].Rows[row]["firstname"] = firstName;
getDataSet().Tables["Students"].Rows[row]["lastname"] = secondName;
getDataSet().Tables["Students"].Rows[row]["sex"] = sex;
getDataSet().Tables["Students"].Rows[row]["age"] = age;
studentDataAdapter.Update(getDataSet(), "Students");
}
35. , :
// " "
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
getForm4().Visible = true;
getForm4().setButton1Visible(true);
getForm4().setButton2Visible(false);
getForm4().setTextBox1Text("");
getForm4().setTextBox2Text("");
getForm4().setTextBox3Text("");
getForm4().setTextBox4Text("");
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
long groupId = (long)getDataSet().Tables["Groups"]
.Rows[selectedRow].ItemArray[0];
string groupName = (string)getDataSet().Tables["Groups"]
.Rows[selectedRow].ItemArray[1];
getForm4().setGroupName(groupName);
getForm4().setGroupId(groupId);
}
// " "
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView2.SelectedCells[0].RowIndex;
DialogResult dr = MessageBox.Show(" ?", "",
MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
getDataSet().Tables["Students"].Rows[selectedRow].Delete();
studentDataAdapter.Update(getDataSet(), "Students");
string key = (string)dataGridView1
.Rows[selectedRow].Cells[1].Value;
FillDataGridView2ByStudents(key);
}
}
// " "
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView2.SelectedCells[0].RowIndex;
string firstName = (string)getDataSet().Tables["Students"]
.Rows[selectedRow].ItemArray[1];
string secondName = (string)getDataSet().Tables["Students"]
.Rows[selectedRow].ItemArray[2];
string sex = (string)getDataSet().Tables["Students"]
.Rows[selectedRow].ItemArray[3];
int age = (int)getDataSet().Tables["Students"]
.Rows[selectedRow].ItemArray[4];
string sAge = System.Convert.ToString(age);
getForm4().Visible = true;
getForm4().setButton1Visible(false);
getForm4().setButton2Visible(true);
getForm4().setTextBox1Text(firstName);
getForm4().setTextBox2Text(secondName);
getForm4().setTextBox3Text(sex);
getForm4().setTextBox4Text(sAge);
getForm4().setRow(selectedRow);
int selectedRow1 = dataGridView1.SelectedCells[0].RowIndex;
string groupName = (string)getDataSet().Tables["Groups"]
.Rows[selectedRow1].ItemArray[1];
getForm4().setGroupName(groupName);
}
3.6.
3.6
, , . , , 3.1. .
3.1
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) |
- ;
- ;
- ;
- ;
- ( , );
- .
1. ADO.NET?
2. ?
3. ?
4. ?
4 4
Fluent NHibernate
ORM Fluent NHibernate, NHibernate.
ORM
- ORM (object relation mapping) - . - , .
- ORM (Object Relation Mapping) , - - / . - , - .
.
.NET - XML- .
Reflection :
1. , , , , . .
2. , , , , .
3. ( ), . Reflection. Reflection.
XML- . . :
1. . .
2. . , , .
ORM .NET LINQ to Entities, NHibernate, Fluent NHibernate. ORM Fluent NHibernate .
ORM Fluent NHibernate , . , NHibernate map-. map- , , . map- , , , .
map- XML NHibernate , Fluent NHibernate - .