CREATE TABLE [dbo].[Produse] ( [IdProdus] INT NOT NULL PRIMARY KEY, [IdProd] INT NOT NULL, [IdCateg] INT NOT NULL, [Denumire] TEXT NOT NULL, [Pret] INT NOT NULL )
CREATE TABLE [dbo].[Producatori] ( [IdProd] INT NOT NULL PRIMARY KEY, [Nume] TEXT NOT NULL, [Adresa] TEXT NOT NULL, [Telefon] INT NOT NULL )
CREATE TABLE [dbo].[Categorii] ( [IdCateg] INT NOT NULL PRIMARY KEY, [Categorii] TEXT NOT NULL )
using using using using using using using using using
System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms;
namespace papetarie { public partial class Form1 : Form { SqlConnection myCon = new SqlConnection(); DataSet dsCateg; DataSet dsProdus; DataSet dsProd; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { myCon.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\papetarie.mdf;Integrated Security=True";
myCon.Open(); dsCateg = new DataSet(); dsProdus = new DataSet(); dsProd = new DataSet(); SqlDataAdapter daCateg = new SqlDataAdapter("SELECT * FROM Categorii", myCon); daCateg.Fill(dsCateg, "Categorii"); SqlDataAdapter daProdus = new SqlDataAdapter("SELECT * FROM Produse", myCon); daProdus.Fill(dsProdus, "Produse"); SqlDataAdapter daProd = new SqlDataAdapter("SELECT * FROM Producatori", myCon); daProd.Fill(dsProd, "Producatori"); foreach (DataRow dr in dsCateg.Tables["Categorii"].Rows) { String name = dr.ItemArray.GetValue(1).ToString(); Lista_categorii.Items.Add(name); } myCon.Close(); } private void Lista_categorii_SelectedIndexChanged(object sender, EventArgs e) { int code = 0; String CategSelected = Lista_categorii.SelectedItem.ToString(); foreach (DataRow dr in dsCateg.Tables["Categorii"].Rows) { if (CategSelected == dr.ItemArray.GetValue(1).ToString()) { tbProdus.Text = dr.ItemArray.GetValue(2).ToString(); tbProducator.Text = dr.ItemArray.GetValue(3).ToString(); } } foreach (DataRow dr in dsProd.Tables["Produse"].Rows) { if (code == Convert.ToInt16(dr.ItemArray.GetValue(0))) { String name = dr.ItemArray.GetValue(1).ToString(); tbProdus.Text = name; } } } } }
using System; using System.Collections.Generic; using System.ComponentModel;
using using using using using using
System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms;
namespace papetarie { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void bInsert_Click(object sender, EventArgs e) { myCon.Open(); SqlDataAdapter adProdus = new SqlDataAdapter(); try { SqlCommand command = new SqlCommand("INSERT INTO Produse( IdProdus , IdProd , IdCateg , Denumire , Pret) VALUES (@IdProdus , @IdProd , @IdCateg , @Denumire , @Pret)", myCon); command.Parameters.Add("@IdProdus", SqlDbType.Int).Value = int.Parse(tbProdus.Text); command.Parameters.Add("@IdProd", SqlDbType.Int).Value = int.Parse(tbProd.Text); command.Parameters.Add("@IdCateg", SqlDbType.Int).Value = int.Parse(tbCateg.Text); command.Parameters.Add("@Denumire", SqlDbType.Text).Value = tbDenumire.Text; command.Parameters.Add("@Pret", SqlDbType.Int).Value = int.Parse(tbPret.Text); adProdus.InsertCommand = command; adProdus.InsertCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } myCon.Close(); } private void bUpdate_Click(object sender, EventArgs e) { myCon.Open(); try { SqlDataAdapter adProdus = new SqlDataAdapter();
SqlCommand command = new SqlCommand("UPDATE Produse SET Denumire=@Denumire, Pret=@Pret where IdProdus=@IdProdus , IdProd=@IdProd , IdCateg=@IdCateg", myCon); command.Parameters.Add("@IdProdus", SqlDbType.Int).Value = int.Parse(tbProdus.Text); command.Parameters.Add("@IdProd", SqlDbType.Int).Value = int.Parse(tbProd.Text); command.Parameters.Add("@IdCateg", SqlDbType.Int).Value = int.Parse(tbCateg.Text); command.Parameters.Add("@Denumire", SqlDbType.Text).Value = tbDenumire.Text; command.Parameters.Add("@Pret", SqlDbType.Int).Value = int.Parse(tbPret.Text); adProdus.UpdateCommand = command; adProdus.UpdateCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } myCon.Close(); } private void bDelete_Click(object sender, EventArgs e) { myCon.Open(); try { SqlDataAdapter adProdus = new SqlDataAdapter(); SqlCommand command = new SqlCommand("Delete from Produse where IdProdus=@IdProdus ", myCon); command.Parameters.Add("@IdProdus", SqlDbType.Int).Value = int.Parse(tbProdus.Text); //command.Parameters.AddWithValue("@IdProdus",tbProdus.Text); adProdus.DeleteCommand = command; adProdus.DeleteCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } myCon.Close(); } private void bRefresh_Click(object sender, EventArgs e) { Lista_produselor.Items.Clear(); tbCategorie.Clear(); tbProducator.Clear(); dsProdus.Clear(); SqlDataAdapter daProdus = new SqlDataAdapter("SELECT * FROM Produse", myCon); daProdus.Fill(dsProdus, "Produse"); foreach (DataRow dr in dsProdus.Tables["Produse"].Rows) {
String name = dr.ItemArray.GetValue(1).ToString(); Lista_produselor.Items.Add(name); } } private void Lista_produselor_SelectedIndexChanged(object sender, EventArgs e) { int code = 0; String ProdusSelected = Lista_produselor.SelectedItem.ToString(); foreach (DataRow dr in dsProdus.Tables["Produse"].Rows) { if (ProdusSelected == dr.ItemArray.GetValue(1).ToString()) { IdCateg = Convert.ToInt16(dr.ItemArray.GetValue(3)); // de la ex1 IdProd = Convert.ToInt16(dr.ItemArray.GetValue(3)); //de la ex1 tbProdus.Text = dr.ItemArray.GetValue(0).ToString(); tbProd.Text = dr.ItemArray.GetValue(1).ToString(); tbCateg.Text = dr.ItemArray.GetValue(2).ToString(); tbDenumire.Text = dr.ItemArray.GetValue(3).ToString(); tbPret.Text = dr.ItemArray.GetValue(4).ToString(); } } } } }