zoukankan      html  css  js  c++  java
  • winform

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Drawing;
      5 using System.Linq;
      6 using System.Text;
      7 using System.Threading.Tasks;
      8 using System.Windows.Forms;
      9 
     10 using System.Data;                                  // 引用【AD0.NET】命名空间
     11 using System.Data.SqlClient;                 // sql
     12 
     13 namespace DataBase
     14 {
     15     public partial class Form1 : Form
     16     {
     17         public Form1()
     18         {
     19             InitializeComponent();
     20         }
     21         
     22         // 窗体载入
     23         private void Form1_Load(object sender, EventArgs e)
     24         {
     25             //编号, 品名, 规格, 材质, 库存, 价格, 仓库
     26             this.lstviewRecordData.Items.Clear();
     27             this.lstviewRecordData.View = View.Details;        // lstviewRecordData视图样式: 详细信息
     28             this.lstviewRecordData.GridLines = true;              // 网格线 
     29             this.lstviewRecordData.CheckBoxes = true;         // 复选框
     30             ColumnHeader ch = new ColumnHeader();
     31             ch.Text = "编号";                                                         // 标题
     32             ch.TextAlign = HorizontalAlignment.Center;          // 居中
     33             ch.Width = 50;                                                            // 宽度
     34             this.lstviewRecordData.Columns.Add(ch);            // 添加列头
     35             this.lstviewRecordData.Columns.Add("品名", 80, HorizontalAlignment.Center);
     36             this.lstviewRecordData.Columns.Add("规格",180, HorizontalAlignment.Center);
     37             this.lstviewRecordData.Columns.Add("材质", 80, HorizontalAlignment.Center);
     38             this.lstviewRecordData.Columns.Add("库存", 100, HorizontalAlignment.Center);
     39             this.lstviewRecordData.Columns.Add("价格", 100, HorizontalAlignment.Center);
     40             this.lstviewRecordData.Columns.Add("仓库", 100, HorizontalAlignment.Center);
     41             this.lstviewRecordData.FullRowSelect = true;
     42 
     43         }
     44         private void btnReadDataBase_Click(object sender, EventArgs e)
     45         {
     46             lstviewRecordData.Items.Clear();
     47             // 绑定数据库
     48             BindDataBase();
     49         }
     50 
     51         /// <summary>   绑定数据库
     52         /// 
     53         /// </summary>
     54         private void BindDataBase()
     55         {
     56             // 连接指定数据库
     57             SqlConnection conn = new SqlConnection();
     58 
     59             /***********************************************************************
     60              *  使用字符串声明连接所需要的信息:
     61              *      1. 服务器;     
     62              *      2. 数据库 ;    
     63              *      3. 验证: 
     64              *              3.1 Windows本地验证: Integrated Security = true;
     65              *              3.2 SqlServer验证: user id = 登录名; password = 密码
     66             ***********************************************************************/
     67             conn.ConnectionString = "Server = 127.0.0.1; DataBase = DuanLaoYeDataBase;"+
     68                                     " user id = DuanLaoYe; password = 123456";
     69             try
     70             {
     71                 // 打开连接
     72                 conn.Open();
     73                 //MessageBox.Show(conn.State.ToString());       // 测试conn的状态,打开或关闭
     74 
     75 
     76                 // 构造数据库命令操作对象,用于执行T-SQL语句
     77                 SqlCommand cmd = new SqlCommand();
     78                 // 执行操作的T-SQL语句或者存储过程
     79                 cmd.CommandText = "select * from Steel";
     80                 // 执行操作所需要的连接
     81                 cmd.Connection = conn;
     82 
     83 
     84                 //声明结果集对象 , 用于检索数据 ; 结果集的来自cmd的ExecuteReader()方法
     85                 SqlDataReader dr = cmd.ExecuteReader();
     86                 // 遍历dr数据 , 判断有无数据可读
     87                 if (dr.HasRows)
     88                 {
     89                     // 开始更新数据, 挂起lstviewRecordData的UI绘制
     90                     this.lstviewRecordData.BeginUpdate();
     91                     while (dr.Read())           // 循环读取所有数据
     92                     {
     93                         ListViewItem lvi = new ListViewItem();
     94                         lvi.Text = dr[0].ToString();
     95                         for (int i = 1; i <= 6; ++i )
     96                         {                      
     97                             lvi.SubItems.Add(dr[i].ToString().Trim());              
     98                         }
     99                         this.lstviewRecordData.Items.Add(lvi);
    100                     }
    101                     // 结束更新数据, 开始绘制UI(UI一次性绘制)
    102                     this.lstviewRecordData.EndUpdate();              
    103                 }
    104                 else
    105                 {
    106                     MessageBox.Show("结果集无数据!");
    107                 }
    108             }
    109             catch (SqlException ex)
    110             {
    111                 MessageBox.Show("数据库操作遇到异常 : " + ex.Message);
    112             }
    113             finally
    114             {
    115                 // 判断当前的conn连接状态, 如果未关闭
    116                 if (conn.State != ConnectionState.Closed)
    117                 {
    118                     // 则关闭连接
    119                     conn.Close();               
    120                 }
    121             }          
    122         }
    123 
    124         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    125         {
    126             if (DialogResult.Yes == MessageBox.Show("确定删除当前所有选中数据?", "提示", MessageBoxButtons.YesNo))
    127             {
    128                 this.lstviewRecordData.BeginUpdate();
    129                 // 遍历选中项
    130                foreach (ListViewItem lvi in lstviewRecordData.SelectedItems)
    131                {
    132                    // 按索引删除选中项
    133                    //this.lstviewRecordData.Items.RemoveAt(lvi.Index);
    134 
    135                    // 按项删除选中项
    136                    this.lstviewRecordData.Items.Remove(lvi);
    137                }
    138                this.lstviewRecordData.EndUpdate();
    139             }
    140         }
    141     }
    142 }

  • 相关阅读:
    BNUOJ 12756 Social Holidaying(二分匹配)
    HDU 1114 Piggy-Bank(完全背包)
    HDU 2844 Coins (多重背包)
    HDU 2602 Bone Collector(01背包)
    HDU 1171 Big Event in HDU(01背包)
    HDU 2571 命运 (入门dp)
    HDU 1069 Monkey and Banana(最长递减子序列)
    HDU 1160 FatMouse's Speed (最长上升子序列)
    HDU 2594 KMP
    POJ 3783 Balls --扔鸡蛋问题 经典DP
  • 原文地址:https://www.cnblogs.com/DuanLaoYe/p/5374472.html
Copyright © 2011-2022 走看看