6:用到图来建立数据关系
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADOtest1027
{
public partial class FormEmployee : Form
{
public FormEmployee()
{
InitializeComponent();
}
private void FormEmployee_Load(object sender, EventArgs e)
{
string ss = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=06E2CCCA8D3D463";
string sql = @"select EmployeeID,Title,BirthDate,Gender,HireDate,ContactID from HumanResources.Employee";
SqlDataAdapter da = new SqlDataAdapter(sql,ss );
DataSet ds = new DataSet();
da.Fill(ds,"Employee");
dataGVtwoseven.DataSource = ds ;
dataGVtwoseven.DataMember = "Employee";
}
private void dataGVtwoseven_DoubleClick(object sender, EventArgs e)
{
//下面的一行中,Cells[5]的意思是选择
//string sql = @"select EmployeeID,Title,BirthDate,Gender,HireDate,ContactID from HumanResources.Employee";
//中所底六个位置的ContactID来作为主键,而来跳转到另一个页面
string cid = dataGVtwoseven.SelectedRows[0].Cells[5].Value.ToString();
contact_table contact = new contact_table(); //这里是以个自定义的.cs 代码是:如下面的
contact.CID = Convert.ToInt32(cid );
contact.ShowDialog();
}
}
}
***********************************************************************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADOtest1027
{
public partial class contact_table : Form
{
private int cid;
public int CID
{
set { cid = value; }
get
{
return cid;
}
}
public contact_table()
{
InitializeComponent();
}
private void contact_table_Load(object sender, EventArgs e)
{
listViewContact.Columns.Add("ContactID",150, HorizontalAlignment.Center);
listViewContact.Columns.Add("FirstName", 150, HorizontalAlignment.Center);
listViewContact.Columns.Add("MiddleName", 150, HorizontalAlignment.Center);
listViewContact.Columns.Add("LastName", 150, HorizontalAlignment.Center);
string ss = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=06E2CCCA8D3D463";
string sql = @"select ContactID,FirstName,MiddleName,LastName from Person.Contact where ContactID="+CID+"";
SqlDataAdapter da = new SqlDataAdapter(sql, ss);
DSEmployeeME ds = new DSEmployeeME();
da.Fill(ds, "ContactME");
foreach (DSEmployeeME.ContactMERow drContact in ds.ContactME.Rows)
{
ListViewItem item = new ListViewItem();
item.SubItems[0].Text = drContact.ContactID.ToString();
item.SubItems.Add( drContact .FirstName);
item.SubItems.Add(drContact .MiddleName );
item.SubItems.Add(drContact .LastName );
listViewContact .Items .Add (item );
}
}
}
}
7:查,排序,修改,显示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADO11_19Sort_AND_find
{
public partial class Form1 : Form
{
private DataView dvperantment = new DataView();
public Form1()
{
InitializeComponent();
}
private void btnload_Click(object sender, EventArgs e)
{
string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=06E2CCCA8D3D463";
string sqlselect = @"select * from HumanResources.Department";
SqlDataAdapter da = new SqlDataAdapter(sqlselect, constr);
DataSet ds = new DataSet();
da.Fill(ds, "Department");
dvperantment = ds.Tables["Department"].DefaultView;
dgvDataView1119.DataSource = dvperantment;
}
private void btnSort_Click(object sender, EventArgs e)
{
dvperantment.Sort = "Name";
dgvDataView1119.DataSource = dvperantment;
}
private void btnFilter_Click(object sender, EventArgs e)
{
dvperantment.RowFilter = "Name like'H%'";
dgvDataView1119.DataSource = dvperantment;
}
private void btnFind_Click(object sender, EventArgs e)
{
dvperantment.Sort = "Name";
DataRowView[] dvp = dvperantment.FindRows("Purchasing");
// DataRowView[] dvp = dvperantment.FindRows("");
foreach (DataRowView dd in dvp )
{
string msg = string.Empty;
msg ="Name:"+dd ["Name"].ToString ()+Environment .NewLine ;
msg += "GroupName:" + dd["GroupName"].ToString() + "\n";
msg += "ModifiedDate:" + dd["ModifiedDate"].ToString();
MessageBox.Show(msg);
}
//MessageBox.Show();
}
}
}