📄 form1.cs
字号:
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 Form Designer generated code
/// <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(280, 256);
this.listView1.TabIndex = 0;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=pubs;Data Source=yinlimin;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=YINLIMIN;Use Encryption for Data=False;Tag with column collation when possible=False";
//
// oleDbCommand1
//
this.oleDbCommand1.CommandText = "SELECT au_id, au_lname, au_fname, city, state FROM authors";
this.oleDbCommand1.Connection = this.oleDbConnection1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
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)
{
this.oleDbConnection1.Open();
System.Data.OleDb.OleDbDataReader Reader;
Reader = oleDbCommand1.ExecuteReader();
this.listView1.View=View.Details;
for(int i=0;i<Reader.FieldCount;i++)
{
this.listView1.Columns.Add(Reader.GetName(i),100,HorizontalAlignment.Left);
}
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);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -