Tải bản đầy đủ (.doc) (29 trang)

Chương trình c bài tập đơn giản

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.35 MB, 29 trang )

Bài tập về C# 2008
Bài tập về C# 2008..............................................................................................................................................1
ComboBOX.................................................................................................................................................2
ListBox........................................................................................................................................................3
Đồng hồ đếm ngược....................................................................................................................................4
Xâu kí tự......................................................................................................................................................5
Dùng ToolBar..............................................................................................................................................7
Thực đơn......................................................................................................................................................8
Mảng dữ liệu..............................................................................................................................................10
Truyền giá trị qua FORM..........................................................................................................................12
Chuyển giá trị nhờ DialogueResult...........................................................................................................13
Chuyển tham số của thủ tục.......................................................................................................................14
Mơ tả lớp, xử lí xâu...................................................................................................................................16
SQL SERVER 2005...................................................................................................................................18
Cơ sở dữ liệu..............................................................................................................................................20
Làm việc với cơ sở dữ liệu........................................................................................................................21
Đưa đữ liệu lên DataGridView..................................................................................................................22
Bổ sung một dịng dữ liệu..........................................................................................................................24
Xóa một dịng dữ liệu đánh dấu trên bảng hiện dữ liệu............................................................................27

1


ComboBOX

using
using
using
using
using
using


using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace bai_COMBOBOX
{
public partial class chinh : Form
{
public chinh()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "Đã chọn ";
this.textBox1.Text += this.comboBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e)
{

this.textBox1.ResetText();
}
private void chinh_Load(object sender, EventArgs e)
{
// trong property cua Combobox da co 3 chi tiet
this.comboBox1.Items.Add("chi tiet 4");
this.comboBox1.Items.Add("chi tiet 5");
}
}
}

2


ListBox

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;

System.Linq;
System.Text;
System.Windows.Forms;

namespace baitap_listbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "Đã chọn ";
this.textBox1.Text += this.listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.ResetText();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}

3



Đồng hồ đếm ngược

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace Baitap_Dongho
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 10;
private void button1_Click(object sender, EventArgs e)

{
this.textBox1.Text = "Hiện trạng : ";
if (this.radioButton1.Checked) this.textBox1.Text += " OK";
else this.textBox1.Text += " NOT";
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label3.Text = i.ToString();
i --;
if (i < 0)
this.timer1.Enabled = false;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}

4


private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}

}
}

Xâu kí tự

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace btap_xau
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string s1 = this.textBox1.Text;
string s2 = this.textBox2.Text;
// so sánh, phân biệt chữ hoa. Nếu không phân biệt Compare (s1,s2, true);

5


// so sánh từ trái sang phải, theo AI
int kq = string.Compare(s1, s2);
// đánh giá kết quả
this.textBox3.Text = "xâu thứ nhất ";
if (kq == -1)
this.textBox3.Text += "<";
else if (kq == 0)
this.textBox3.Text += "=";
else
this.textBox3.Text += ">";
this.textBox3.Text += " xâu thứ hai";
}
private void button2_Click(object sender, EventArgs e)
{

string s1 = this.textBox1.Text;
string s2 = this.textBox2.Text;
// nối hai xâu
this.textBox3.Text = string.Concat(s1, s2);
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.textBox2.Text = "";
this.textBox3.Text = "";
this.textBox1.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
string s1 = this.textBox1.Text;
string s2 = this.textBox2.Text;
// cho biết vị trí xuất hiện s2 trong s1. lưu ý xâu từ vị trí 0
int i = s1.IndexOf(s2);
if (i >= 0)
{
this.textBox3.Text = "Xâu 2 xuất hiện trong xâu 1 tại vị trí i = ";
this.textBox3.Text += i;
// thay thế xâu con s2 bằng xâu con s3 mới
string s3 = "THAY THẾ";
this.textBox3.Text += "\n\r\n Thay thế s2 bằng s3 mới";
this.textBox3.Text += "\n\r\n kết quả " + s1.Replace(s2, s3);
}
else
this.textBox3.Text = " xâu 1 không chứa xâu 2";
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
string s1 = this.textBox1.Text;
string s2 = this.textBox2.Text;
// chèn s2 vào giữa s1. Vị trí chèn sau số 4 đầu tiên
this.textBox3.Text = "Chèn s2 vào s1, TRƯỚC kí tự 4 đầu tiên\n\r\n";
this.textBox3.Text += s1.Insert(s1.IndexOf("4"), s2);
}
}
}

6


Dùng ToolBar

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;

System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace baitap_Toolbar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void toolStripTextBox1_Click(object sender, EventArgs e)
{
//ứng với Một
Form Mot = new Form2();
Mot.ShowDialog();
}
private void toolStripTextBox2_Click(object sender, EventArgs e)
{
//ứng với Hai
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}


using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace baitap_Toolbar
{
public partial class Form2 : Form
{
public Form2()

7


{
InitializeComponent();

}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}

Thực đơn

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace baitap_thucdon
{
public partial class Form1 : Form

{
public Form1()
{
InitializeComponent();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
//Mục 1.2. của thực đơn Chức năng 1
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
//mục thực đơn 1.1.
Form muc11 = new Form2();
muc11.ShowDialog();
}
private void mục21ToolStripMenuItem_Click(object sender, EventArgs e)
{
//Mục 2.1. của thực đơn Chức năng 2
}

8



private void mục22ToolStripMenuItem_Click(object sender, EventArgs e)
{
// mục 2.2. của thực đơn Chức năng 2
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace baitap_thucdon
{

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}

9


Mảng dữ liệu

using
using
using
using
using
using
using
using


System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace baitap_Mang_dlieu
{
public partial class Form1 : Form
{
// khai bao bien du lieu
const int m = 3;
const int n = 4;
int spt = 0;
int[] a = { 1, 2, 3, 4 }; // hay là : int [] a = new int[4] {1,2,3,4};
int [,] b = new int [m, n];
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
// xóa textbox trong form này
this.textBox1.Text = "";

}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// in ra ma tran 1 chieu
this.textBox1.Text = "Mảng một chiều a :\n\r\n";
for (int i = 0; i < a.Length; i++)
this.textBox1.Text += a[i] + "
";
// in ra mảng đã sắp xếp tăng dần
Array.Sort(a, 0, n);

10


this.textBox1.Text += "\n\r\n Xếp tăng dần : \n\r\n";
for (int i = 0; i < a.Length; i++)
this.textBox1.Text += a[i] + "
";
}
private void button4_Click(object sender, EventArgs e)
{
// nhập từng phần tử của mảng 1 chiều, độ dài n
if (spt == n)
MessageBox.Show("mảng đã đủ n phần tử");
else
{
a[spt] = Convert.ToInt32(this.textBox2.Text);

spt++;
this.textBox2.ResetText();
this.textBox2.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{
// gán giá trị cho mảng 2 chiều
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i, j] = i * j;
}
}
// in ra mang 2 chieu b
this.textBox1.Text = "Mảng hai chiều : \n\r\n";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
this.textBox1.Text += b[i, j] + " ";
this.textBox1.Text += "\n\r\n";
}
}
}
}

11



Truyền giá trị qua FORM

namespace ChuyenFORM
{
public partial class chinh : Form
{
public chinh()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txt1_TextChanged(object sender, EventArgs e)
{

/*

}
private void txtEdit_Click(object sender, EventArgs e)
*/
{
traodoi frm = new traodoi();
// gan gia tri cho bien t trong traodoi
frm.t = txt.Text;
// an nut OK
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// gan gia tri cho form chinh
txt.Text = frm.t;

}
}
}
}
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace ChuyenFORM
{
public partial class traodoi : Form
{
public string t = "";
public traodoi()
{


12


InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
// khi tai form, t duoc truyen tu form chinh cho textbox txt
txt.Text = t;
}
private void button1_Click(object sender, EventArgs e)
{
// gan gi tri cho t, tu textbox txt
t = txt.Text;
}
}
}

Chuyển giá trị nhờ DialogueResult

using
using
using
using
using
using
using

using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace DialogResult
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void txtEdit_Click(object sender, EventArgs e)
{
DialogForm frm = new DialogForm();
//Gán giá trị cho biến textValue trong DialogForm
frm.textValue = txtValue.Text;
//Nếu sau khi bạn ấn nút OK trên DialogForm
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
//Gán giá trị textValue trong DialogForm cho textbox txtValue trong MainForm
txtValue.Text = frm.textValue;
}
}


13


private void MainForm_Load(object sender, EventArgs e)
{
}
}
}

namespace DialogResult
{
public partial class DialogForm : Form
{
public string textValue = "";
public DialogForm()
{
InitializeComponent();
}
private void DialogForm_Load(object sender, EventArgs e)
{
//Khi load form, Gán giá trị textValue được truyển từ MainForm cho textbox txtValue
txtValue.Text = textValue;
}
private void btnOK_Click(object sender, EventArgs e)
{
//Gán giá trị cho textValue từ textbox txtValue
textValue = txtValue.Text;
}
}

}

Chuyển tham số của thủ tục

using System;

14


using
using
using
using
using
using
using

System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace Btap_truyen_thamso
{
public partial class Form1 : Form
{
public Form1()

{
InitializeComponent();
}
// Bổ sung CLASS, tức trình con
public class Trao
{
public void Hoanvi(ref int a, ref int b) //truyen theo biến
{
int w = a;
a = b;
b = w;
}
public void Hv(int a, int b) // truyền theo giá trị
{
int c = a;
a = b;
b = c; // khi kết thúc, A, B không thay đổi
}
}
// hết mô tả CLASS
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(this.textBox1.Text);
int b = int.Parse(this.textBox2.Text);
Trao i = new Trao(); //đến class Trao
i.Hoanvi(ref a, ref b);
this.textBox1.Text = a.ToString();

this.textBox2.Text = b.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.ResetText();
this.textBox2.ResetText();
}
private void button3_Click(object sender, EventArgs e)
{
int a = int.Parse(this.textBox1.Text);
int b = int.Parse(this.textBox2.Text);
Trao i = new Trao(); //đến class Trao
i.Hv(a, b); // A, B không thay đổi
this.textBox1.Text = a.ToString();
this.textBox2.Text = b.ToString();
}
}
}

15


Mơ tả lớp, xử lí xâu

using
using
using
using
using
using

using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace lopXAU
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// ------ mô tả CLASS trong FORM ----public class Xau
{
// ----------------------- thuoc tinh
string gthieu = "Xử lí dịng văn bản";
// ----------------------- method
// in ra
public string In()
{
return gthieu;
}

//lay ten từ hoten
public string Ten(string hoten)
{
int i = hoten.LastIndexOf(" ");
return hoten.Substring(i+1, hoten.Length - i-1);
}
// lay ho và dem từ hoten
public string Ho(string hoten)
{
int i = hoten.LastIndexOf(" ");
return hoten.Substring(0, i);
}
// đếm số từ trong hoten
public int Dem(string hoten)
{
const char Trong = ' ';
string s = hoten.Trim();

16


int i = s.Split(Trong).Length; // chuyển xâu thành mảng, rồi đếm
return i;
}
}
//----------------------------- hết mô tả CLASS
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)

{
// giới thiệu
Xau s = new Xau();
this.textBox2.Text = s.In();
}
private void button2_Click(object sender, EventArgs e)
{
// trích tên
Xau s = new Xau();
this.textBox2.Text = "tên là : " + s.Ten(this.textBox1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
// trích họ, đệm
Xau s = new Xau();
this.textBox2.Text = "Họ là : " + s.Ho(this.textBox1.Text);
}
private void button4_Click(object sender, EventArgs e)
{
Xau s = new Xau();
this.textBox2.Text = "Số từ trong họ tên là : " + s.Dem (this.textBox1.Text);
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

17



SQL SERVER 2005




Chạy SQLServer2005_SSMSEE để tạo chương trình quản lí máy chủ SQL Server;
File MSSQL2005_Setup cho phép tạo cấu hình

Sau khi cài đặt chương trình. Khởi động chương trình này sẽ đưa người dùng đến cửa sổ tạo cơ sở dữ liệu.

Khi khởi động chương trình, xuất hiện phần yêu cầu kết nối đến máy chủ :

18


Tạo them bảng NGUOI vào cơ sở dữ liệu chính.

19


Cơ sở dữ liệu

20


Làm việc với cơ sở dữ liệu

using

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.SqlClient; // khai bao

namespace baitap_csdl
{
public partial class Form1 : Form
{
// xâu kết nối
string strConnectionString = "Data Source = .\\SQLEXPRESS; Initial Catalog = Quanly;
Integrated Security = True";
// Đối tượng kết nối
SqlConnection conn = null;
// Đối tượng đưa dữ liệu vào bảng NGUOI
SqlDataAdapter danguoi = null;

// đối tượng hiển thị lên Form
DataTable dtnguoi = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
// khởi động nối
conn = new SqlConnection (strConnectionString);
// tải dữ liệu
danguoi = new SqlDataAdapter("SELECT * FROM nguoi", conn);
dtnguoi = new DataTable();
dtnguoi.Clear();
danguoi.Fill(dtnguoi);
// Đưa dữ lieu vào listBox
this.listBox1.DataSource = dtnguoi;
//this.listBox1.DisplayMember = "ten";
this.listBox1.ValueMember = "ten";
}
catch (SqlException)
{
MessageBox.Show ("không tải được !");
}

21



}
private void button1_Click(object sender, EventArgs e)
{
// giải phóng
dtnguoi.Dispose();
dtnguoi = null;
//hủy kết nói
conn = null;
//thốt
DialogResult Traloi;
Traloi = MessageBox.Show("Thốt", "Trả lời", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if (Traloi==DialogResult.OK) Application.Exit();
}
}
}

Đưa đữ liệu lên DataGridView

using
using
using
using
using
using
using
using
using

System;

System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.SqlClient; // khai bao

namespace btap_datagridview
{
public partial class Form1 : Form
{
// xâu kết nối
string strConnectionString = "Data Source = .\\SQLEXPRESS; Initial Catalog = Quanly;
Integrated Security = True";
// Đối tượng kết nối
SqlConnection conn = null;
// Đối tượng đưa dữ liệu vào bảng NGHE
SqlDataAdapter danghe = null;
// đối tượng hiển thị lên Form
DataTable dtnghe = null;

22


// Đối tượng đưa dữ liệu vào bảng NGUOI
SqlDataAdapter danguoi = null;
// đối tượng hiển thị lên Form
DataTable dtnguoi = null;

//mô tả hàm Tải lại dữ liệu
void Tai()
{
try
{
// kết nối
conn = new SqlConnection(strConnectionString);
// tải dư lieu
danghe = new SqlDataAdapter("SELECT * FROM nghe", conn);
dtnghe = new DataTable();
dtnghe.Clear();
danghe.Fill(dtnghe);
// đưa dư lieu lên COMBOBOX trong DataGridView
// (nghenghiep.Columns["ten"] as DataGridViewComboBoxColumn).DataSource = dtnghe;
// (nghenghiep.Columns["ten"] as DataGridViewComboBoxColumn).DisplayMember =
"viec";
//(nghenghiep.Columns["ten"] as DataGridViewComboBoxColumn).ValueMember = "viec";
//đưa dữ liệu vào dtnguoi
danguoi = new SqlDataAdapter("SELECT * FROM nguoi", conn);
dtnguoi = new DataTable();
dtnguoi.Clear();
danguoi.Fill(dtnguoi);
// đưa dữ liệu lên DataGridView
nghenghiep.DataSource = dtnguoi;
}
catch (SqlException)
{
MessageBox.Show("Không tải được bảng NGUOI");
}
}

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
// kết nối
conn = new SqlConnection(strConnectionString);
// tải dư lieu
danghe = new SqlDataAdapter("SELECT * FROM nghe", conn);
dtnghe = new DataTable();
dtnghe.Clear();
danghe.Fill(dtnghe);
// dư lieu vào DataGridView
nghenghiep.DataSource = dtnghe;
}
catch (SqlException)
{
MessageBox.Show("không tải được dữ liệu");
}
}
private void nghenghiep_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

23


}

private void button1_Click(object sender, EventArgs e)
{
// giải phóng
dtnghe.Dispose();
dtnghe = null;
dtnguoi.Dispose();
dtnguoi = null;
//hủy kết nói
conn = null;
//thốt
DialogResult Traloi;
Traloi = MessageBox.Show("Thốt", "Trả lời", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if (Traloi == DialogResult.OK) Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
Tai();
}
}
}

Bổ sung một dòng dữ liệu

using
using
using
using
using
using

using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.SqlClient; // khai báo bổ sung

namespace btap_insert_CSDL
{
public partial class Form1 : Form
{
// xâu kết nối
string strConnectionString = "Data Source = .\\SQLEXPRESS; Initial Catalog = Quanly;
Integrated Security = True";
// Đối tượng kết nối
SqlConnection conn = null;
// Đối tượng đưa dữ liệu vào bảng NGHE
SqlDataAdapter da = null;
// đối tượng hiển thị lên Form

24



DataTable dt = null;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Thốt khỏi chương trình
// giải phóng
dt.Dispose();
dt = null;
//hủy kết nối
conn = null;
//thốt
DialogResult Traloi;
Traloi = MessageBox.Show("Thốt", "Trả lời", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if (Traloi == DialogResult.OK) Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{

// Hiện dữ liệu. tải bảng dữ liệu NGUOI
try
{
// kết nối
conn = new SqlConnection(strConnectionString);
// tải dư lieu
da = new SqlDataAdapter("SELECT * FROM nguoi", conn);
dt = new DataTable();
dt.Clear();
da.Fill(dt);
// dư lieu vào DataGridView
hien.DataSource = dt;
}
catch (SqlException)
{
MessageBox.Show("không tải được dữ liệu NGUOI");
}
// hiện số dòng trong bảng hiện dữ liệu
int r = this.hien.Rows.Count;
string r2 = r.ToString();
this.textBox5.Text = r2;
}
private void button4_Click(object sender, EventArgs e)
{
//xóa vùng đệm

25



×