第六周学习笔

(学习笔记)如何在各类控件中实现信息的输入输出:

一、知识点描述
1、相关控件
①下拉框(ComboBox)
显示一个可编辑的文本框,其中包含一个允许值下拉列表。
②日历框(DateTimePick)
允许用户选择日期和时间,并以指定的格式显示该日期和时间。
③文本框(TextBox)
允许用户输入文本,并提供多行编辑和密码字符掩码功能。
④图片框(PictureBox)
指定支持事务处理初始化,允许用户加载图片。
⑤单选框(RadioButton)
当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。
二、思维导图

 

 

 
三、示例代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
 
namespace 护士工作站
{
    public partial class 信息管理 : Form
    {
        private string PhotoFileName;
        public 信息管理()
        {
            InitializeComponent();
            this.工号.Text = 登录.txt;
        }
        //button1_Click事件下的代码为载入代码
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                             
            sqlConnection.ConnectionString =
                "Server=(local);Database=EduBase1;Integrated Security=sspi";                    
            SqlCommand sqlCommand = new SqlCommand();                                                      
            SqlCommand sqlCommand2 = new SqlCommand();                                                     
            sqlCommand.Connection = sqlConnection;                                                         
            sqlCommand2.Connection = sqlConnection;                                                        
            sqlCommand.CommandText = "SELECT * FROM tb_Room;";                                            
            sqlCommand2.CommandText = "SELECT * FROM tb_Nurse WHERE No=@No;";                                      
           sqlCommand2.Parameters.AddWithValue("@No", this.工号.Text.Trim());                                                  
          SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                          
            sqlDataAdapter.SelectCommand = sqlCommand;                                                     
            DataTable roomTable = new DataTable();                                                        
            sqlConnection.Open();                                                                          
            sqlDataAdapter.Fill(roomTable);                                                               
            this.所属科室.DataSource = roomTable;                                                        
            this.所属科室.DisplayMember = "Name";                                                         
            this.所属科室.ValueMember = "No";                                                             
            SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();                                     
            byte[] photoBytes = null;                                                                      
            if (sqlDataReader.Read())                                                                      
            {
                this.工号.Text = sqlDataReader["No"].ToString();                                         
                this.姓名.Text = sqlDataReader["Name"].ToString();
                this.rdb_Male.Checked = (bool)sqlDataReader["Gender"];
                this.rdb_Female.Checked = !(bool)sqlDataReader["Gender"];
                this.职称.Text = sqlDataReader["Title"].ToString();
                this.所属科室.SelectedValue =(int) sqlDataReader["RoomNo"];
                photoBytes =
                    (sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]);     
            }
            sqlDataReader.Close();                                                                                   
      if (photoBytes != null)                                                                        
            {
                MemoryStream memoryStream = new MemoryStream(photoBytes);                                  
                this.照片.Image = Image.FromStream(memoryStream);                                     
            }
        }
        //button3_Click事件下的代码为打开照片代码
        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                          
            {                                                          
                Title = "打开照片文件(位图格式)"                                                     
                ,
                Filter = "BMP Files (*.bmp)|*.bmp"                                                   
                ,
                InitialDirectory = @"C:\"                                                            
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                           
            {
                this.PhotoFileName = openPhotoDialog.FileName;                                             
                this.照片.Image = Image.FromFile(this.PhotoFileName);                                 
            }
        }
        //button2_Click事件下的代码为载入代码
        private void button2_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();                                                
            this.照片.Image.Save(memoryStream, ImageFormat.Bmp);                                                 
      byte[] photoBytes = new byte[memoryStream.Length];                                             
            memoryStream.Seek(0, SeekOrigin.Begin);                                                        
            memoryStream.Read(photoBytes, 0, photoBytes.Length);                                           
            SqlConnection sqlConnection = new SqlConnection();                                             
            sqlConnection.ConnectionString =
                "Server=(local);Database=EduBase1;Integrated Security=sspi";                    
            SqlCommand sqlCommand = new SqlCommand();                                                      
            sqlCommand.Connection = sqlConnection;                                                         
            sqlCommand.CommandText =                                                                       
                "UPDATE tb_Nurse"
                + " SET Name=@Name,Gender=@Gender,Title=@Title,RoomNo=@RoomNo,Photo=@Photo"
                + " WHERE No=@No;";
            sqlCommand.Parameters.AddWithValue("@Name", this.姓名.Text.Trim());                 
            sqlCommand.Parameters.AddWithValue("@Gender", this.rdb_Male.Checked);
            sqlCommand.Parameters.AddWithValue("@Title", this.职称.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@RoomNo", (int)this.所属科室.SelectedValue);
            sqlCommand.Parameters.AddWithValue("@Photo", photoBytes);
            sqlCommand.Parameters.AddWithValue("@No", this.工号.Text.Trim());
            sqlConnection.Open();                                                                          
            int rowAffected = sqlCommand.ExecuteNonQuery();                                                
            sqlConnection.Close();                                                                         
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");    
        }
    }
}

转载于:https://www.cnblogs.com/JKOBGHJ/p/9803528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值