大连外国语大学软件学院c# winform互联网科技

大外大三上WinForm考试,瞬间GET新技能~

2016-12-30  本文已影响3752人  Vantiboolean

文章介绍

2016-12-23 更新
本人在学校近两年的讲课经验,如今已经毕业了,将之前课程的内容分享出来,也包括我自己一些对该课程的理解内容,和一些想法在其中,怎么样才可以更快的上手winform开发,在考试中取得好成绩。
虽然现在已经很少有人在做WinForm开发了,但是VS工具用来做开发还是蛮不错,这里分享出来简单的WinForm开发,已经连接SQLServer数据库实现数据动态显示。

2018-1-1更新
记14级WinForm考试,本文章覆盖了期末考试90%的内容。再一次在新一年级考试中,同学们可以继续去阅读使用。
修改内容,更改一些排版。

作者介绍

李泓铮 软件学院13级 联系微信18512489412

控件介绍

我们课程学习的WinForm,是可以这样子做出一个Windows窗体应用程序,WinForm编程是结合于C#和SQLServer的内容,我们的终止目标是做出一个具有登录功能 ,菜单选项入口,数据查询,数据添加,数据删除,数据修改的。


窗体样式
公共控件介绍
公共控件
容器和菜单
数据控件

本文通过7个练习代码来讲述考试的整体内容,从简到繁琐在最小的时间内上手winform

练习1 登陆验证(绑定数据库)

案例内容:
这里我们制作一个登陆界面,然后我们输入用户名密码,点击登录,这里我们可以从在数据库中读取数据库中username和password,来判断我们输入的用户名和密码是否正确,如果正确则提示登录成功,不正确提示登录失败。

练习1 登录界面

从控件中拖出控件,控件上右键属性,在属性面板上修改控件的显示文本信息。


图片为我们添加的文本框的名字

既然我们制作出了窗口,在我们输入了用户名和密码之后,我们需要点击登录来完成登录操作。
所以我们的登录验证代码应该都存在于登录按钮的后面,这里我们只需要双击登录按钮,就可以跳转到后台cs文件,自动创建登录后台验证代码。

SqlConnection myConn = new SqlConnection();
//新建Sql连接
myConn.ConnectionString ="server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;";
//创建连接字符串
string sql = "select count(*) from tb_user where username='"+textBox1.Text.Trim()+"' and password ='"+textBox2.Text.Trim()+"'";
//这里textBox1的名字是我们拖拽进去的控件的名字,如上图
//新建Sql查询
SqlCommand cmd = new SqlCommand(sql, myConn);
//封装sql命令
myConn.Open();
//打开连接
int res=Convert.ToInt32(cmd.ExecuteScalar());
//执行sql语句
myConn.Close();
//关闭连接
if (res > 0)
{
  MessageBox.Show("登录成功", "提示");
}
else
{
  MessageBox.Show("登录失败", "提示");
}

第一种写法
这里上面用到了cmd.ExecuteScalar()因为我们这里是从数据库中取一个值出来,所有需要使用cmd.ExecuteScalar()来执行操作来取一个值

  Application.Exit();

添加引用
在代码的最上方添加
using System.Data.SqlClient;

练习2 使用DataGridView读取student表信息,并附加添加删除按钮,表值修改功能。

我们从工具箱中拖出GridView工具,然后使用编辑列,将我们的表中对应的名字和表中对应的列名将数据表和DataGridView绑定

GridView读取student表信息 如图点击编辑列,添加列名

我们创建好了DataGridView的框架,但是我们需要绑定上数据,怎么绑定呢?一种解决方法就是在我们打开窗体的时候加载我们的数据,这里我们需要创建一个窗体加载的函数,双击标题栏,就可以跳转到后台代码,在这里我们添加进入加载代码

private void Form1_Load(object sender, EventArgs e)
{
  SqlConnection myConn = new SqlConnection();
  myConn.ConnectionString = "server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;";
  //定义数据库连接字符串
  string sql = "select * from student";
  //定义sql语句,查询出student表中所有数据
  SqlCommand cmd = new SqlCommand(sql, myConn);
  SqlDataAdapter sda = new SqlDataAdapter();
  //使用SqlDataAdapter执行sql操作
  sda.SelectCommand = cmd;
  DataSet ds = new DataSet();
  //使用DataSet绑定返回数据信息
  myConn.Open();
  sda.Fill(ds, "student");
  //填充DataSet
  myConn.Close();

  dataGridView1.DataSource = ds.Tables["student"];
  //给GridView填充数据
  dataGridView1.ClearSelection();
}

练习3 添加按钮 添加学生信息

好了,我们上面完成了,DataGridView的读取操作,下面我们来添加一个添加按钮,用于我们添加学生信息,

添加两个按钮 添加学生信息窗体
string sno = textBox1.Text.Trim();
string sname = textBox2.Text.Trim();
string age = textBox3.Text.Trim();
string dept = textBox4.Text.Trim();
string sex;
if (radioButton1.Checked)
  sex = "男";
else
  sex = "女";

SqlConnection myConn = new SqlConnection();
//
myConn.ConnectionString = "server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;";
string sql = "insert into student values ('" + sno + "','" + sname + "','" + sex + "','" + age + "','" + dept + "')";
SqlCommand cmd = new SqlCommand(sql, myConn);

myConn.Open();
int res = cmd.ExecuteNonQuery();
myConn.Close();
if (res > 0)
    MessageBox.Show("学生信息添加成功", "提示");
else
    MessageBox.Show("学生信息添加失败", "提示");

测试
我们点击添加按钮后,再次查询即可看到添加后信息

添加后的效果

练习4 删除按钮 删除学生信息**IMPORTANT

这里我们来讲解一下删除,我们在没有选择某一项的时候,我们删除按钮是灰色的不能点击,所以当我们点击上面DataGridView中的某一列后会变成可点击的按钮,点击后即可删除!如下图所示:


删除按钮不可选 点击某一列后按钮可选
button2.Enabled = true;
sno = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
private void button2_Click(object sender, EventArgs e)
{
  if (MessageBox.Show("确实要删除吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
  {
    SqlConnection myConn = new SqlConnection();
    myConn.ConnectionString = "server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;";
    string sql = "select count(*) from sc where sno='" + sno + "'";
    SqlCommand cmd = new SqlCommand(sql, myConn);
    //连接数据库
    myConn.Open();
    int res = Convert.ToInt32(cmd.ExecuteScalar());
    myConn.Close();
    if (res > 0)
    {
        MessageBox.Show("该同学已经选课,不能删除", "提示");
    }
    else
    {
      cmd.CommandText = "delete from student where sno='" + sno + "'";
      myConn.Open();
      int result = cmd.ExecuteNonQuery();
      myConn.Close();
      if (result == 1)
          MessageBox.Show("删除成功!", "提示");
      else
          MessageBox.Show("删除失败!", "提示");
          dataGridView1.ClearSelection();
    }
  }
  button2.Enabled = false;
  dataGridView1.ClearSelection();
}

测试
删除效果

删除按钮

练习5 双击DataGridView修改学生属性

新建立的修改窗体
Form3 asf = new Form3(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
asf.Show();

public Form3(string _sno)
{
  button_add.Text = "更新";
  SqlConnection myConn = new SqlConnection();
  myConn.ConnectionString = "server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;";
  //连接数据库
  string sql = "select * from student where sno='"+sno+"'";
  //设置SQL语句
  SqlCommand cmd = new SqlCommand(sql, myConn);
  myConn.Open();
  SqlDataReader sdr = cmd.ExecuteReader();
  //读取数据库
  while (sdr.Read())
  {
    textBox1.Text = sno;
    textBox2.Enabled = false;
    if (sdr["sex"].ToString().Trim() == "男")
        radioButton1.Checked = true;
    else
        radioButton2.Checked = true;
    textBox3.Text = sdr["sname"].ToString().Trim();
    textBox4.Text = sdr["age"].ToString();
    textBox5.Text = sdr["dept"].ToString();            
  }
  //修改赋值
  sdr.Close();
  myConn.Close();
  //关闭数据库连接
}

练习6 DataGridView模糊查找

模糊查询界面

我们使用模糊查找的SQL语句
select * from student where sname like '%" + textBox1.Text.Trim() + "%'"
上面的sql语句就是模糊查找的主要语句存在,条件在where中like%%,百分号中间的是关键词,两边就是模糊查找。

下面的代码全部写入,搜索button按钮当中

SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = "server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;";
string sql = "select * from student where sname like '%" + textBox1.Text.Trim() + "%'";
SqlCommand cmd = new SqlCommand(sql, myConn);

SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
myConn.Open();
sda.Fill(ds, "student");
myConn.Close();

dataGridView1.DataSource = ds.Tables["student"];
dataGridView1.ClearSelection();

模糊查找效果

模糊查询结果

练习7 院系实战训练

我们之前所说到的都是关乎学生表的增删改查,这里我们添加一个实战,使用院系表进行增删改查,如图构造:在comboBox中下拉选择院系后,点击查询系别人数,在后面的label上显示人数,再下一行使用textBox输入院系名字,点击查询,在后面的label上显示院系的人数。


院系查询样式

第一个查询按钮(ComboBox后面的查询按钮)

SqlConnection myConn = new SqlConnection("server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;integrated security=sspi");
SqlCommand cmd = new SqlCommand("select count(*) from student where dept='" + cbDept.Text + "'", myConn);
myConn.Open();
int i = Convert.ToInt32(cmd.ExecuteScalar());
myConn.Close();
lblDeptCount.Text = cbDept.Text + "学生人数为:" + i.ToString() + "人";

第二个查询按钮(textBox后面的查询按钮)


SqlConnection myConn = new SqlConnection("server=.\\sqlexpress;database=demo;uid=sa;pwd=123456;integrated security=sspi");
SqlCommand cmd = new SqlCommand("select count(*) from student where dept='" + txtDept.Text.Trim() + "'", myConn);
myConn.Open();
int i = Convert.ToInt32(cmd.ExecuteScalar());
myConn.Close();
lblDeptCount.Text = txtDept.Text.Trim() + "学生人数为:" + i.ToString() + "人";

查询每个系别人数(两组按钮后的查询系别人数按钮)

lblTotalCount.Text="";
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = "server=.\\sqlexpress;database=demo;integrated security=yes;uid=sa;pwd=123456;";
string sql = "select dept,count(dept) from student group by dept";
SqlCommand cmd = new SqlCommand(sql, myConn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "stu");
foreach (DataRow dr in ds.Tables["stu"].Rows)
{
    lblTotalCount.Text += dr[0].ToString() + " " + dr[1].ToString()+"\n";
}

二维码

联系作者


大外大三上WinForm考试,瞬间GET新技能~
上一篇下一篇

猜你喜欢

热点阅读