
Лабораторная работа № 7
Задание:
Создать в Visual Studio .Net проект, на главной форме проекта создать меню с пунктом Таблицы и подпунктами Товары и Заказы.
Подключить к проекту Nu-get пакет, позволяющий работать с провайдером баз данных PostgreSQL.
Добавить в проект две новые формы и озаглавить их соответственно подпунктам главного меню.
Обеспечить отображение в созданных формах данных из таблиц товаров и заказов с помощью элементов типа TextBox.
В форме товаров обеспечить навигацию по записям с помощью элемента управления BindigNavigator.
В форме заказов обеспечить навигацию по записям с помощью пользовательских кнопок Button.
Обеспечить корректность закрытия всех форм проекта.
Текст программы:
Форма Товары:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Npgsql;
namespace Лабораторная_работа__5
{
public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
}
DataSet ds;
BindingSource b = new BindingSource();
private void Form8_Load(object sender, EventArgs e)
{
string Con;
Con = "Host=192.168.55.2; Database=Sale1;Username=Admin;" + "Password=Qwe88888;Persist Security Info=true";
NpgsqlConnection NC = new NpgsqlConnection(Con);
NC.Open();
string Sel = "SELECT * from public.product";
NpgsqlDataAdapter NA = new NpgsqlDataAdapter(Sel, NC);
ds = new DataSet();
ds.Clear();
NA.Fill(ds, "product");
b.DataSource = ds.Tables["product"];
textBox1.DataBindings.Add("Text", b, "key_product");
textBox2.DataBindings.Add("Text", b, "category");
textBox3.DataBindings.Add("Text", b, "name_product");
textBox4.DataBindings.Add("Text", b, "price");
NC.Close();
bindingNavigator1.BindingSource = b;
}
}
}
Форма Заказы:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Npgsql;
namespace Лабораторная_работа__5
{
public partial class Form9 : Form
{
public Form9()
{
InitializeComponent();
}
DataSet ds;
private void Form9_Load(object sender, EventArgs e)
{
string Con;
Con = "Host=192.168.55.2; Database=Sale1;Username=Admin;" + "Password=Qwe88888;Persist Security Info=true";
NpgsqlConnection NC = new NpgsqlConnection(Con);
NC.Open();
string Sel = "SELECT * from public.order";
NpgsqlDataAdapter NA = new NpgsqlDataAdapter(Sel, NC);
ds = new DataSet();
ds.Clear();
NA.Fill(ds,"order");
textBox1.DataBindings.Add(new Binding("Text", ds, "order.key_client"));
textBox2.DataBindings.Add(new Binding("Text", ds, "order.key_product"));
textBox3.DataBindings.Add(new Binding("Text", ds, "order.date_order"));
textBox4.DataBindings.Add(new Binding("Text", ds, "order.quantity_order"));
textBox5.DataBindings.Add(new Binding("Text", ds, "order.date_sale"));
textBox6.DataBindings.Add(new Binding("Text", ds, "order.quantity_sale"));
NC.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.BindingContext[ds, "order"].Position += 1;
}
private void button3_Click(object sender, EventArgs e)
{
this.BindingContext[ds, "order"].Position -= 1;
}
private void button1_Click(object sender, EventArgs e)
{
this.BindingContext[ds, "order"].Position = 0;
}
private void button4_Click(object sender, EventArgs e)
{
this.BindingContext[ds, "order"].Position = this.BindingContext[ds, "order"].Count - 1;
}
}
}
Контрольный пример:
Форма Товары:
Форма Заказы: