1. 程式人生 > >如何在各類控制元件中輸入/輸出資料(學習筆記)

如何在各類控制元件中輸入/輸出資料(學習筆記)

一、知識點描述

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 [email protected];";                                       

           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 [email protected],[email protected],[email protected],[email protected],[email protected]"

                + " WHERE [email protected];";

            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() + "行。");     

        }

    }

}

 

四、效果截圖

1、首先進入資訊管理頁面

 

2、載入該護士的個人資訊

3、在開啟照片中選擇自己需要載入的圖片

4、若要更新個人資訊可直接在頁面上更改內容

5、點選更新可提示更新的行數

6、這時再點選載入就是你更新的內容