标签:
数据库,不得不说真是个折磨人的东西。为了学习这个,不得不说,小编出了多少脑油,掉了多少头发,哎,说出来都是眼泪,小心脏也是拔凉拔凉的。我想想,肯定也会有许多的编程员像我这样(初学者),所以小编要把自己的全部技巧教给大家。
Lady and Gentle,男的女的,都听好了,我要使出浑身解数了,轰隆!!!!
第一步:既然连接数据库,我们当然要有一个非常绚丽的登录界面。在这里不多做解释,相信大家肯定都会。所以就着重讲解数据库了。
private void btnLogin_Click(object sender, EventArgs e) { //验证用户名密码是否和数据库中匹配 //ADO.NET string str = "Data source=.;initial catalog=MySchool;uid=sa;pwd=6375196"; SqlConnection con = new SqlConnection(str); string sql = "select count(1) from student where studentname=‘" + txtName.Text + "‘ and loginpwd=‘" + txtPwd.Text + "‘"; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = Convert.ToInt32(cmd.ExecuteScalar()); if (count > 0) { //MessageBox.Show("登陆成功"); //如何在登陆成功后,显示FrmMain界面 //隐藏自己(不为任何人所铭记),显示别人(鲜花和掌声),伟大 this.Hide(); FrmMain frm = new FrmMain(); frm.name = txtName.Text; frm.Show(); } } catch (Exception) { MessageBox.Show("脑子有病");//提示用户信息 } finally { con.Close(); } } //Load窗体在被用户肉眼看到前就被执行了 private void FrmLogin_Load(object sender, EventArgs e) { }
以上就是登录,也就是连接。这样就是登录到数据库。
插播一条信息!!! 每个用户使用每一个软件肯定都会喜欢一个界面,就是欢迎界面:
private void FrmMain_Load(object sender, EventArgs e) { this.IsMdiContainer = true; this.Text = this.Text + " 欢迎您" + name; }
第二步:我们可以浏览到数据库的内容,然后可以为数据库添加数据咯!
private void btnEdit_Click(object sender, EventArgs e) { //添加学生 string pwd = txtPwd.Text;//密码 string stuname = txtName.Text;//姓名 //获取性别 string stugender = string.Empty; if (rbtnMale.Checked) { stugender = "1"; } else { stugender = "0"; } //如何给下拉框绑定数据 int gid = GetIdByName(); //电话 string stuPhone = txtPhone.Text; //地址 string stuAddress = txtAddress.Text; //日期 DateTime dt= dpBirthday.Value; string stuEmail = txtEmail.Text; //StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email, MyTT string sql = "insert into student values(‘"+pwd+"‘,‘"+stuname+"‘,"+stugender+","+gid+",‘"+stuPhone+"‘,‘"+stuAddress+"‘,‘"+dt+"‘,‘"+stuEmail+"‘)"; string str = "Data source=.;initial catalog=MySchool;uid=sa;pwd=6375196"; SqlConnection con=new SqlConnection(str); SqlCommand cmd=new SqlCommand(sql,con); con.Open(); int count = cmd.ExecuteNonQuery(); if (count>0) { MessageBox.Show("成功!"); } con.Close(); } //该方法将年级名称转换成年级编号 public int GetIdByName() { string str = "Data source=.;initial catalog=MySchool;uid=sa;pwd=6375196"; SqlConnection con = new SqlConnection(str); string sql = "select gradeid from grade where gradename=‘"+cboGrade.Text+"‘"; SqlCommand cmd = new SqlCommand(sql, con); int gid = 0; try { con.Open(); gid =Convert.ToInt32(cmd.ExecuteScalar()); } catch (Exception) { MessageBox.Show("网络异常"); } finally { con.Close(); } return gid; } private void FrmAddStudent_Load(object sender, EventArgs e) { //拿到所有年级名称 string str = "Data source=.;initial catalog=MySchool;uid=sa;pwd=6375196"; SqlConnection con = new SqlConnection(str); string sql = "select gradename from grade"; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); SqlDataReader dr= cmd.ExecuteReader(); if (dr!=null) { if (dr.HasRows) { while (dr.Read()) { string gname = dr["gradename"].ToString(); //每读到一个年级名称,我就将"S1",添加到下拉框 cboGrade.Items.Add(gname); } } } } catch (Exception ex) { MessageBox.Show("网络异常"); } finally { con.Close(); } }
第三步:我们可以添加学生到数据库就可以搜寻某一条数据拉~
public FrmSearchByName() { InitializeComponent(); } //加载DB中的数据到ListView中 public void LoadDataFromDBToListView(string sql) { string str = "data source=.;initial catalog=MySchool;uid=sa;pwd=6375196;"; SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand(sql, con); try { con.Open();//2分 SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { //每从DB中读取出一条数据,需要形成一个ListViewItem对象,代表的是ListView中的一行 int stuNo = Convert.ToInt32(dr["studentno"]); //第二列 stuname string stuName = Convert.ToString(dr["studentname"]); //第三列 性别 string stugender = Convert.ToString(dr["gender"]); //年级名称 string gname = Convert.ToString(dr["gradename"]); ListViewItem lvItem = new ListViewItem(stuNo.ToString()); lvItem.SubItems.Add(stuName); lvItem.SubItems.Add(stugender); lvItem.SubItems.Add(gname); //让lvItem和ListView关联 lvStuList.Items.Add(lvItem); } dr.Close();//-2分 } } } catch (Exception) { throw; } finally { con.Close();//2分 } } private void FrmSearchByName_Load(object sender, EventArgs e) { string sql = @"select Studentno,Studentname,Gender,gradename from student,grade where student.gradeid=grade.gradeid "; LoadDataFromDBToListView(sql); } private void btnSearch_Click(object sender, EventArgs e) { //查询数据前,清空LIstView中的数据 lvStuList.Items.Clear(); string sql = @"select Studentno,Studentname,Gender,gradename from student,grade where student.gradeid=grade.gradeid and studentname like ‘%"+txtStudentName.Text+"%‘ "; LoadDataFromDBToListView(sql); } } }
插播一条!!! 部分的软件会有要求输入生日,什么日期什么的,那么怎么获取呢?
//获取日历控件的value值 MessageBox.Show(dateTimePicker1.Value.ToShortDateString()); if (rboMale.Checked) { //给文本框赋值为男 txtResult.Text = "男"; }else if (rboFeMale.Checked) { //女 txtResult.Text = "女"; } else { //春哥,开玩笑了 txtResult.Text = "春哥"; }
学习到这里部分的功能就介绍完了,可能大家看完了还会迷迷糊糊,这就对了,好东西都要参悟参悟在参悟的,怎么会被一下就明白,大家要多思考哦
标签:
原文地址:http://www.cnblogs.com/ay-nzz/p/ay_wzz.html