Читайте также:
|
|
ПРИЛОЖЕНИЕ А Диаграмма вариантов использования
ПРИЛОЖЕНИЕ Б Листинг программы
ПРИЛОЖЕНИЕ А
Диаграмма вариантов использования
ПРИЛОЖЕНИЕ Б
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Globalization;
namespace OOP_Kurs
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
private static string sourse = @"charset = 'utf8'; Database = OOP_Praktika; Data Source = localhost; User Id = root; Password = '';";
private static MySqlConnection con;
private DataTable GetTable(string queryString)
{
DataTable dt = new DataTable();
con = new MySqlConnection(sourse);
MySqlCommand com = new MySqlCommand(queryString, con);
try
{
con.Open();
using (MySqlDataReader dr = com.ExecuteReader())
{
if (dr.HasRows) dt.Load(dr);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return dt;
}
private void buttonShow_Click(object sender, EventArgs e)
{
con = new MySqlConnection(sourse);
try
{
con.Open();
dataGridView1.DataSource = GetTable(@"Select * from BD order by ID;");
}
catch
{
MessageBox.Show("Ошибка");
}
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
this.buttonShow_Click(this, new EventArgs());
}
private void checkBoxAddId_CheckedChanged(object sender, EventArgs e)
{
textBox1.Enabled =!textBox1.Enabled;
}
private void Warning(TextBox tb, bool isError)
{
Color color = Color.FromName((isError)?"Red":"Black");
tb.ForeColor = color;
}
private void buttonAdd_Click(object sender, EventArgs e)
{
int warning = 0, temp;
double tempd;
string Add = @"insert into BD(";
if (textBox1.Enabled)
if (!Int32.TryParse(textBox1.Text, out temp)) { Warning(textBox1, true); ++warning; }
else { Warning(textBox1, false); Add += "ID,"; }
Add += "Name,Price,Quantity,Matherial,Height,Width,Depth,Weight,Color) values (";
if (textBox1.Enabled && Int32.TryParse(textBox1.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out temp)) { Warning(textBox1, false); Add += textBox1.Text + ","; }
if (textBox2.Text.Length == 0) { Warning(textBox2, true); ++warning; }
else { Warning(textBox3, false); Add += "'" + textBox2.Text + "',"; }
if (!Double.TryParse(textBox3.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out tempd)) { Warning(textBox3, true); ++warning; }
else
{
Warning(textBox3, false);
string loopstr = textBox3.Text, str = "";
foreach (char loopch in loopstr)
{
char ch = loopch;
if (ch == '.') ch = ',';
str += ch;
}
Add += "'" + str + "',";
}
if (!Int32.TryParse(textBox4.Text, out temp)) { Warning(textBox4, true); ++warning; }
else { Warning(textBox4, false); Add += textBox4.Text + ","; }
if (textBox5.Text.Length == 0) { Warning(textBox5, true); ++warning; }
else { Warning(textBox5, false); Add += "'" + textBox5.Text + "',"; }
if (!Int32.TryParse(textBox6.Text, out temp)) { Warning(textBox6, true); ++warning; }
else { Warning(textBox6, false); Add += textBox6.Text + ","; }
if (!Int32.TryParse(textBox7.Text, out temp)) { Warning(textBox7, true); ++warning; }
else { Warning(textBox7, false); Add += textBox7.Text + ","; }
if (!Int32.TryParse(textBox8.Text, out temp)) { Warning(textBox8, true); ++warning; }
else { Warning(textBox8, false); Add += textBox8.Text + ","; }
if (!Double.TryParse(textBox9.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out tempd)) { Warning(textBox9, true); ++warning; }
else
{
Warning(textBox9, false);
string loopstr = textBox9.Text, str = "";
foreach (char loopch in loopstr)
{
char ch = loopch;
if (ch == '.') ch = ',';
str += ch;
}
Add += "'" + str + "',";
}
if (textBox10.Text.Length == 0) { Warning(textBox10, true); ++warning; }
else { Warning(textBox10, false); Add += "'" + textBox10.Text + "');"; }
if (warning > 0)
{
MessageBox.Show("Ошибка. Неверный формат данных");
return;
}
try
{
con = new MySqlConnection(sourse);
con.Open();
MySqlCommand com = new MySqlCommand(Add, con);
MySqlDataReader reader = com.ExecuteReader();
MessageBox.Show("Запись добавлена в таблицу");
con.Close();
}
catch (Exception ex)
{
if (ex.Message.Contains("Duplicate entry")) MessageBox.Show("Ошибка. Запись с таким ID уже существует");
else MessageBox.Show("Ошибка:\n" + ex.Message);
}
this.buttonShow_Click(this, new EventArgs());
}
private void buttonDelete_Click(object sender, EventArgs e)
{
//Удаление для выделенной строки
if (dataGridView1.SelectedRows.Count > 0)
{
string str = "";
for (int i = 0; i < dataGridView1.SelectedRows.Count; ++i)
str += "id = " + dataGridView1.SelectedRows[i].Cells[0].Value + " or ";
str = str.Remove(str.Length - 3);
dataGridView1.DataSource = GetTable(@"delete from BD where " + str + ";");
}
// Удаление для выделенной ячейки
if (dataGridView1.SelectedCells.Count > 0)
{
DataGridViewSelectedCellCollection cells = dataGridView1.SelectedCells;
string str = "";
for (int i = 0; i < cells.Count; ++i)
str += "id = " + dataGridView1.Rows[cells[i].RowIndex].Cells[0].Value + " or ";
str = str.Remove(str.Length - 3);
dataGridView1.DataSource = GetTable(@"delete from BD where " + str + ";");
}
this.buttonShow_Click(this, new EventArgs());
}
private void buttonSearchAdd_Click(object sender, EventArgs e)
{
dataGridView2.Rows.Add();
}
private void buttonSearchDelete_Click(object sender, EventArgs e)
{
if (dataGridView2.SelectedCells.Count > 0 || dataGridView2.SelectedRows.Count > 0)
{
if (dataGridView2.SelectedCells.Count > 0)
for (int i = 0; i < dataGridView2.SelectedCells.Count; ++i)
dataGridView2.Rows.RemoveAt(dataGridView2.SelectedCells[i].RowIndex);
if (dataGridView2.SelectedRows.Count > 0)
for (int i = 0; i < dataGridView2.SelectedRows.Count; ++i)
dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[i].Index);
}
else dataGridView2.Rows.RemoveAt(dataGridView2.Rows.Count - 1);
}
private void buttonSearch_Click(object sender, EventArgs e)
{
string andor = (radioButtonSearchAnd.Checked)?"AND":"OR";
string result = @"select * from BD";
if (dataGridView2.Rows.Count > 0) result += " where";
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
if (i > 0) result += " " + andor;
result += " " + dataGridView2.Rows[i].Cells[0].Value + dataGridView2.Rows[i].Cells[1].Value + "'" + dataGridView2.Rows[i].Cells[2].Value + "'";
}
result += ";";
con = new MySqlConnection(sourse);
try
{
con.Open();
dataGridView3.DataSource = GetTable(result);
}
catch
{
MessageBox.Show("Error:\\");
}
con.Close();
}
private void checkBoxID_CheckedChanged(object sender, EventArgs e)
{
comboBoxID.Enabled =!comboBoxID.Enabled;
}
private void checkBoxColor_CheckedChanged(object sender, EventArgs e)
{
comboBoxColor.Enabled =!comboBoxColor.Enabled;
}
private void checkBoxMatherial_CheckedChanged(object sender, EventArgs e)
{
comboBoxMatherial.Enabled =!comboBoxMatherial.Enabled;
}
private void checkBoxNameItem_CheckedChanged(object sender, EventArgs e)
{
comboBoxNameItem.Enabled =!comboBoxNameItem.Enabled;
}
private DataTable DT()
{
string query = @"select * from BD";
bool ID = comboBoxID.Enabled && comboBoxID.Text!= "", Matherial = comboBoxMatherial.Enabled && comboBoxMatherial.Text!= "", Color = comboBoxColor.Enabled && comboBoxColor.Text!= "", Name = comboBoxNameItem.Enabled && comboBoxNameItem.Text!= "";
if (ID || Name || Matherial || Color)
{
query += " where ";
if (ID) query += "ID = " + comboBoxID.Text + " and ";
if (Name) query += "Name = " + comboBoxNameItem.Text + " and ";
if (Matherial) query += "Matherial = " + comboBoxMatherial.Text + " and ";
if (Color) query += "Color = " + comboBoxColor.Text + " and ";
query = query.Substring(0, query.Length - 5);
}
query += ";";
return GetTable(query);
}
private void comboBoxID_Click(object sender, EventArgs e)
{
comboBoxID.Items.Clear();
DataTable dt = new DataTable();
dt = GetTable(@"select ID from BD;");
for (int i = 0; i < dt.Rows.Count; ++i)
{
comboBoxID.Items.Add(dt.Rows[i][0]);
}
}
private void comboBoxNameItem_Click(object sender, EventArgs e)
{
comboBoxColor.Items.Clear();
DataTable dt = DT();
string [] str = new string [dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; ++i)
{
str[i] = (string)dt.Rows[i]["Name"];
}
}
private void comboBoxColor_Click(object sender, EventArgs e)
{
comboBoxColor.Items.Clear();
DataTable dt = new DataTable();
dt = GetTable(@"select Name " + ((checkBoxID.Enabled)? "": "where ID = " + comboBoxID.Text) + "from BD;");
for (int i = 0; i < dt.Rows.Count; ++i)
{
comboBoxColor.Items.Add(dt.Rows[i][0]);
}
}
private void label11_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Дата добавления: 2015-07-16; просмотров: 46 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
ОПИСАНИЕ ОСНОВНЫХ ФУНКЦИЙ ПРОГРАММНОГО ПРОДУКТА | | | Устройство арматурного каркаса. |