Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

кожин / лаба 1

.txt
Скачиваний:
11
Добавлен:
20.03.2016
Размер:
3.35 Кб
Скачать
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace koj_4l
{
public partial class Form1 : Form
{
string constring = "Dsn=ilya_db;uid=mihajlovals;PWD=1234";
string osnova = "select * from clothes";
public int counter, id;
public Form1()
{
InitializeComponent();
label4.Visible = false;
Query(osnova);
}
private void Query(string str)
{
OdbcConnection con = new OdbcConnection(constring);
con.Open();
OdbcCommand cmd = new OdbcCommand(str, con);
try
{
OdbcDataReader reader = cmd.ExecuteReader();
OdbcDataAdapter DA = new OdbcDataAdapter(cmd);
DataSet ds = new DataSet();
reader.Close();
DA.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch
{
MessageBox.Show("Таблица пуста!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
con.Close();
}
/*Для команд delete и insert*/
private void QueryDelIns(string str)
{
OdbcConnection con = new OdbcConnection(constring);
con.Open();
OdbcCommand cmd = new OdbcCommand(str, con);
OdbcDataReader reader = cmd.ExecuteReader();
OdbcDataAdapter DA = new OdbcDataAdapter(cmd);
DataSet ds = new DataSet();
reader.Close();
con.Close();
}
private void button1_Click(object sender, EventArgs e)//добавление
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
string quer = "insert into clothes values(clothes_id.nextval, '" + textBox1.Text + "', " + textBox2.Text + ", '" + textBox3.Text + "')";
QueryDelIns(quer);
Query(osnova);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
label4.Visible = true;
}

private void button2_Click(object sender, EventArgs e)//удаление
{
int prov = dataGridView1.Rows.Count;
string z = dataGridView1.Rows[counter].Cells[1].Value.ToString();
string vt = dataGridView1.Rows[counter].Cells[4].Value.ToString();
string str = "delete from clothes where Id =" + id;//или counter
QueryDelIns(str);
Query(osnova);
}

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
for (counter = 0; counter < (dataGridView1.Rows.Count); counter++)
{
if (dataGridView1.Rows[counter].Selected)
{
id = (int)dataGridView1.Rows[counter].Cells[0].Value;
break;
}
}
}
}
}
Соседние файлы в папке кожин