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

кожин / лаба 2

.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;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace koj_4l
{
public partial class Form1 : Form
{
string constring = "Dsn=StudBD; uid=Lerick_; PWD=03051994";
string osnova = "select Id, name, price, description from product";
public int counter, id;
public Form1()
{
InitializeComponent();
}

private void Query(string str)
{
using (OdbcConnection con = new OdbcConnection(constring))
{
con.Open();
OdbcCommand cmd = new OdbcCommand(str, con);
OdbcDataReader reader = cmd.ExecuteReader();
OdbcDataAdapter DA = new OdbcDataAdapter(cmd);
reader.Close();
DataSet ds = new DataSet();
DA.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
/*Для команд delete и insert*/
private void QueryDelIns(string str)
{
using (OdbcConnection con = new OdbcConnection(constring))
{
con.Open();

using (OdbcCommand cmd = new OdbcCommand(str, con))
{
cmd.ExecuteNonQuery();
}
}
}
private void button1_Click(object sender, EventArgs e)//добавление
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
label4.Visible = false;
new Thread((o) =>
{
QueryDelIns(o.ToString());
Invoke(new Action(() =>
{
Query(osnova);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}));
}).Start("insert into product values(S_FOR_PRODUCT.NEXTVAL, '" +
textBox1.Text + "', " + textBox2.Text + ", '" + textBox3.Text + "')");


}
else
label4.Visible = true;
}

private void button2_Click(object sender, EventArgs e)//удаление
{
new Thread((o) =>
{
QueryDelIns(o.ToString());
Invoke(new Action(() => Query(osnova)));
}).Start("delete from product where Id =" + id);
}

private void Form1_Load(object sender, EventArgs e)
{
label4.Visible = false;
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.Parse(dataGridView1.Rows[counter].Cells[0].Value.ToString());
break;
}
}
}
}
}
Соседние файлы в папке кожин