通过SQL 联表查询:(一看就明白)
private void Form1_Load(object sender, EventArgs
e)
{
SqlConnection con = new SqlConnection(@"Data Source=myServer;Initial
Catalog=Northwind;Integrated
Security=True;");//数据连接字符串
SqlDataAdapter dap = new SqlDataAdapter("SELECT Orders.OrderID,
Customers.CompanyName, Orders.OrderDate FROM Orders INNER JOIN Customers ON
Orders.CustomerID=Customers.CustomerID",
con); //INNER
JOIN
DataSet ds = new
DataSet();
dap.Fill(ds,
"table");
dataGridView1.DataSource =
ds.Tables[0].DefaultView;
}
联接ACCESS数据库进行多表查询:
private void
Form1_Load(object sender, EventArgs
e)
{
OleDbConnection
conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=myDataBase.accdb;");
OleDbDataAdapter dap = new OleDbDataAdapter("SELECT Course.course,
Course.classroom, Faculty.faculty_name,Faculty.office,Faculty.email FROM Faculty
INNER JOIN Course ON Faculty.faculty_id =
Course.faculty_id",conn);
DataSet ds = new
DataSet();
dap.Fill(ds);
dataGridView1.DataSource =
ds.Tables[0].DefaultView;
}