📄 ex-14-02
字号:
// Example 14-02: Using the ADO Managed Provider
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace ProgrammingCSharpWinForm
{
public class ADOForm1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.ListBox lbCustomers;
public ADOForm1()
{
InitializeComponent();
// connect to Northwind Access database
string connectionString =
"provider=Microsoft.JET.OLEDB.4.0; "
+ "data source = c:\\nwind.mdb";
// get records from the customers table
string commandString =
"Select CompanyName, ContactName from Customers";
// create the data set command object
// and the DataSet
OleDbDataAdapter DataAdapter =
new OleDbDataAdapter(
commandString, connectionString);
DataSet DataSet = new DataSet();
// fill the data set object
DataAdapter.Fill(DataSet,"Customers");
// Get the one table from the DataSet
DataTable dataTable = DataSet.Tables[0];
// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
{
lbCustomers.Items.Add(
dataRow["CompanyName"] +
" (" + dataRow["ContactName"] + ")" );
}
}
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
private void InitializeComponent()
{
this.components =
new System.ComponentModel.Container ();
this.lbCustomers = new System.Windows.Forms.ListBox ();
lbCustomers.Location = new System.Drawing.Point (48, 24);
lbCustomers.Size = new System.Drawing.Size (368, 160);
lbCustomers.TabIndex = 0;
this.Text = "ADOFrm1";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (464, 273);
this.Controls.Add (this.lbCustomers);
}
public static void Main(string[] args)
{
Application.Run(new ADOForm1());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -