按某属性查、直接展现001,(注释中:向数据库中创建表grade2,并插入xml表给的内容)
目录结构:
DB.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;//DataTable用到
using System.Data.SqlClient;//一系列的数据库操作类用到
namespace WindowsFormsApp2checkonly // 和项目名字一致
{
class DB : IDisposable
{
private SqlConnection sqlConnection;
public DB()//私有无参构造函数
{
sqlConnection = new SqlConnection(@"server=.\SQLEXPRESS;database=SCNT;Trusted_Connection=SSPI;");
//sqlConnection = new SqlConnection(@"server=.\SQLEXPRESS;uid=;pwd=;database=SCNT;");//sqlserver身份验证-用户密码版
sqlConnection.Open();
}
public DataTable getBySql(string sql)
{//查询
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(new SqlCommand(sql, sqlConnection));
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
public void setBySql(string sql)
{ //修改
new SqlCommand(sql, sqlConnection).ExecuteNonQuery();
}
public void Dispose()
{//相当于析构函数
sqlConnection.Close();
//在C#中关闭数据库连接不能在类的析构函数中关,否则会抛“内部 .Net Framework 数据提供程序错误 1”的异常
//通过实现C#中IDisposable接口中的Dispose()方法主要用途是释放非托管资源。
}
}
}
Form1.cs的窗体构件
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;//用到了正则表达式
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WindowsFormsApp2checkonly;
using System.Diagnostics;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Linq;
namespace WindowsFormsApp2checkonly
{
public partial class Form1 : Form
{
DB db;
public Form1()
{
InitializeComponent();
db = new DB();
}
private void Form1_Load(object sender, EventArgs e)
{
/* 向数据库中创建表grade2,并插入xml表给的内容
// 创建XmlDocument对象并加载XML文件
var path = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
doc.Load(path + "\\" + "prac1.xml");
// 获取根元素
XmlElement root = doc.DocumentElement;
// 获取所有成绩节点
XmlNodeList grades = root.GetElementsByTagName("grade");
// 创建List存储成绩信息 --List<数据类型> 变量名=new List<数据类型>();
List<Grade> gradeList = new List<Grade>();
//创建表
string mytable = "create table grades2( id varchar(20) primary key, [name] varchar(20),course varchar(20), score int);";
DB dB = new DB();
dB.setBySql(mytable);
//删除表中 王五 的信息;
// string mydelete = "DELETE FROM grades2 WHERE[name] = '王五' ;";
//DB dB = new DB();
//dB.setBySql(mydelete);
// 遍历所有成绩节点并添加到List中
foreach (XmlNode grade in grades)
{
//开始拼接插入的sql语句,逐行插入
// SelectSingleNode 得到指定的节点
// bookModel(模型).BookISBN(属性名字) = xe.GetAttribute("ISBN").ToString(); GetAttribute得到具体的属性值
string id = grade.SelectSingleNode("id").InnerText;
string name = grade.SelectSingleNode("name").InnerText;
string course = grade.SelectSingleNode("course").InnerText;
int score = int.Parse(grade.SelectSingleNode("score").InnerText);
//插入执行语句,我先注释,你需要的时候在用起来
string myinsert = "INSERT INTO grades2(id,name,course,score) VALUES('" + id + "','" + name + "','" + course + "','" + score + "')";
//DB dB = new DB();
dB.setBySql(myinsert);
}
*/
DataTable table;
//1-2
//生成表头
listView2.Columns.Add("公司号", listView1.Width / 2 - 2, HorizontalAlignment.Left);
listView2.Columns.Add("员工数", listView1.Width / 2 - 2, HorizontalAlignment.Left);
//表的内容
table = db.getBySql(@"select CmpNo,count(CmpNo) from [WORKS]" +
" group by [CmpNo]");
listView2.BeginUpdate();//数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
for (int i = 0; i < table.Rows.Count; i++)
{
ListViewItem listViewItem = new ListViewItem();//生成每一列
for (int j = 0; j < table.Columns.Count; j++)
{
if (j <= 0)
{
listViewItem.Text = table.Rows[i][j] + "";
}
else
{
listViewItem.SubItems.Add(table.Rows[i][j] + "");
}
}
listView2.Items.Add(listViewItem);
}
listView2.EndUpdate();//结束数据处理,UI界面一次性绘制。
} //Form1
private void button1_Click(object sender, EventArgs e)
{
listView1.Clear();
//生成表头
listView1.Columns.Add("公司号", listView1.Width / 6 - 1, HorizontalAlignment.Left);
listView1.Columns.Add("员工号", listView1.Width / 6 - 1, HorizontalAlignment.Left);
listView1.Columns.Add("员工姓名", listView1.Width / 6 - 1, HorizontalAlignment.Left);
listView1.Columns.Add("性别", listView1.Width / 6 - 1, HorizontalAlignment.Left);
listView1.Columns.Add("年龄", listView1.Width / 6 - 1, HorizontalAlignment.Left);
listView1.Columns.Add("工资", listView1.Width / 6 - 1, HorizontalAlignment.Left);
//表的内容
DataTable table = db.getBySql(@"select [WORKS].[CmpNo],[EMPLOYEE].[EmpNo],[EMPLOYEE].[EmpName],[EMPLOYEE].[EmpSex],[EMPLOYEE].[EmpAge],[WORKS].[Salary] " +
" from [EMPLOYEE],[WORKS]" +
" where [EMPLOYEE].[EmpNo]=[WORKS].[EmpNo]" +
" and [WORKS].[CmpNo]='" + textBox1.Text + "'" +
" order by [WORKS].[Salary] desc");
listView1.BeginUpdate();//数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
for (int i = 0; i < table.Rows.Count; i++)
{
ListViewItem listViewItem = new ListViewItem();//生成每一列
for (int j = 0; j < table.Columns.Count; j++)
{
if (j <= 0)
{
listViewItem.Text = table.Rows[i][j] + "";
}
else
{
listViewItem.SubItems.Add(table.Rows[i][j] + "");
}
}
listView1.Items.Add(listViewItem);
}
listView1.EndUpdate();//结束数据处理,UI界面一次性绘制
}//button1
}
}