注意: OLE DB 提供程序连接字符串的规范包括一个完整的驱动器和路径声明,用于在默认位置存储示例数据。如果您的数据位于其他位置,请对以下行进行相应的修改。// 用于连接到 TESTDATA 数据库的 Visual FoxPro 7.0 OLE DB
提供程序字符串。
protected string strConnection = @"provider=VFPOLEDB.1 ;data source=
'C:\PROGRAM FILES\MICROSOFT VISUAL FOXPRO 7
\SAMPLES\DATA\TESTDATA.DBC';password='';user id=''";
// 此方法是由 Visual Studio .NET 生成的。
public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
// 首次加载 ASP.NET 页以及每次刷新该页时都会运行
Page_Load 事件。
private void Page_Load(object sender, System.EventArgs e)
{
// 此处放置用于初始化该页的用户代码。
// 如果是第一次呈现该页。
if (Page.IsPostBack == false)
{
// 运行该方法将 Visual FoxPro 7.0 数据绑定到
ASP.NET 页上的 DataGrid。
BindDataGrid();
}
}
// 此方法是由 Visual Studio .NET 生成的。
private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
}
// 此方法将 Visual FoxPro 7.0 数据绑定到
ASP.NET 页上的 DataGrid。
public void BindDataGrid()
{
// 连接到 VFP 数据库。
OleDbConnection VFP7Connection = new OleDbConnection();
VFP7Connection.ConnectionString = strConnection;
VFP7Connection.Open();
// 创建带 select 语句的 Command 对象。
String strSelect = "SELECT * FROM customer";
OleDbCommand VFP7SelectCommand = new
OleDbCommand(strSelect,VFP7Connection);
// 创建 DataAdapter。
OleDbDataAdapter VFP7DataAdapter = new OleDbDataAdapter();
VFP7DataAdapter.SelectCommand = VFP7SelectCommand;
// 创建 DataSet。
DataSet VFP7DataSet = new DataSet();
// 使用表信息填充 DataSet。
VFP7DataAdapter.Fill(VFP7DataSet,"Customer");
// 绑定到 DataGrid。
DataView VFP7DataView = new
DataView(VFP7DataSet.Tables["Customer"]);
dgrCustomers.DataSource = VFP7DataView;
dgrCustomers.DataBind();
}
// 用户选择 ASP.NET 页上的“编辑”时
将调用此方法。
public void DoItemEdit(object objSource,
DataGridCommandEventArgs objArgs)
{
// 将 DataGrid 的 Edit 索引设置为选定的行。
// 启动编辑模式。
dgrCustomers.EditItemIndex = objArgs.Item.ItemIndex;
// 运行该方法将 Visual FoxPro 7.0 数据绑定到
ASP.NET 页上的 DataGrid。
BindDataGrid();
}
// 用户选择 ASP.NET 页上的“取消”时
将调用此方法。
public void DoItemCancel(object objSource,
DataGridCommandEventArgs objArgs)
{
// 将 DataGrid 的 Edit 索引重置为 -1,
退出编辑模式。
dgrCustomers.EditItemIndex = -1;
// 运行该方法将 Visual FoxPro 7.0 数据绑定到
ASP.NET 页上的 DataGrid。
BindDataGrid();
}
// 用户选择 ASP.NET 页上的“更新”时
将调用此方法。
public void DoItemUpdate(object objSource,
DataGridCommandEventArgs objArgs)
{
// 创建对象以保存对所编辑行的引用。
TextBox objCompanyCtrl;
TextBox objContactCtrl;
Label objCustIDCtrl;
// 将这些对象分配给所编辑行中的每个对象。
objCompanyCtrl = (TextBox)objArgs.Item.FindControl("txtCompany");
objContactCtrl = (TextBox)objArgs.Item.FindControl("txtContact");
objCustIDCtrl = (Label)objArgs.Item.FindControl("lblCustID");
// 创建 update select 语句。
strUpdateSelect = "UPDATE Customer SET Company='" +
objCompanyCtrl.Text + "', " +
"Contact='" + objContactCtrl.Text +
"' " + "WHERE Cust_ID='" +
objCustIDCtrl.Text + "'";
// 创建新的连接。
OleDbConnection VFP7Connection = new OleDbConnection();
VFP7Connection.ConnectionString = strConnection;
VFP7Connection.Open();
// 创建带 select 语句的 Command 对象。
OleDbCommand VFP7UpdateCommand = new
OleDbCommand(strUpdateSelect,VFP7Connection);
// 执行该命令。
VFP7UpdateCommand.ExecuteNonQuery();
// 关闭连接。
VFP7Connection.Close();
// 将 DataGrid 的 Edit 索引重置为 -1,
退出编辑模式。
dgrCustomers.EditItemIndex = -1;
// 运行该方法将 Visual FoxPro 7.0 数据绑定到
ASP.NET 页上的 DataGrid。
BindDataGrid();
}
// 这段代码是由 Visual Studio .NET 生成的。
#region Web 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
///
private void InitializeCompone






