学习笔记:如何在C#各类控件中输入/输出数据

 

1、思维导图

2、声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令

将SQL数据适配器的查询命令属性指向SQL命令

声明并实例化数据表,以用作下拉框数据源

SQL数据适配器读取数据,并填充数据表

将下拉框的显示成员设为数据表的名称列

调用SQL命令的方法ExecuteReader来执行命令,并获取数据阅读器

在数据阅读器的索引器中指定列名,从而访问当前记录的指定列的值,并赋予相应控件

3、

public frm_doctorinformation()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                            
            sqlConnection.ConnectionString =
                "Server=(local);Database=医院门诊预约管理系统;Integrated Security=sspi";                             
            SqlCommand sqlCommand = new SqlCommand();
            SqlCommand sqlCommand1 = new SqlCommand();                                          
            SqlCommand sqlCommand2 = new SqlCommand();                                                      
            sqlCommand.Connection = sqlConnection;
            sqlCommand1.Connection = sqlConnection;                                              
            sqlCommand2.Connection = sqlConnection;                                                         
            sqlCommand.CommandText = "SELECT * FROM tb_doctorTitle;";                                             
            sqlCommand1.CommandText = "SELECT * FROM tb_Department;";
           sqlCommand2.CommandText = "SELECT * FROM tb_doctor WHERE No_hospital=@No_hospital AND No_doctor=@No_doctor";                             
           sqlCommand2.Parameters.AddWithValue("@No_doctor", this.txb_Nodoctor.Text);
           sqlCommand2.Parameters.AddWithValue("@No_hospital", this.txb_Nohospital.Text);                          
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            sqlDataAdapter1.SelectCommand = sqlCommand1;                                         
            DataTable tb_doctorTitle = new DataTable();
            DataTable tb_Department = new DataTable();                                         
            sqlConnection.Open();                                                                           
            sqlDataAdapter.Fill(tb_doctorTitle);
            sqlDataAdapter1.Fill(tb_Department);                                                    
            this.cmb_title.DataSource = tb_doctorTitle;                                                         
            this.cmb_Department.DataSource = tb_Department
            this.cmb_Department.DisplayMember = "Name";
            this.cmb_title .DisplayMember = "Title";                                                          
            this.cmb_title .ValueMember = "No_doctorTitle";
            this.cmb_Department.ValueMember = "No";                                                
            SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();                                      
            if (sqlDataReader.Read())                                                                       
            {
                this.txb_Namedoctor .Text = sqlDataReader["Name_doctor"].ToString();                                          
                this.dtp_employDate.Value = (DateTime)sqlDataReader["BirthDate_doctor"];
                this.rdb_doctorMale.Checked = (bool)sqlDataReader["Gender_doctor"];
                this.rdb_doctorFemale .Checked = !(bool)sqlDataReader["Gender_doctor"];
                this.dtp_BirthdateDoctor .Value = (DateTime)sqlDataReader["BirthDate_doctor"];
                this.cmb_title.SelectedValue = (int)sqlDataReader["TitleNo"];
                this.cmb_Department.SelectedValue = (int)sqlDataReader["Department_No"];
                this.txb_Graduatedoctor.Text = sqlDataReader["Graduate_doctor"].ToString();
                this.txb_Remaindoctor.Text = sqlDataReader["Remain_Doctor"].ToString();
            }
            sqlDataReader.Close();    
        }

当输入医生编号以及医院标识码时,点击查询将查询到医生的具体信息

 

posted @ 2018-10-17 19:27  ZH512627  阅读(444)  评论(0编辑  收藏  举报