Profesyonel bir şekilde ADO.NET ile veritabanı bağlantısı bu şekilde kuruluyor. Burdan sonrası filtreleme işlemleri oluyor.(Eksiğim varsa belirtin)
Form1 Görüntüsü Elemanlar = 1 adet datagridview, 3 adet textbox, 3 adet button, ve bir adet ben
Burası Komutların verildiği Class Sonuna Dal konması gerekiyor profluk bakımından unutmuşum.
Form1 Görüntüsü Elemanlar = 1 adet datagridview, 3 adet textbox, 3 adet button, ve bir adet ben
Burası Komutların verildiği Class Sonuna Dal konması gerekiyor profluk bakımından unutmuşum.
Kod:
class ProductManager
{
SqlConnection _connection = new SqlConnection(@"server=(localdb)\MSSQLLocalDB;initial catalog=Eticaret;integrated security=true;");
public List<Product> Getir()
{
_connection.Open();
List<Product> products = new List<Product>();
SqlCommand SqlCommand = new SqlCommand("select * from Products", _connection);
SqlDataReader dr = SqlCommand.ExecuteReader();
while (dr.Read())
{
Product product = new Product
{
Id = Convert.ToInt16(dr["Id"]),
Name = dr["Name"].ToString(),
UnitPrice = Convert.ToDecimal(dr["UnitPrice"]),
StockAmount = Convert.ToInt32(dr["StockAmount"])
};
products.Add(product);
}
_connection.Close();
return products;
}
public vo id Ekle(Product product)
{
_connection.Open();
SqlCommand sqlCommand = new SqlCommand("insert into Products values (@Name [USER=547885]unit[/USER]Price [USER=138852]stock[/USER]Amount)",_connection);
sqlCommand.Parameters.AddWithValue("@Name",product.Name);
sqlCommand.Parameters.AddWithValue( [USER=547885]unit[/USER]Price", product.UnitPrice);
sqlCommand.Parameters.AddWithValue( [USER=138852]stock[/USER]Amount", product.StockAmount);
sqlCommand.ExecuteNonQuery();
_connection.Close();
}
public vo id Guncelle(Product product)
{
_connection.Open();
SqlCommand cmd = new SqlCommand("update Products set Name=@Name,UnitPrice [USER=547885]unit[/USER]Price,StockAmount [USER=138852]stock[/USER]Amount where Id=@Id", _connection);
cmd.Parameters.AddWithValue("@Id",product.Id);
cmd.Parameters.AddWithValue("@Name",product.Name);
cmd.Parameters.AddWithValue( [USER=547885]unit[/USER]Price", product.UnitPrice);
cmd.Parameters.AddWithValue( [USER=138852]stock[/USER]Amount", product.StockAmount);
cmd.ExecuteNonQuery();
_connection.Close();
}
public vo id Sil(Product product)
{
_connection.Open();
SqlCommand sqlCommand = new SqlCommand("delete from Products where Id=@Id",_connection);
sqlCommand.Parameters.AddWithValue("@Id",product.Id);
sqlCommand.ExecuteNonQuery();
_connection.Close();
}
}
Burası propların tanımlandığı class.
Kod:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal UnitPrice { get; set; }
public int StockAmount { get; set; }
}
Burasıda Formun içi
Kod:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ProductManager _productManager = new ProductManager();
private vo id Form1_Load(object sender, EventArgs e)
{
NewMethod();
}
private vo id NewMethod()
{
dataGridView1.DataSource = _productManager.Getir();
}
private vo id button1_Click(object sender, EventArgs e)
{
_productManager.Ekle(new Product
{
Name = textBox1.Text,
UnitPrice=Convert.ToDecimal(textBox2.Text),
StockAmount=Convert.ToInt32(textBox3.Text)
});
NewMethod();
MessageBox.Show("Eklendi");
}
private vo id button2_Click(object sender, EventArgs e)
{
_productManager.Guncelle(new Product {
Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value),
Name = textBox1.Text,
UnitPrice = Convert.ToDecimal(textBox2.Text),
StockAmount = Convert.ToInt32(textBox3.Text)
});
NewMethod();
MessageBox.Show("Güncellendi");
}
private vo id dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
private vo id button3_Click(object sender, EventArgs e)
{
_productManager.Sil(new Product {
Id =Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value)
});
NewMethod();
MessageBox.Show("Silindi");
}
}
Moderatör tarafında düzenlendi: