C#对数据库的操作

Windowsform窗体应用:

实验二 学生信息管理界面的设计

实验目的

1、熟悉NET程序设计方法

2、熟悉Windows Form控件

3、熟悉NET程序调试方法

实验内容

1、在第一次实验基础上,添加学生管理模块,包括增加学生,修改学生,删除学生三个菜单选项及界面。

2、增加学生界面需要姓名(文本框)、学号(文本框)、性别(单选框)、入学日期(时间日期选择控件)、手机号(文本框)、学院(文本框)、专业(文本框)、班级(下拉框)、家庭地址(文本框)、提交按钮,提交按钮实现将界面内录入内容存到一个列表内。

3、修改学生界面需要除增加学生界面控件外、增加一个学号录入框、一个查询按钮,通过查询,找到列表内学生信息,填入到对应框,同时将修改信息存入学生列表内。

4、删除学生界面需要一个学号(文本框)、一个删除按钮,实现将学生列表内内指定学号学生从学生列表移除。

https://wenku.baidu.com/view/8fc6583a580216fc700afd4d.html

http://soft.chinabyte.com/database/414/12425914.shtml


页面之间跳转:

private void button1_Click ( object sender , EventArgs e )
        {
            Form2 f2 = new Form2 ();
            f2 . Show ();
            Hide () //form2打开之后,隐藏当面页面;
        }
ssss hw
showDialog() 作用是:打开子页面,父页面保留,且只能在子页面进行操作!
private void 增加学生 ToolStripMenuItem_Click ( object sender , EventArgs e )
        {
            Form6 f6 = new Form6 ();
            f6 . ShowDialog ();
        }
this.Close();的作用是:关闭当前页面的作用!
 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  this.Close();
  }




  1. 增加学生入数据库的代码:(向数据库中增加元素  insert into 表名()values('"+传值+"'))

string str_sql = "insert INTOnew_table( 学号 , 密码 , 姓名 , 性别 , 入学日期 , 手机号 , 学院 , 专业 , 班级 , 家庭住址 ) values(" + 学号 + ",'" + 密码 + "','" + 姓名 + "','" + 性别 + "','" + 入学日期 + "','" + 手机号 + "','" + 学院 + "','" + 专业 + "','" + 班级 + "','" + Convert . ToString ( textBox8 . Text ) + "')" ;

记住:除主键外,要想将 TextBox Text 里的值(包括 中英文)都存到数据库中一定要加单引号 + 双引号‘“   ”’(且符号为英文输入法)

完整代码如下:

 private void button1_Click(object sender, EventArgs e)   //一个按钮
  {
  //string userid = textBox1.Text;//用户名
  string 学号 = textBox2.Text;//学号
  string 密码 = textBox9.Text; //密码
  string 姓名 = textBox1.Text;//学号
  string 性别 = textBox10.Text; //密码
  string 入学日期 = textBox3.Text;//学号
  string 手机号 = textBox4.Text; //密码
  string 学院 = textBox5.Text;//学号
  string 专业 = textBox6.Text; //密码
  string 班级 = textBox7.Text;//学号
  string 家庭住址= textBox8.Text; //密码

 
  string conn = "server=127.0.0.1;user id=root;password=123456;database=wlbc;";  //连接数据库

  MySqlConnection connection = new MySqlConnection(conn);//创建数据库连接
  try
  {
  connection.Open();//打开连接

  MySqlCommand comm = new MySqlCommand();//创建数据库命令

  comm.Connection = connection;//设置命令的数据库连接
  string str_sql = "insert INTO new_table(学号,密码,姓名,性别,入学日期,手机号,学院,专业,班级,家庭住址) values(" + 学号 + ",'" + 密码 + "','" + 姓名 + "','" + 性别 + "','" + 入学日期 + "','" + 手机号 + "','" + 学院 + "','" + 专业 + "','" + 班级 + "','" + Convert.ToString(textBox8.Text) + "')";
  //string str_sql ="insert INTO new_table(学号,密码,姓名,性别,入学日期,手机号,学院,专业,班级,家庭住址) values(" + 学号 + "," + 密码 + "," + 姓名 + "," + 性别 + "," + 入学日期 + "," + 手机号 + "," + 学院 + "," + 专业 + "," + 班级 + "," + 家庭住址 + ")";

  comm.CommandText = str_sql;//设置命令文本
  comm.ExecuteNonQuery();
 
  MessageBox.Show("增加学生信息成功");

  }
  catch (MySqlException ex)
  {
  MessageBox.Show(ex.Message);
  }
  finally
  {
  connection.Close();//关闭数据库连接
  }

  }

2.查询数据:

private void button1_Click(object sender, EventArgs e)  //一个按钮
  {
string userid = textBox3.Text;//用户名
  string conn = "server=127.0.0.1;user id=root;password=123456;database=wlbc";   //连接数据库
  //Data Source 服务器名 ;Initial Catalog 数据库名; User ID 用户名; pwd 密码
  MySqlConnection connection = new MySqlConnection(conn);//创建数据库连接
  try
  {
  connection.Open();//打开连接
  MySqlCommand comm = new MySqlCommand();//创建数据库命令

  comm.Connection = connection;//设置命令的数据库连接
                
  string str_sql = "select count(*) from new_table where 学号='" + userid + "' ";//执行的命令Sql    查找指令  select count(*)from 数据表名 where 要查找的目标项名='"+输入的配对项数值+"'
 
  comm.CommandText = str_sql;//设置命令文本

              

  int count = Convert.ToInt32(comm.ExecuteScalar());//执行命令

  if (count > 0)
  {
  MySqlDataReader getValueReader = comm.ExecuteReader();
  //用户名等于输入的用户名,密码等于输入的密码的记录条数大于0,表示登陆成功,显示主界面

  string s = this.textBox3.Text;
                    
  Form9 f9 = new Form9(s)
  f9.ShowDialog();

  }

  else
  {
  MessageBox.Show("学号输入错误或者没有该学生信息!");
  }
  }

  catch (MySqlException ex)
  {
  MessageBox.Show(ex.Message); //抛出异常
  }

  finally
  {

  connection.Close();//关闭数据库连接

  }
  }
3.增加数据(写入insert into 表名
private void button2_Click(object sender, EventArgs e)
  {
  string userid = textBox3.Text;//用户名
  //string pwd = textBox2.Text; //密码
  string conn = "server=127.0.0.1;user id=root;password=123456;database=wlbc"; //连接数据库
  //Data Source 服务器名 ;Initial Catalog 数据库名; User ID 用户名; pwd 密码
  MySqlConnection connection = new MySqlConnection(conn);//创建数据库连接
  try
  {
  connection.Open();//打开连接

  MySqlCommand comm = new MySqlCommand();//创建数据库命令

  comm.Connection = connection;//设置命令的数据库连接

  //string str_sql = "select count(*) from new_table where 学号='" + userid + "' ";//执行的命令Sql
  string str_sql = "insert INTO new_table(学号) values(" + userid + ")";
  comm.CommandText = str_sql;//设置命令文本
  comm.ExecuteNonQuery();
  MessageBox.Show("学号录入成功!");
 

  }
  catch (MySqlException ex)
  {
  MessageBox.Show("录入失败或者该学号已存在!" + ex.Message);
  }
  finally
  {
  connection.Close();//关闭数据库连接
  }
  }
  }
}
4.删除操作:(删除 delete from表名where 将要删除的数据所在的行名字='"+删除数据的值+"'
private void button1_Click(object sender, EventArgs e)
  {
  string 学号 = textBox3.Text;//学号
  string conn = "server=127.0.0.1;user id=root;password=123456;database=wlbc";//连接数据库
  //Data Source 服务器名 ;Initial Catalog 数据库名; User ID 用户名; pwd 密码
  MySqlConnection connection = new MySqlConnection(conn);//创建数据库连接
  try
  {
  connection.Open();//打开连接

  MySqlCommand comm = new MySqlCommand();//创建数据库命令

  comm.Connection = connection;//设置命令的数据库连接

  string str_sql = "delete from new_table where 学号='" + 学号 + "'";//执行的命令Sql
  comm.CommandText = str_sql;//设置命令文本
  comm.ExecuteNonQuery();

  MessageBox.Show("删除学生信息成功");

  }
  catch (MySqlException ex)
  {
  MessageBox.Show(ex.Message);
  }
  finally
  {
  connection.Close();//关闭数据库连接
  }

  }
  }
}












  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值