.net窗体程序要点(速记)

SqlManage.cs

using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

 1 //public static string cnn_string = @"server=DESKTOP-48M8EJT\MSSQLSERVER1;database=tmp2015;integrated security=SSPI";
 2 public static string cnnstring = @"Server=AN2XOC6HS03APKT;Database=tmp2019;Uid=sa;Pwd=liwei666;";
 3 public static SqlConnection cnn = null;
 4 
 5 /// 
 6 /// 数据库查询操作
 7 /// 
 8 /// 数据库语句
 9 public static void ExcuteTable(string sql)
10 {
11     try
12     {
13         SqlCommand cmd = new SqlCommand(sql, SqlManage.cnn);
14         if (cmd.ExecuteNonQuery() > 0)
15         {
16             MessageBox.Show("操作成功");
17         }
18         else
19         {
20             MessageBox.Show("操作失败!\n检查数据库是否有该记录");
21         }
22     }
23     catch (Exception ex)
24     {
25         MessageBox.Show(ex.Message);
26     }
27 }
28 
29 /// 
30 /// 数据库更新操作(包括插入、修改、删除)
31 /// 
32 /// 数据库语句
33 /// 返回查询结果
34 public static DataTable SelectTable(string sql)
35 {
36     try
37     {
38         DataTable table = new DataTable();
39         SqlDataAdapter sda = new SqlDataAdapter(sql, SqlManage.cnn);
40         sda.Fill(table);
41         return table;
42     }
43     catch (Exception ex)
44     {
45         MessageBox.Show(ex.Message);
46         return null;
47     }
48 }
49 
50 /// 
51 /// 设置DataGridView控件的属性
52 /// 
53 /// 控件名
54 public static void SetDataGridView(DataGridView d)
55 {
56     d.ReadOnly = true;
57     d.AllowUserToAddRows = false;
58     d.RowHeadersVisible = false;
59     d.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
60 }
61 
62 /// 
63 /// 判断TextBox控件是否输入了内容
64 /// 
65 /// 控件名
66 /// 输入了内容发挥true
67 public static bool JudTextBox(TextBox t)
68 {
69     return t.Text.Trim().Length > 0 ? true : false;
70 }

 

Program.cs

using System.Data.SqlClient;

 1 Application.EnableVisualStyles();
 2 Application.SetCompatibleTextRenderingDefault(false);
 3 try
 4 {
 5     SqlManage.cnn = new SqlConnection(SqlManage.cnnstring);
 6     SqlManage.cnn.Open();
 7 }
 8 catch (Exception ex)
 9 {
10     MessageBox.Show(ex.Message);
11     Application.Exit();
12     return;
13 }
14 Application.Run(new FormMain());
15 
16 SqlManage.cnn.Close();

 

插入数据:

 1 if (!SqlManage.JudTextBox(this.textBox1))
 2 {
 3     MessageBox.Show("输入员工编号");
 4     return;
 5 }
 6 if (!SqlManage.JudTextBox(this.textBox2))
 7 {
 8     MessageBox.Show("输入员工姓名");
 9     return;
10 }
11 if (!SqlManage.JudTextBox(this.textBox3))
12 {
13     MessageBox.Show("输入员工年龄");
14     return;
15 }
16 int age;
17 if (!int.TryParse(this.textBox3.Text, out age))
18 {
19     MessageBox.Show("年龄必须是整数");
20     return;
21 }
22 string sex = this.radioButton1.Checked ? "" : "";
23 string sql = string.Format("insert into EMPLOYEE values('{0}','{1}','{2}','{3}')",
24     this.textBox1.Text.Trim(), this.textBox2.Text.Trim(), sex, age);
25 SqlManage.ExcuteTable(sql);

 

修改数据:

 1 if (!SqlManage.JudTextBox(this.textBox1))
 2 {
 3     MessageBox.Show("输入员工编号");
 4     return;
 5 }
 6 if (!SqlManage.JudTextBox(this.textBox2))
 7 {
 8     MessageBox.Show("输入员工姓名");
 9     return;
10 }
11 if (!SqlManage.JudTextBox(this.textBox3))
12 {
13     MessageBox.Show("输入员工年龄");
14     return;
15 }
16 int age;
17 if (!int.TryParse(this.textBox3.Text, out age))
18 {
19     MessageBox.Show("年龄必须是整数");
20     return;
21 }
22 string sex = this.radioButton1.Checked ? "" : "";
23 string sql = string.Format("update EMPLOYEE set  EmpName='{0}',EmpSex='{1}',EmpAge='{2}' where EmpNo='{3}'",
24      this.textBox2.Text.Trim(), sex, age, this.textBox1.Text.Trim());
25 SqlManage.ExcuteTable(sql);

 

删除数据:

1 if (!SqlManage.JudTextBox(this.textBox1))
2 {
3     MessageBox.Show("输入员工编号");
4     return;
5 }
6 string sql = string.Format("delete from EMPLOYEE where EmpNo='{0}'", this.textBox1.Text.Trim());
7 SqlManage.ExcuteTable(sql);

 

显示网格:

1 SqlManage.SetDataGridView(this.dataGridView1);
2 string sql = "select EmpNO as 员工编号,EmpName as 员工姓名,EmpSex as 员工表性别,EmpAge as 员工年龄 from EMPLOYEE";
3 DataTable table = SqlManage.SelectTable(sql);
4 this.dataGridView1.DataSource = table;

 

下拉框查找:

 1 SqlManage.SetDataGridView(this.dataGridView1);
 2 
 3 //初始化教师编号
 4 string sql = "select EmpNo from EMPLOYEE";
 5 DataTable table = SqlManage.SelectTable(sql);
 6 string empno;
 7 foreach (DataRow row in table.Rows)
 8 {
 9     empno = row["EmpNo"].ToString();
10     this.comboBox1.Items.Add(empno);
11 }
12 if (table.Rows.Count > 0)
13 {
14     this.comboBox1.SelectedIndex = 0;
15 }
16 
17 //初始化教师姓名
18 string sql_name = "select EmpName from EMPLOYEE";
19 table.Clear();
20 table = SqlManage.SelectTable(sql_name);
21 string empname;
22 foreach (DataRow row in table.Rows)
23 {
24     empname = row["EmpName"].ToString();
25     this.comboBox2.Items.Add(empname);
26 }
27 if (table.Rows.Count > 0)
28 {
29     this.comboBox2.SelectedIndex = 0;
30 }

 

 1 string sql = "";
 2 if (this.radioButton1.Checked)
 3 {
 4     sql = string.Format("select CmpName as 公司名,Salary as 工资 from EMPLOYEE,COMPANY,WORKS where EMPLOYEE.EmpNo=WORKS.EmpNo and COMPANY.CmpNo=WORKS.CmpNo and WORKS.EmpNo = '{0}'",
 5         this.comboBox1.Text);
 6 }
 7 else if (this.radioButton2.Checked)
 8 {
 9     sql = string.Format("select CmpName as 公司名,Salary as 工资 from EMPLOYEE,COMPANY,WORKS where EMPLOYEE.EmpNo=WORKS.EmpNo and COMPANY.CmpNo=WORKS.CmpNo and EmpName = '{0}'",
10         this.comboBox2.Text);
11 }
12 DataTable table = SqlManage.SelectTable(sql);
13 if (table.Rows.Count > 0)
14 {
15     this.dataGridView1.DataSource = table;
16 }
17 else
18 {
19     MessageBox.Show("没有相关内容");
20 }

 

having子句查询:

1 SqlManage.SetDataGridView(this.dataGridView1);
2 string sql = "select EmpName as 员工姓名, CmpName as 公司名 from EMPLOYEE,COMPANY,WORKS where EMPLOYEE.EmpNo=WORKS.EmpNo and COMPANY.CmpNo=WORKS.CmpNo and" +
3     " WORKS.EmpNo in (select EmpNo from WORKS group by EmpNo having count(*)>1)";
4 DataTable table = SqlManage.SelectTable(sql);
5 this.dataGridView1.DataSource = table;

 

readme.txt

1.附加数据库文件test.mdf
3 2.backinfo.bak为数据库的备份文件
3.建表.sql为所有数据库操作的sql语句
4.Info.doc为说明文件
5.连接字符串:"Server=<你的计算机全名>;Database=test;Uid=sa;Pwd=123456;"

 

Info.doc

数据库用户名:sa;
登录密码:123456;
连接字符串:Server=AN2XOC6HS03APKT;Database=tmp2019;Uid=sa;Pwd=123456;

 

你可能感兴趣的:(.net窗体程序要点(速记))