万恶的课程设计。。呜呜。。求大神改一下学生信息管理系统

小方同志 2014-05-29 10:27:18
学期快结束了,学校要求计算机专业的要做个课程设计;本以为做个课本上有的学生信息管理系统会节省下时间。
尼玛做到数据库那块发现书上的例题都是问题了,忒坑了;老师忙于其他同学的问题也没时间解决。
大家都是从学生时代过来的帮忙解决下吧
代码在这里,删除数据时老是出错;请大神指点
全部代码在这里http://yun.baidu.com/share/link?shareid=1137622446&uk=489028261


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 MySchool
{
public partial class StudentFrm : Form
{
int current = 1;
string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;Integrated Security=True";
public StudentFrm()
{
InitializeComponent();
current = 1;
ShowCurrentStudent();
}
private void ShowCurrentStudent()
{
string sql = string.Format("SELECT * FROM StudentMsg WHERE StudentNo='{0}'", current);
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
textBox4.Text = Convert.ToString(reader.GetInt32(0));
textBox1.Text = reader.GetString(1);
string sex = reader.GetString(2);
if (sex == "男")
radioButton1.Checked = true;
else
radioButton2.Checked = true;
dateTimePicker1.Value = reader.GetDateTime(3);
textBox2.Text = reader.GetString(4);
textBox3.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.Asterisk);
reader.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
current--;
ShowCurrentStudent();
}

private void button2_Click(object sender, EventArgs e)
{
current++;
ShowCurrentStudent();
}

private void button3_Click(object sender, EventArgs e)
{
string sex = "";
if (radioButton1.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 StudentNo='{0}', StudentName='{1}',Sex='{2}',Birthday='{3}',Department='{4}',Speciality='{5}',Hobby='{6}'WHERE StudentNo='{7}'", textBox4.Text, textBox1.Text, sex, dateTimePicker1.Value, textBox2.Text, textBox3.Text, hobby, current);
string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=EX10;Integrated Security=true";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
int n = comm.ExecuteNonQuery();
if (n > 0)
MessageBox.Show("数据更新操作失败,请检查数据格式!", "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

private void button4_Click(object sender, EventArgs e)
{
// int t=Convert .ToInt32( textBox4.Text);
string sql = string.Format("delete from StudentMsg WHERE StudentNo={0}",current );
// string sql = string.Format("DELETE FROM StudentMsg");
// string sql = "delete from StudentMsg WHERE StudentNo=" + t.ToString() + "'";
string connString = @"Data Source =.\SQLEXPRESS;Initial Catalog=MySchool;Integrated Security=True";
try
{
SqlConnection conn = new SqlConnection(connString);


// SqlParameter pa = new SqlParameter("@id", SqlDbType.Int);
// pa.Value = t;
conn.Open();
SqlCommand comm = new SqlCommand(sql,conn);
// comm.Parameters.Add(pa);
// comm.CommandText = "delete from StudentMsg where StudentNo=" + Convert.ToInt32(textBox4.Text);
// comm.Connection = conn;
int n = comm.ExecuteNonQuery();
// if (n == 1)
// {
// MessageBox.Show("删除成功!", "注意", MessageBoxButtons.OK);
// }
if (n <= 0)
MessageBox.Show("删除失败,请与管理员联系!", "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
current--;
ShowCurrentStudent();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"",MessageBoxButtons.OK );
}
}
}
}
...全文
175 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
小方同志 2015-11-29
  • 打赏
  • 举报
回复
exception92 2014-05-29
  • 打赏
  • 举报
回复
你不知道 天下没有免费的午餐么。。。。
  • 打赏
  • 举报
回复
不想看 .
tempt_ss 2014-05-29
  • 打赏
  • 举报
回复
我要告诉你们老师去。
小恒丶 2014-05-29
  • 打赏
  • 举报
回复
你在这里这么屌 你老师知道吗
wind_cloud2011 2014-05-29
  • 打赏
  • 举报
回复
这是删除数据的SQL语句, StudentNo在数据库是int,还是char? 你可以将这个语句在数据库查询一下,看返回什么, "delete from StudentMsg where StudentNo=" + Convert.ToInt32(textBox4.Text)
wind_cloud2011 2014-05-29
  • 打赏
  • 举报
回复
你删除时有什么提示?
mjp1234airen4385 2014-05-29
  • 打赏
  • 举报
回复
删除出错,那就是删除的sql语句有问题了。 好好找问题吧。
mjp1234airen4385 2014-05-29
  • 打赏
  • 举报
回复
这个代码太有价值了,收藏!
bwangel 2014-05-29
  • 打赏
  • 举报
回复
这还是计算机专业? 我还是非计算机专业的都看不下去了。
EdsionWang 2014-05-29
  • 打赏
  • 举报
回复
加断点,看看current值在删除时是多少。 删除的sql语句StudentNo = ‘{0}’ 要加单引号吧? 贴那么多无用代码,也不说什么错。谁有空给你看那么仔细。。。。。。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧