第九周学习笔记

Posted wllw6

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第九周学习笔记相关的知识,希望对你有一定的参考价值。

第九周学习笔记

一、本周首先讲了ADO.NET的应用,使用ADO.NET连接数据库,它的代码更简便一些,但是运行速度较平常的代码慢。

1.步骤:项目—添加—新建项,选用ADO.NET Entity Data Model模板。

2.按照提示的步骤一直下去,直到数据库连接成功。

代码如下截屏所示:

 技术分享图片

技术分享图片

 

 技术分享图片

 技术分享图片

数据库连接成功。

注意:选择服务器时,建议写(local),这样下次到另一台电脑使用会比较方便。

 

1.本次示例为制作一个登录界面:

①数据库建库、建表、插入数据等代码如下:

 技术分享图片

 

②最终运行界面如下:

 技术分享图片

 

C#中代码如下:

//添加调用:

using System.Data.SqlClient

//包含访问SQL Server所需的各类对象;

using System.Security.Cryptography;

namespace Ex22_Command_Parameter

{

public partial class frm_Login : Form

{

/// <summary>

/// 公有方法:构造函数;

/// </summary>

public frm_Login()

{

InitializeComponent();

this.StartPosition=FormStartPosition.CenterScreen;                                       

//本窗体启动位置设为屏幕中央;

}

private void btn_Login_Click(object sender, EventArgs e)

{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

byte[] passwordBytes = Encoding.Default.GetBytes(txb_Password .Text .Trim ());

byte[] passwordHashed = md5.ComputeHash(passwordBytes );

 

EduBaseDemo edubase=new EduBaseDemo ();

var user=(from us in edubase.User

where us.No ==this.txb_UserNo .Text .Trim () && us.Password ==passwordHashed

select us).FirstOrDefault ();

 

if (user!=null)                                                                         

//若查得所输用户号相应的1行记录;

{

MessageBox.Show("登录成功。");                                                         

//显示正确提示;

}

else                                                                                       

//否则;

{

MessageBox.Show("用户号/密码有误,请重新输入!");                                      

//显示错误提示;

this.txb_Password.Focus();                                                             

//密码文本框获得焦点;

this.txb_Password.SelectAll();                                                          

//密码文本框内所有文本被选中;

}

}

}

}

 

二、本周另外主要讲解了数据集、数据关系的运用。

创建窗体,加入树形视图、数据表和载入按钮。

实现的功能有:点击载入按钮,载入医院表、科室表和科室职能表(即具体疾病、职能),以树形视图的模式显现出来。点击叶子节点的内容,在右边的数据表显示病人的信息。

C#主要代码如下:

using System.Data.SqlClient;

namespace _5._6数据集_数据关系

{

public partial class frm_OutpatientService : Form

{

public frm_OutpatientService()

{

InitializeComponent();

this.StartPosition = FormStartPosition.CenterScreen;

this.dgv_Patient.AllowUserToAddRows = false;

this.dgv_Patient.RowHeadersVisible = false;

this.dgv_Patient.BackgroundColor = Color.White;

this.dgv_Patient.AutoSizeColumnsMode =

DataGridViewAutoSizeColumnsMode.AllCells;

}

 

private void btn_Load_Click(object sender, EventArgs e)

{

SqlConnection sqlConnection = new SqlConnection();

sqlConnection.ConnectionString =

"Server=(local);Database=HISDatabase;uid=sa;[email protected]";

SqlCommand sqlCommand = new SqlCommand();

sqlCommand.Connection = sqlConnection;

sqlCommand.CommandText =

"SELECT * FROM tb_Hospital;"

+ "SELECT * FROM tb_Department;"

+ "SELECT * FROM tb_Disease;";

 

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();

sqlDataAdapter.SelectCommand = sqlCommand;

DataSet dataSet = new DataSet();

sqlConnection.Open();

sqlDataAdapter.Fill(dataSet);

sqlConnection.Close();

DataTable hospitalTable = dataSet.Tables[0];

DataTable departmentTable = dataSet.Tables[1];

DataTable diseaseTable = dataSet.Tables[2];

DataRelation[] dataRelations =                                                                  

//声明数据关系数组;

{

new DataRelation                                                                            

//实例化数据关系,实现医院表、科室表之间的层次关系;

("Hospital_Department"                                                                     

//数据关系名称;

, hospitalTable.Columns["No"]                                                         

//父表的被参照列为院系表的编号列;

, departmentTable.Columns["HospitalNo"]                                                    

//子表的参照列为专业表的院系编号列;

, false)                                                                                

//不创建约束(父列上的唯一约束、子列上的外键约束);

, new DataRelation                                                                         

 //实例化数据关系,实现科室表、疾病表之间的层次关系;

("Department_Disease"                                                                          //数据关系名称;

, departmentTable.Columns["No"]                                                              

//父表的被参照列为专业表的编号列;

, diseaseTable.Columns["DepartmentNo"]                                                         //子表的参照列为疾病表的专业编号列;

,false)                                                                                 

//不创建约束(父列上的唯一约束、子列上的外键约束);

};

dataSet.Relations.AddRange(dataRelations);                                                      

//将数据关系数组批量加入数据集的关系集合中;

this.trv_HospitalUnit.Nodes.Clear();                                                           

//树形视图的节点集合清空;

List<TreeNode> treeNodes = new List<TreeNode>();                                                

//声明并实例化树形节点列表;

foreach (DataRow hospitalRow in hospitalTable.Rows)                                         

//遍历院系数据表中的每一数据行;

{

TreeNode hospitalNode = new TreeNode();                                                   

//声明并实例化医院表节点,该节点对应当前某个部门;

hospitalNode.Text = hospitalRow["Name"].ToString();                                     

//医院表节点的文本设为当前部门的名称;

treeNodes.Add(hospitalNode);                                                              

//医院表节点加入树形节点列表,成为第0级节点之一;

foreach (DataRow departmentRow in hospitalRow.GetChildRows("Hospital_Department"))                

//借助先前定义的数据关系,遍历当前医院所在数据行的子行,即下属所有科室;

{

TreeNode departmentNode = new TreeNode();                                                    

//声明并实例化科室节点,该节点对应当前某个科室;

departmentNode.Text = departmentRow["Name"].ToString();                                           //科室节点的文本设为当前科室的名称;

hospitalNode.Nodes.Add(departmentNode);                                                    

//科室节点加入当前医院节点的节点集合,成为第1级节点之一;

foreach (DataRow diseaseRow in departmentRow.GetChildRows("Department_Disease"))                      //借助先前定义的数据关系,遍历当前科室所在数据行的子行,即下属所有疾病;

{

TreeNode diseaseNode = new TreeNode();                                                

//声明并实例化疾病节点,该节点对应当前某个疾病;

diseaseNode.Text = diseaseRow["Name"].ToString();                                       

//疾病节点的文本设为当前疾病的名称;

diseaseNode.Tag = diseaseRow["No"];                                                     

//疾病节点的标签设为当前疾病的编号;

departmentNode.Nodes.Add(diseaseNode);                                                     

//疾病节点加入当前科室节点的节点集合,成为第2级节点之一;

}

}

}

this.trv_HospitalUnit.Nodes.AddRange(treeNodes.ToArray());                                       

//将树形节点列表转为数组,并批量加入树形视图的节点集合;

}

 

private void trv_HospitalUnit_AfterSelect(object sender, TreeViewEventArgs e)

{

if (this.trv_HospitalUnit.SelectedNode.Level == 2)                                             

//若树形视图的选中节点的级别为3,即选中疾病节点;

{

int diseaseNo = (int)this.trv_HospitalUnit.SelectedNode.Tag;                                 

//将树形视图的选中节点的标签转为整型,即可获得事先保存的疾病编号;

SqlConnection sqlConnection = new SqlConnection();                                          

sqlConnection.ConnectionString =

"Server=(local);Database=HISDatabase;uid=sa;[email protected]";                         

SqlCommand sqlCommand = new SqlCommand();                                                   

sqlCommand.Connection = sqlConnection;                                                      

sqlCommand.CommandText = "SELECT No,Name,Phone FROM tb_Patient WHERE [email protected];";          //指定SQL命令的命令文本;该命令查询当前选中班级的所有病人名单,以用作数据网格视图数据源;

sqlCommand.Parameters.AddWithValue("@DiseaseNo", diseaseNo);                                    //SQL命令的参数集合添加参数的名称、值;

 

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                       

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

sqlDataAdapter.SelectCommand = sqlCommand;                                                  

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

 

DataTable patientTable = new DataTable();                                                   

//声明并实例化数据表,用于保存当前选中班级的所有病人名单,以用作数据网格视图的数据源;

sqlConnection.Open();                                                                       

//打开SQL连接;

sqlDataAdapter.Fill(patientTable);                                                          

//SQL数据适配器读取数据,并填充疾病数据表;

sqlConnection.Close();                                                                      

//关闭SQL连接;

 

this.dgv_Patient.DataSource = patientTable;                                                

 //设置数据网格视图的数据源;

this.dgv_Patient.Columns["No"].HeaderText = "就诊卡号";                                         

//将数据网格视图的指定列的表头文本设为中文;

this.dgv_Patient.Columns["Name"].HeaderText = "姓名";

this.dgv_Patient.Columns["Phone"].HeaderText = "电话";

this.dgv_Patient.Columns[this.dgv_Patient.Columns.Count - 1].AutoSizeMode =                 

//数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘);

DataGridViewAutoSizeColumnMode.Fill;

}

}

}

}

运行如下:点击“脑血管疾病”选项,显示出了三位病人的信息。点击“产前检查”选项,显示出了一位病人的信息。

 技术分享图片

 技术分享图片

以上是关于第九周学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

第九周学习笔记

学习笔记第九周

Python第九周 学习笔记

第九周学习笔记

20165204 Java第九周学习

20179223《Linux内核原理与分析》第九周学习笔记