YY Yg Y : @@A D Ecf &
YY Yg Y : @@A D Ecf &
NET
THAO TC CSDL ADO.NET V I DATAGRIDVIEW
string sqlquery = "SELECT * FROM EMPLOYEES"; string sqlConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\SQL Server 2000 Sample Databases\\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True"; SqlConnection conn = new SqlConnection(sqlConnectionString); SqlCmdmand cmd = new SqlCmdmand(sqlQuery, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); DataTable dt = new DataTable(); try { conn.Open(); adapter.Fill(ds); dt = ds.Tables[0]; dataGridView1.DataSource = dt; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); }
TH C THI CU TRUY V N
N CSDL
string sqlConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\SQL Server 2000 Sample Databases\\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True"; string sqlQuery = "SELECT FIRSTNAME, LASTNAME FROM EMPLOYEES"; //string sqlQuery = "SELECT COUNT(*) FROM EMPLOYEES"; SqlConnection conn = new SqlConnection(sqlConnectionString); SqlCommand cmd = new SqlCommand(sqlQuery, conn); SqlDataReader dr; try { conn.Open(); dr = cmd.ExecuteReader(); while (dr.Read()) { label1.Text = "K T QU : " + dr[0].ToString() + " " + dr[1].ToString(); } dr.Close(); } catch (SqlException ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); }
L Y THAY
IC AD
DataSet dsChange = new DataSet("Changed"); try { dsChange = ds.GetChanges(); if (dsChange != null) this.dataGridView2.DataSource = dsChange.Tables[0]; else this.dataGridView2.DataSource = null; } catch (Exception ex) { MessageBox.Show(ex.Message); }
DataSet, DataTable
// t tn cho DataSet DataSet ds = new DataSet("EMPLOYEES"); // L y tn c a DataSet string s = ds.DataSetName; // Thm table vo DataSet ds.Tables.Add(new DataTable("EMPLOYEES")); // L y tn DataTable DataTable dt = new DataTable("EMPLOYEES"); string s = dt.TableName; // L y t ng s table trong DataSet int i = dataSet.Tables.Count; // L y s l ng dng trong 1 table int i = dataSet.Tables[0].Rows.Count; // L y s l ng c t trong 1 table int i = dataSet.Tables[0].Columns.Count; // Ch p nh n cc s thay ds.AcceptChanges(); // Khng ch p nh n cc s ds.RejectChanges(); // Xa tr ng cc CSDL ds.Clear(); // Ghi d li u ra file XML ds.WriteXml("D:\\TestXML.xml"); // c d li u t file XML ds.ReadXml("D:\\TestXML.xml"); i d thay li u t i d pha ng i dng i dng
li u t
pha ng
Load d li u b ng DataTable
string sqlquery = "SELECT TOP 10 * FROM EMPLOYEES"; dt = new DataTable("EMPLOYEES"); try { SqlDataAdapter da = new SqlDataAdapter(sqlquery,Connection.sqlConnection); da.Fill(dt); da.Dispose(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } dataGridView1.DataSource = dt; label1.Text = dt.TableName;
Copy m t DataTable c s n:
DataTable dtClone = new DataTable(); try { dtClone = dataTable.Clone(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } dataGridView2.DataSource = dtClone;
L u : ph ng th c Clone() ch l copy c u trc c a 1 table, ch khng copy h t cc d li u c trong table . N u mu n thm d li u vo th dng ph ng th c ImportRow() :D
L c d li u trong DataView
DataTable dt =cls.FillDataTable("SELECT * FROM EMPLOYEES", CommandType.Text); DataView dv = new DataView(dt); if (textBox1.Text != "")// L c theo t kha c trong textbox1 { string strFilter = "FirstName like '%" + textBox1.Text + "%'"; strFilter += " or LastName like '%" + textBox1.Text + "%'"; dv.RowFilter = strFilter; } label1.Text = "Number of Employee(s): " + dv.Count.ToString(); dataGridView1.DataSource = dv;
nh button
c nh n
DataTable dt = cls.FillDataTable("select CustomerID, CompanyName,City from Customers", CommandType.Text); dataGridView1.DataSource = dt; foreach (DataGridViewRow dgRow in dataGridView1.Rows) { dgRow.Cells[0].Value = "Accept"; dgRow.Cells[1].Value = "Cancel"; } // dng Event CellClick c a DataGirdView xc nh nt c nh n private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { switch (e.ColumnIndex) { case 0: // khi duoc nhan se hien ra Vi du Accept v 2 l s c t s hi n thi MessageBox.Show("Vi du Accept: " + Convert.ToString(dataGridView1[2, e.RowIndex].Value)); break; case 1: MessageBox.Show("Vi du Cancel: " + Convert.ToString(dataGridView1[2, e.RowIndex].Value)); break; } }