在连接数据库读取信息的时候,为什么会出现说我的对象的函数需要连接,各位帮帮忙!谢谢!
    using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Example131_优化只读数据的访问
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbCommand oleDbCommand1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
this.SuspendLayout();
// 
// listView1
// 
this.listView1.Location = new System.Drawing.Point(8, 8);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(272, 248);
this.listView1.TabIndex = 0;
// 
// oleDbConnection1
// 
this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""C:\Documents and Settings\songjia\My Documents\ip_mac.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
// 
// oleDbCommand1
// 
this.oleDbCommand1.CommandText = "SELECT IP, MAC, 计算机名, 位置, 用户名 FROM IP_MAC";
this.oleDbCommand1.Connection = this.oleDbConnection1;
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
System.Data.OleDb.OleDbDataReader Reader;
Reader = oleDbCommand1.ExecuteReader(); //有错误
                           /*错误提示:
未处理的“System.InvalidOperationException”类型的异常出现在 system.data.dll 中。其他信息: ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Closed。*/ //设置listView1的View属性,为listView1添加列并设置列标题
this.listView1.View = View.Details;
for (int i = 0;i < Reader.FieldCount;i++)
{
this.listView1.Columns.Add(Reader.GetName(i),100,HorizontalAlignment.Left);
} //将数据阅览器对象中的数据逐行添加到listView1的各列中
while(Reader.Read())
{
ListViewItem tempItem = new ListViewItem();
tempItem.Text = Reader.GetString(0);
tempItem.SubItems.Add(Reader.GetString(1));
tempItem.SubItems.Add(Reader.GetString(2));
tempItem.SubItems.Add(Reader.GetString(3));
tempItem.SubItems.Add(Reader.GetString(4));
this.listView1.Items.Add(tempItem);
}
}
}
}