数据库学习笔记(七)

1、 创建树形视图

树形视图涉及多张表的查询以及表与表之间的层次关系,首先进行表的查询(用一个命令进行多张表的查询)

SqlCommand sqlCommand = new SqlCommand();                                                       
            sqlCommand.Connection = sqlConnection;                                                                      sqlCommand.CommandText =                                                                        
                "SELECT * FROM tb_Department;"                                                              
                + "SELECT * FROM tb_Major;"
                + "SELECT * FROM tb_Class;";  

声明一个数据集(DataSet)将查询的表放入一个数据集然后放入数据表的网格视图

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                                       sqlDataAdapter.SelectCommand = sqlCommand;                                                     
            DataSet dataSet = new DataSet();                                                                            sqlConnection.Open();                                                                           //打开SQL连接;
   sqlDataAdapter.Fill(dataSet);                                                                   //SQL数据适配器读取数据,并填充数据集;
            sqlConnection.Close();     

声明3张表分别对应数据集的索引器(类似于数组) 

DataTable departmentTable = dataSet.Tables[0];       //声明院系数据表,对应数据集的表集合中的第1张数据表;
            DataTable majorTable = dataSet.Tables[1];    //声明专业数据表,对应数据集的表集合中的第2张数据表;
            DataTable classTable = dataSet.Tables[2];      //声明班级数据表,对应数据集的表集合中的第3张数据表;

声明关系数据组DateRelation进行表与表之间的层次关系划分

 DataRelation[] dataRelations =                                                                  
            {
                new DataRelation                                                                            
                    ("Department_Major"                                                                     
                    , departmentTable.Columns["No"]                                                         
                    , majorTable.Columns["DepartmentNo"]                                                    
                    , false)                                                                                
                , new DataRelation                                                                          
                    ("Major_Class"                                                                          
                    , majorTable.Columns["No"]                                                              
                    , classTable.Columns["MajorNo"]                                                         
                    ,false)                                                                                 
            };
            dataSet.Relations.AddRange(dataRelations);    
声明实例化树形节点,进行数据的批量加载
List<TreeNode> treeNodes = new List<TreeNode>();                                                
            foreach (DataRow departmentRow in departmentTable.Rows)                                         
            {//声明一个departmentRow每次都指向表的一行
                TreeNode departmentNode = new TreeNode();                                                   
                departmentNode.Text = departmentRow["Name"].ToString();                                     
                treeNodes.Add(departmentNode); //添加节点                                                             
                foreach (DataRow majorRow in departmentRow.GetChildRows("Department_Major"))                  
{ //借助先前定义的数据关系,遍历当前院系所在数据行的子行,即下属所有专业; TreeNode majorNode = new TreeNode(); majorNode.Text = majorRow["Name"].ToString(); departmentNode.Nodes.Add(majorNode);
foreach (DataRow classRow in majorRow.GetChildRows("Major_Class")) TreeNode classNode = new TreeNode(); classNode.Text = classRow["Name"].ToString();
classNode.Tag = classRow["No"]; //Tag标签可以记住编号进行信息的载入 majorNode.Nodes.Add(classNode); } } } this.trv_EducationUnit.Nodes.AddRange(treeNodes.ToArray()); 树形视图的进一步优化,选中相应的班级才显示学生的信息,其他不显示 if (this.trv_EducationUnit.SelectedNode.Level == 2) //若树形视图的选中节点的级别为3,即选中班级节点; { int classNo = (int)this.trv_EducationUnit.SelectedNode.Tag; //将树形视图的选中节点的标签转为整型,即可获得事先保存的班级编号; SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = "Server=(local);Database=EduBaseDemo;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT No,Name FROM tb_Student WHERE ClassNo=@ClassNo;";
sqlCommand.Parameters.AddWithValue("@ClassNo", classNo); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataTable studentTable = new DataTable(); sqlConnection.Open();
sqlDataAdapter.Fill(studentTable); sqlConnection.Close();
this.dgv_Student.DataSource = studentTable; this.dgv_Student.Columns["No"].HeaderText = "学号"; this.dgv_Student.Columns["Name"].HeaderText = "姓名"; this.dgv_Student.Columns[this.dgv_Student.Columns.Count - 1].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill; } }}

2使用ADO.NET数据模型,可以进行数据的部分可视化操作

添加新项ADO.NET实体数据模型——>从数据库生成——>新建连接——>Microsoft SQL Sever——>服务器名称填写(local)——>测试连接

using System.Security.Cryptography;//添加新的类用于密码的参数化检验,防止注入式攻击

 

以简单的登录界面为例,通过上述的步骤可以省去数据库的连接,命令的实例化代码

MD5CryptoService Provider MD5=new MD5CryptoService();
byte[]passwordBytes=Encoding.Default.GetBytes(txb_password);
Byte[]passwordHashed=MD5.computeHash(passwordBytes);

EdeBase eduBase= new EduBase;
var User=(from u in eduBase.User
  Where u.No=this.txt_UserNo.Text.Trim()&& u.password.Hashed
Select u).FirstorDefault();//FirstorDefault()防止查询数据是产生多条数据或者查询不到数据时产生的错误
If(user!=null){
MessageBox(“登录成功”);}
Else{ MessageBox(“用户名或者密码错误”);
 this.txb_password.Focus;
this.txb_password.SelectAll;}

 

 

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                                       sqlDataAdapter.SelectCommand = sqlCommand;                                                     

            DataSet dataSet = new DataSet();                                                                            sqlConnection.Open();                                                                           //打开SQL连接;

   sqlDataAdapter.Fill(dataSet);                                                                   //SQL数据适配器读取数据,并填充数据集;

            sqlConnection.Close();     

posted @ 2017-11-05 17:28  Angular_JS  阅读(267)  评论(0编辑  收藏  举报