| // 定义数据连接的字符串,程序中使用的是Acess 2000数据库 private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Application.StartupPath + "MY.MDB" ; private OleDbConnection conConnection = new OleDbConnection ( strConnect ) ; OleDbDataReader reader ; // 获得Person里面的所以数据记录 string strCommand = "SELECT * FROM Persons" ; this.conConnection.Open ( ) ; // 打开数据连接 OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ; reader = cmd.ExecuteReader ( ) ; file://获得数据集 |
| // 初始化ListView lv = new ListView ( ) ; lv.Left = 0 ; lv.Top = 0 ; lv.Width = 700 ; lv.Height = this.ClientRectangle.Height ; lv.GridLines = true ; file://显示各个记录的分隔线 lv.FullRowSelect = true ; file://要选择就是一行 lv.View = View.Details ; file://定义列表显示的方式 lv.Scrollable = true ; file://需要时候显示滚动条 lv.MultiSelect = false ; // 不可以多行选择 lv.HeaderStyle = ColumnHeaderStyle.Nonclickable ; // 针对数据库的字段名称,建立与之适应显示表头 lv.Columns.Add ( "姓名" , 60 , HorizontalAlignment.Right ) ; lv.Columns.Add ( "住宅电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "办公电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "移动电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "居住地点" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "工作单位" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "电子邮件" , 100 , HorizontalAlignment.Left ) ; lv.Visible = true ; |
| while ( reader.Read ( ) ) { ListViewItem li = new ListViewItem ( ) ; li.SubItems.Clear ( ) ; li.SubItems[0].Text = reader["name"].ToString ( ) ; li.SubItems.Add ( reader["HomePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["WorkPhone"].ToString ( ) ) ; li.SubItems.Add ( reader["MobilePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["City"].ToString ( ) ) ; li.SubItems.Add ( reader["Address"].ToString ( ) ) ; li.SubItems.Add ( reader["Email"].ToString ( ) ) ; lv.Items.Add ( li ) ; } |
| reader.Close ( ) ; file://关闭数据集 this.conConnection.Close ( ) ; // 关闭数据连接 |
| using System ; using System.Windows.Forms ; using System.Drawing ; using System.Data ; using System.Data.OleDb ; class MainForm : Form { // 定义数据连接的字符串 private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Application.StartupPath + "MY.MDB" ; private OleDbConnection conConnection = new OleDbConnection ( strConnect ) ; private ListView lv ; public MainForm ( ) { // 初始化Form this.Left = 0 ; this.Top = 0 ; this.Text = "在ListView中显示数据库内容!" ; // 初始化ListView lv = new ListView ( ) ; lv.Left = 0 ; lv.Top = 0 ; lv.Width = 700 ; lv.Height = this.ClientRectangle.Height ; lv.GridLines = true ; file://显示各个记录的分隔线 lv.FullRowSelect = true ; file://要选择就是一行 lv.View = View.Details ; file://定义列表显示的方式 lv.Scrollable = true ; file://需要时候显示滚动条 lv.MultiSelect = false ; // 不可以多行选择 lv.HeaderStyle = ColumnHeaderStyle.Nonclickable ; // 针对数据库的字段名称,建立与之适应显示表头 lv.Columns.Add ( "姓名" , 60 , HorizontalAlignment.Right ) ; lv.Columns.Add ( "住宅电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "办公电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "移动电话" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "居住地点" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "工作单位" , 100 , HorizontalAlignment.Left ) ; lv.Columns.Add ( "电子邮件" , 100 , HorizontalAlignment.Left ) ; lv.Visible = true ; OleDbDataReader reader ; string strCommand = "SELECT * FROM Persons" ; this.conConnection.Open ( ) ;// 打开数据连接 OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ; reader = cmd.ExecuteReader ( ) ;//获得数据集 // 不断往列表中添加数据记录 while ( reader.Read ( ) ) { ListViewItem li = new ListViewItem ( ) ; li.SubItems.Clear ( ) ; li.SubItems[0].Text = reader["name"].ToString ( ) ; li.SubItems.Add ( reader["HomePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["WorkPhone"].ToString ( ) ) ; li.SubItems.Add ( reader["MobilePhone"].ToString ( ) ) ; li.SubItems.Add ( reader["City"].ToString ( ) ) ; li.SubItems.Add ( reader["Address"].ToString ( ) ) ; li.SubItems.Add ( reader["Email"].ToString ( ) ) ; lv.Items.Add ( li ) ; } reader.Close ( ) ; // 关闭数据集 // 在Form中添加此列表 this.Controls.Add ( lv ) ; // 关闭Form的时候,同时也关闭数据连接 this.Closed+=new EventHandler ( this_Closed ) ; } protected void this_Closed ( object sender , EventArgs eArgs ) { this.conConnection.Close ( ) ; file://关闭数据连接 } public static void Main ( ) { Application.Run ( new MainForm ( ) ) ; } } |
![]() 图01:用ListView显示数据记录 |
| private static string strConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + Application.StartupPath + "MY.MDB" ; |
| private static string strConnect = "Provider=SQLOLEDB.1 ; Persist Security Info=False ; User ID = sa ; Initial Catalog=数据库名称; Data Source = 服务器名称 " ; |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8781179/viewspace-924598/ ,如需转载,请注明出处,否则将追究法律责任。