如何在各类控件中输入输出数据

如何在各类控件中输入输出数据

思维导图;
如何在各类控件中输入输出数据

一. TextBox文本框
对于TextBox框的输入输出,主要要依赖于TextBox框的Text属性:于控件关联的文本。
(1)输入,在连接上数据库的情况下,将TextBox框中文本值赋给某一参数,然后插入数据库,代码如下:

sqlCommand.Parameters.AddWithValue("@No", this.txt_no.Text.Trim());

(2)输出同理

this.txt_no.Text = sqlDataReader["No"].ToString();

二. Lable框
与TextBox框一样,也是依赖于Text属性:于控件关联的文本。
(1)输入在连接上数据库的情况下,将Lable框中文本值赋给某一参数,然后插入数据库,代码如下:

sqlCommand.Parameters.AddWithValue("@No", this.lbl_no.Text.Trim());

(2)输出同理

this.txt_no.lbl = sqlDataReader["No"].ToString();

三. RadioButton单选按钮
对于RadioButton框的输入输出,主要用于类似于选择性别这样单选的数据,依赖于Checked属性:指示单选按钮是否被选中
(1)输入的代码如下:

sqlCommand.Parameters.AddWithValue("@Sex", (bool)(this.rdb_male.Checked));

(2)输出

this.rdb_male.Checked = (bool)sqlDataReader["Sex"];
this.rdb_remale.Checked = !(bool)sqlDataReader["Sex"];

四. ComboBox组合框
类似于TextBox文本框
(1) 输入

sqlCommand.Parameters.AddWithValue("@Ksno", this.cmb_ks.Text);

(2) 输出

this.cmb_ks.Text  = sqlDataReader["Ksno"].ToString() ;

五. dateTimePicker日期控件
主要依赖于Value属性:该控件当前的日期、时间值
(1) 输入代码如下

sqlCommand.Parameters.AddWithValue("@Birthday", this.dtp_birthday.Value);

(2) 输出

this.dtp_birthday.Value = (DateTime)sqlDataReader["Birthday"];

六. PictureBox图像框
该控件的使用比较复杂,主要依赖于Image属性:在PictureBox显示的图像
(1) 输入
首先在代码中的配置类添加对命名空间的引用

Using  System.Drawing.Imaging;          //包含图像处理功能;

其次,新建一个私有字段PhotoFileName用于存放对话框获取的文件名

private string PhotoFileName;

在配合Button控件选择图片

private void btn_OpenPhoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                           //声明并实例化打开文件对话框;
            {                                                                                           //在初始化器中,设置打开文件对话框的各属性;
                Title = "打开照片文件(任何格式)"                                                      //对话框标题;
                ,
                Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.png"                                            //文件格式过滤器;
                ,
                InitialDirectory = @"C:\"                                                             //初始目录;
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                            //显示打开文件对话框,若打开文件对话框的对话结果为点击OK键;
            {
                this.PhotoFileName = openPhotoDialog.FileName;                                              //将对话框获得的文件名,存入本窗体的私有字段中;
                this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);                                  //调用图像的静态方法FromFile从指定文件中读取图像,并赋予图像框;
            }
        }

最后在添加事件中

MemoryStream memoryStream = new MemoryStream();                                                 //声明并实例化内存流,用于读取照片的字节数据;
            this.ptb_Photo.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);                                       //调用图像框的图像的静态方法Save,将图像保存至内存流;
            byte[] photoBytes = new byte[memoryStream.Length];                                              //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
            memoryStream.Seek(0, SeekOrigin.Begin);                                                         //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
            memoryStream.Read(photoBytes, 0, photoBytes.Length);                                            //将内存流读入字节数组;

然后将图片数据存入数据库

sqlCommand.Parameters.AddWithValue("@Image", photoBytes);

(2) 输出
先实例化一个byte数组

byte[] photoBytes = null;

然后

photoBytes =
                    (sqlDataReader["Image"] == DBNull.Value ? null : (byte[])sqlDataReader["Image"]);       //根据照片是否为数据库空值,分别将空值、转为字节数组的照片数据赋予事先声明的字节数组;
            }
最后

if (photoBytes != null)                                                                         //若学生的照片非空;
            {
                MemoryStream memoryStream = new MemoryStream(photoBytes);                                   //声明并实例化内存流,用于读取照片的字节数据;
                this.ptb_Photo.Image = Image.FromStream(memoryStream);                                      //调用图像的静态方法FromStream从内存流中读取图像,并赋予图像框;
            }

综上,下面有个程序运用了以上知识,实现注册,查询的数据输入输出

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;                                                                                //包含访问SQL Server所需的各类对象;
using System.IO;                                                                                            //包含输入输出对象;
using System.Drawing.Imaging;                                                                               //包含图像处理功能;
using System.Configuration;                                                                                 //包含访问配置文件所需的配置管理器;需事先在本项目的“引用”中添加对System.Configuration的引用;

namespace WindowsFormsApplication1
{
    public partial class frm_adduser : Form
    {

        private string PhotoFileName;

        /// <summary>
        /// 公有方法:构造函数;
        /// </summary>
   

           
        public frm_adduser()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;                                            //本窗体启动位置设为屏幕*;
           
        }

        private void frm_adduser_Load(object sender, EventArgs e)
        {

        }

        private void btn_OpenPhoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                           //声明并实例化打开文件对话框;
            {                                                                                           //在初始化器中,设置打开文件对话框的各属性;
                Title = "打开照片文件(任何格式)"                                                      //对话框标题;
                ,
                Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.png"                                            //文件格式过滤器;
                ,
                InitialDirectory = @"C:\"                                                             //初始目录;
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                            //显示打开文件对话框,若打开文件对话框的对话结果为点击OK键;
            {
                this.PhotoFileName = openPhotoDialog.FileName;                                              //将对话框获得的文件名,存入本窗体的私有字段中;
                this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);                                  //调用图像的静态方法FromFile从指定文件中读取图像,并赋予图像框;
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();                                                 //声明并实例化内存流,用于读取照片的字节数据;
            this.ptb_Photo.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);                                       //调用图像框的图像的静态方法Save,将图像保存至内存流;
            byte[] photoBytes = new byte[memoryStream.Length];                                              //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
            memoryStream.Seek(0, SeekOrigin.Begin);                                                         //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
            memoryStream.Read(photoBytes, 0, photoBytes.Length);                                            //将内存流读入字节数组;
            SqlConnection sqlConnection = new SqlConnection();                                              //声明并实例化SQL连接;
            sqlConnection.ConnectionString =
                "Server=5E5C907E6357438;Database=医院设备管理系统;Integrated Security=sspi";                             //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
            SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
            sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
            sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本;
                "INSERT tb_yg (No,Name,Sex,Birthday,Phone,Ksno,Address,Pwd,Image) VALUES (@No ,@Name,@Sex,@Birthday,@Phone,@Ksno,@Address,@Pwd,@Image)";
            sqlCommand.Parameters.AddWithValue("@No", this.txt_no.Text.Trim());                         //向SQL命令的参数集合添加参数的名称、值;
            sqlCommand.Parameters.AddWithValue("@Name", this.txt_name.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Sex", (bool)(this.rdb_male.Checked));
            sqlCommand.Parameters.AddWithValue("@Birthday", this.dtp_birthday.Value);
            sqlCommand.Parameters.AddWithValue("@Phone", this.txt_phone.Text.Trim ());
            sqlCommand.Parameters.AddWithValue("@Ksno", this.cmb_ks.Text);
            sqlCommand.Parameters.AddWithValue("@Address", this.txt_address.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Pwd", (this.txt_no.Text.Trim()).Substring(this.txt_no.Text.Trim().Length - 4, 4));
            sqlCommand.Parameters.AddWithValue("@Image", photoBytes);
            sqlConnection.Open();                                                                           //打开SQL连接;
            int rowAffected = sqlCommand.ExecuteNonQuery();                                                 //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
            sqlConnection.Close();                                                                          //关闭SQL连接;
            MessageBox.Show("添加成功");                                      //在消息框显示受影响行数;
        }

        private void cmb_ks_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              //声明并实例化SQL连接;
            sqlConnection.ConnectionString =
                ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                             //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
            SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令;
            sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接;
            sqlCommand.CommandText = "SELECT * FROM tb_yg WHERE [email protected];";                             //指定SQL命令的命令文本;该命令查询指定学生;
            sqlCommand.Parameters.AddWithValue("@No",this.txt_no.Text.Trim());                                       //向SQL命令的参数集合添加参数的名称、值;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
            sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
            //DataTable classTable = new DataTable();                                                         //声明并实例化数据表,用于保存所有班级,以用作下拉框数据源;
            sqlConnection.Open();                                                                           //打开SQL连接;
            //sqlDataAdapter.Fill(classTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();                                      //调用SQL命令的方法ExecuteReader来执行命令,并获取数据阅读器;
            byte[] photoBytes = null; 
            if (sqlDataReader.Read())                                                                       //若数据阅读器成功读取到下一条记录(首次查询则表示第一条记录);
            {
                this.txt_no.Text = sqlDataReader["No"].ToString();                                          //在数据阅读器的索引器中指定列名,从而访问当前记录的指定列的值,并赋予相应控件;
                this.txt_name.Text = sqlDataReader["Name"].ToString();
                this.rdb_male.Checked = (bool)sqlDataReader["Sex"];
                this.rdb_remale.Checked = !(bool)sqlDataReader["Sex"];
                this.dtp_birthday.Value = (DateTime)sqlDataReader["Birthday"];
                this.txt_phone.Text =  sqlDataReader["Phone"].ToString ();
                this.cmb_ks.Text  = sqlDataReader["Ksno"].ToString() ;
                this.txt_address.Text = sqlDataReader["Address"].ToString();
                photoBytes =
                    (sqlDataReader["Image"] == DBNull.Value ? null : (byte[])sqlDataReader["Image"]);       //根据照片是否为数据库空值,分别将空值、转为字节数组的照片数据赋予事先声明的字节数组;
            }
            sqlDataReader.Close();                                                                          //关闭数据阅读器(同时关闭连接);
            if (photoBytes != null)                                                                         //若学生的照片非空;
            {
                MemoryStream memoryStream = new MemoryStream(photoBytes);                                   //声明并实例化内存流,用于读取照片的字节数据;
                this.ptb_Photo.Image = Image.FromStream(memoryStream);                                      //调用图像的静态方法FromStream从内存流中读取图像,并赋予图像框;
            }
            }
        }
        
    
}

运行结果如下
在这里插入图片描述
如何在各类控件中输入输出数据

如何在各类控件中输入输出数据

如何在各类控件中输入输出数据

如何在各类控件中输入输出数据