using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 学生成绩管理系统
{
public partial class StudentFrm : Form
{
private int current = 1;
string connString = @"Data Source=Win-01412261307;Initial Catalog=MySchool;Integrated Security=True";
private void ShowCurrentStudent(){
string sql=String.Format("SELECT * FROM StudentMsg WHERE StudentNo='{0}'",current);//sql语句
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();//打开数据库连接
SqlCommand comm = new SqlCommand(sql, conn);//创建command对象
SqlDataReader reader = comm.ExecuteReader();//执行“添加”命令,返回值为更新的行数
if (reader.Read())
{
txtName.Text = reader.GetString(1);//显示学生姓名
string sex = reader.GetString(2);//显示学生性别
if (sex == "男")
rdoMale.Checked = true;
else
rdoFemale.Checked = true;
dtBirthday.Value = reader.GetDateTime(3);//显示学生出生年月
txtDept.Text = reader.GetString(4);//显示所在院系
txtSpec.Text=reader.GetString(5);//显示专业
string[] hobbies = new string[6];
hobbies = reader.GetString(6).Split('、');
checkBox1.Checked = false; checkBox2.Checked = false;
checkBox3.Checked = false; checkBox4.Checked = false;
checkBox5.Checked = false; checkBox6.Checked = false;
foreach (string s in hobbies)//显示爱好
{
switch (s)
{
case "阅读": checkBox1.Checked = true; break;
case "体育": checkBox2.Checked = true; break;
case "音乐": checkBox3.Checked = true; break;
case "上网": checkBox4.Checked = true; break;
case "旅行": checkBox5.Checked = true; break;
default: checkBox6.Checked = true; break;
}
}
}
else{
MessageBox.Show("前面或后面已经无数据了", "没有数据", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
reader.Close();
}
}
public StudentFrm()
{
InitializeComponent();
current = 1;//current用来保存显示学生的序号
ShowCurrentStudent();//如果第一个学生存在,则显示
}
private void btnPrevious_Click(object sender, EventArgs e)
{
current--;
ShowCurrentStudent();
}
private void btnNext_Click(object sender, EventArgs e)
{
current++;
ShowCurrentStudent();
}
private void btnEdit_Click(object sender, EventArgs e)
{
string sex = "";
if (rdoMale.Checked)
sex = "男";
else
sex ="女";
string hobby = "";
if (checkBox1.Checked) hobby += checkBox1.Text;
if (checkBox2.Checked) hobby +="、"+ checkBox2.Text;
if (checkBox3.Checked) hobby += "、" + checkBox3.Text;
if (checkBox4.Checked) hobby += "、" + checkBox4.Text;
if (checkBox5.Checked) hobby += "、" + checkBox5.Text;
if (checkBox6.Checked) hobby += "、" + checkBox6.Text;
string sql = String.Format("UPDATE StudentMsg SET StudentName='{0}',Sex='{1}',Birthday='{2:yy-MM-dd}',Department='{3}',Speciality='{4}',Hobby='{5}' WHERE StudentNo='{6}'", txtName.Text, sex, dtBirthday.Value, txtDept.Text, txtSpec.Text, hobby, current);//sql语句
string connString = @"Data Source=Win-01412261307;Initial Catalog=MySchool;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();//打开数据库连接
SqlCommand comm = new SqlCommand(sql, conn);//创建command对象
int n = comm.ExecuteNonQuery();//执行“更新”命令,返回值为更新的行数
if (n <= 0)
{
MessageBox.Show("数据更新操作失败,请检查数据格式!", "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("数据更新操作成功!", "操作数据库成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
string sql = String.Format("DELETE FROM StudentMsg WHERE StudentNo='{0}'",current);//sql语句
string connString = @"Data Source=Win-01412261307;Initial Catalog=MySchool;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();//打开数据库连接
SqlCommand comm = new SqlCommand(sql, conn);//创建command对象
int n = comm.ExecuteNonQuery();//执行“添加”命令,返回值为更新的行数
if (n <= 0)
{
MessageBox.Show("删除失败,请与管理员联系!", "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else //删除当前记录之后,刷新对话框以显示上一条记录
{
current--;
ShowCurrentStudent();
}
}
}
}
}
下面椒数据表肯界面,大神们帮小弟找找错误吧。(没报错,就是点击上一个的时候名字老是不变,id未1时明明有数据)
------解决思路----------------------
SqlDataReader是只能往下读,不能往回读的。建议使用SqlDataAdapter填充到DataSet
------解决思路----------------------
断点跟啊,看到底代码怎么走的,变量值到底是多少,读数据库成功了没有,走进哪个分支了
------解决思路----------------------
建议把你的代码给重新封装一下吧,你看乱的。
如2楼,把数据读取到datatable,使用rows属性显示数据属性。