1. 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.SqlClient;
namespace ql_tour
{
public partial class Form1 : Form
{
public DataTable dt = new DataTable();
public SqlDataAdapter da;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//hiển thị thông tin tour hiện hành trên
Textbox,combobox,DateTimePicker,PictureBox,DataGridView
//Duyệt bản ghi, kích chọn hàng DataGridView hàng tương ứng hiển thị lên
các control
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=FCVU1209AJESQLEXPRESS;Initial
Catalog=ql_tour;Integrated Security=True";
con.Open();
SqlCommand cmd = new SqlCommand("select * from tour", con);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dt;
grid_tour.DataSource = bindingSource;
bindingNavigator1.BindingSource = bindingSource;
this.txtma.DataBindings.Add("Text", bindingSource, "matour");
this.txtten.DataBindings.Add("Text", bindingSource, "tentour");
this.dtpngay.DataBindings.Add("Value", bindingSource,
"ngaykhoihanh");
this.txthinh.DataBindings.Add("Text", bindingSource, "hinh");
this.cmbloai.DataBindings.Add("Text", bindingSource, "maloai");
this.loadImage();
this.dtpngay.Format = DateTimePickerFormat.Custom;
this.dtpngay.CustomFormat = "dd/MM/yyyy";
}
catch (SqlException ex) { MessageBox.Show(ex.Message); }
}
private void loadImage()
{
try { pic1.Image = Image.FromFile("image" + txthinh.Text); }
catch (System.IO.FileNotFoundException) { pic1.Image = null; }
catch (System.ArgumentException) { pic1.Image = null; }
2. }
private void btnthem_Click(object sender, EventArgs e)
{
//Thêm thông tin tour
string ma = this.txtma.Text;
string ten = this.txtten.Text;
string ngaykhoihanh = this.dtpngay.Value.ToString();
string hinh = this.txthinh.Text;
string maloai = this.cmbloai.Text;
try
{
SqlConnection con = new SqlConnection(@"Data
Source=FCVU1209AJESQLEXPRESS;Initial Catalog=ql_tour;Integrated Security=True");
con.Open();
string sql = "insert into tour(matour, tentour, ngaykhoihanh,
hinh,maloai) values ('" + ma + "',N'" + ten + "','" + ngaykhoihanh + "','" + hinh +
"','" + maloai + "')";
SqlCommand command = new SqlCommand(sql, con);
int n = command.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}
catch (SqlException ex) { MessageBox.Show(ex.Message); }
}
private void btnsua_Click(object sender, EventArgs e)
{
//sửa thông tin tour
string ma= this.txtma.Text;
string ten = this.txtten.Text;
string ngaykhoihanh = this.dtpngay.Value.ToString();
string hinh = this.txthinh.Text;
string maloai = this.cmbloai.SelectedValue.ToString();
try
{
SqlConnection con = new SqlConnection(@"Data
Source=FCVU1209AJESQLEXPRESS;Initial Catalog=ql_tour;Integrated Security=True");
con.Open();
string sql = "update tour set tenhang = N'" + ten + "', ngaykhoihanh
= '" + ngaykhoihanh + "', hinh = '" + hinh + "', maloai = '" + maloai +"' where
matour = '" + ma+ "'";
SqlCommand command = new SqlCommand(sql, con);
int n = command.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}
catch (SqlException ex)
{ MessageBox.Show(ex.Message); }
}
private void btnxoa_Click(object sender, EventArgs e)
3. {
//xóa thông tin tour
DialogResult d = MessageBox.Show("Bạn có muốn xóa không", "Xóa",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (d == DialogResult.Yes)
{
string ma = this.txtma.Text;
try
{
SqlConnection con = new SqlConnection(@"Data
Source=FCVU1209AJESQLEXPRESS;Initial Catalog=ql_tour;Integrated Security=True");
con.Open();
string sql = "delete from tour where matour = '" + ma + "'";
SqlCommand command = new SqlCommand(sql, con);
int n = command.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}
catch (SqlException ex) { MessageBox.Show(ex.Message); }
}
}
}
}