using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
private SqlConnection sqlConnection = null;
DataSet dataSet = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString);
sqlConnection.Open();
if (sqlConnection.State == ConnectionState.Open)
{
MessageBox.Show("Ulandi");
}
}
private void button1_Click(object sender, EventArgs e)
{
insert(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
}
private void insert(string nomi, string qavat, string telefon, string boshliq)
{
SqlCommand sqlCommand = new SqlCommand("INSERT INTO [bolimlar] (nomi,qavat,telefon,boshliq) VALUES (@nomi,@qavat,@telefon,@boshliq)", sqlConnection);
sqlCommand.Parameters.AddWithValue("nomi", nomi);
sqlCommand.Parameters.AddWithValue("qavat", qavat);
sqlCommand.Parameters.AddWithValue("telefon", telefon);
sqlCommand.Parameters.AddWithValue("boshliq", boshliq);
MessageBox.Show(sqlCommand.ExecuteNonQuery().ToString());
}
private void funk(string query)
{
try
{
dataSet = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sqlConnection);
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
int rowindex = dataGridView1.CurrentCell.RowIndex;
funk("DELETE FROM bolimlar where id=" + dataGridView1.Rows[rowindex].Cells[0].Value.ToString());
funk("SELECT * FROM bolimlar");
}
private void button3_Click(object sender, EventArgs e)
{
funk("SELECT * FROM bolimlar");
}
int rowId;
private void dataGridView1_Click(object sender, EventArgs e)
{
int rowindex = dataGridView1.CurrentCell.RowIndex;
rowId = Convert.ToInt32(dataGridView1.Rows[rowindex].Cells[0].Value);
textBox5.Text = dataGridView1.Rows[rowindex].Cells[1].Value.ToString();
textBox6.Text = dataGridView1.Rows[rowindex].Cells[2].Value.ToString();
textBox7.Text = dataGridView1.Rows[rowindex].Cells[3].Value.ToString();
textBox8.Text = dataGridView1.Rows[rowindex].Cells[4].Value.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommand sqlCommand = new SqlCommand("UPDATE [bolimlar] SET nomi=@nomi, qavat=@qavat, telefon=@telefon, boshliq=@boshliq WHERE id="+rowId, sqlConnection);
sqlCommand.Parameters.AddWithValue("nomi", textBox5.Text);
sqlCommand.Parameters.AddWithValue("qavat", textBox6.Text);
sqlCommand.Parameters.AddWithValue("telefon", textBox7.Text);
sqlCommand.Parameters.AddWithValue("boshliq", textBox8.Text);
MessageBox.Show(sqlCommand.ExecuteNonQuery().ToString());
MessageBox.Show(sqlCommand.CommandText);
funk("SELECT * FROM bolimlar");
}
}
}
Do'stlaringiz bilan baham: |