Код формы.
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 System.Data.OleDb; //using Session; //using Description;
namespace СУБД_ООП_Cs_2008 { public partial class Form1 : Form { Session session = new Session();
public Form1() { InitializeComponent(); }
private void buttonInsert_Click(object sender, EventArgs e) {
Description description = new Description(); description.id_p = textBox1.Text; description.name = textBox2.Text; description.myvalue = textBox3.Text; description.id_t = textBox4.Text; description.stage = textBox5.Text; description.diagnosis = textBox6.Text; string CommandText = "INSERT INTO temp (id_p, name, value, id_t, stage, diagnosis) VALUES(' " + description.id_p + " ', ' " + description.name + " ', ' " + description.myvalue + " ', ' " + description.id_t + " ', ' " + description.stage + " ', ' " + description.diagnosis + " ' )"; textBox7.Text = CommandText; session.Insert(description); }
private void buttonSelect_Click(object sender, EventArgs e) {
comboBox1.DataSource = session.FillComboBox(); }
private void buttonUpdate_Click(object sender, EventArgs e) { Description oldDescription = new Description(); Description newDescription = new Description(); oldDescription = comboBox1.SelectedItem as Description;
newDescription.id_p = textBox1.Text; newDescription.name = textBox2.Text; newDescription.myvalue = textBox3.Text; newDescription.id_t = textBox4.Text; newDescription.stage = textBox5.Text; newDescription.diagnosis = textBox6.Text;
string CommandText = "UPDATE temp SET id_p = " + newDescription.id_p + ", name = " + newDescription.name + ", [value] = " + newDescription.myvalue + ", id_t = " + newDescription.id_t + ", stage = " + newDescription.stage + ", diagnosis = " + newDescription.diagnosis + " WHERE ID = " + oldDescription.id; textBox7.Text = CommandText;
session.Update(oldDescription, newDescription); }
private void buttonDelete_Click(object sender, EventArgs e) { Description description = new Description(); description = comboBox1.SelectedItem as Description; session.Delete(description); } } }
Код класса Description.
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace СУБД_ООП_Cs_2008 { public class Description { public int id; public string id_p; public string name; public string myvalue; public string id_t; public string stage; public string diagnosis;
public override string ToString() { return Convert.ToString(id) + " " + id_p + " " + name + " " + myvalue + " " + id_t + " " + stage + " " + diagnosis; }
} }
Код класса Session.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb;
namespace СУБД_ООП_Cs_2008 { public class Session { OleDbConnection connection; OleDbCommand command;
public void ConnectTo() { connection = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=" + System.Windows.Forms.Application.StartupPath + "\\dbTest.mdb"); command = connection.CreateCommand(); }
public Session() { ConnectTo(); }
public void Insert(Description description) { try { command.CommandText = "INSERT INTO temp (id_p, name, [value], id_t, stage, diagnosis) VALUES(' " + description.id_p + " ', ' " + description.name + " ', ' " + description.myvalue + " ', ' " + description.id_t + " ', ' " + description.stage + " ', ' " + description.diagnosis + " ' );"; //command.CommandType = command.CommandText; //command.CommandType = CommandType.Text; connection.Open();
command.ExecuteNonQuery(); }
catch (Exception) { throw; }
finally { if (connection != null) { connection.Close(); } } } //завершился метод
public List<Description> FillComboBox() { List<Description> descriptionsList = new List<Description>();
try { command.CommandText = "SELECT * FROM temp;" ; //(id_p, name, [value], id_t, stage, diagnosis) VALUES(' " + // description.id_p + " ', ' " + description.name + " ', ' " + // description.myvalue + " ', ' " + description.id_t + " ', ' " + // description.stage + " ', ' " + description.diagnosis + " ' );"; connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read()) { Description description = new Description(); description.id = Convert.ToInt32(reader["id"].ToString()); description.id_p = reader["id_p"].ToString(); //MessageBox.Show(description.id_p); description.name = reader["name"].ToString(); description.myvalue = reader["value"].ToString(); description.id_t = reader["id_t"].ToString(); description.stage = reader["stage"].ToString(); description.diagnosis = reader["diagnosis"].ToString();
descriptionsList.Add(description); }
return descriptionsList; }
catch (Exception) { throw; }
finally { if (connection != null) { connection.Close(); } }
}
//--------------------------------- public void Update(Description oldDescription, Description newDescription) {
try { command.CommandText = "UPDATE temp SET id_p = "+newDescription.id_p + ", name = "+newDescription.name + ", [value] = "+newDescription.myvalue + ", id_t = "+newDescription.id_t + ", stage = "+newDescription.stage + ", diagnosis = "+newDescription.diagnosis + " WHERE ID = " + oldDescription.id;
connection.Open();
command.ExecuteNonQuery(); }
catch (Exception) { throw; }
finally { if (connection != null) { connection.Close(); } }
} //----------------------------------------------- public void Delete(Description description) {
try { command.CommandText = "DELETE FROM temp WHERE ID = "+description.id;
connection.Open();
command.ExecuteNonQuery(); } catch (Exception) { throw; }
finally { if (connection != null) { connection.Close(); } }
} //------------------------------------------------ } }
|