学期快结束了,学校要求计算机专业的要做个课程设计;本以为做个课本上有的学生信息管理系统会节省下时间。
尼玛做到数据库那块发现书上的例题都是问题了,忒坑了;老师忙于其他同学的问题也没时间解决。
大家都是从学生时代过来的帮忙解决下吧
代码在这里,删除数据时老是出错;请大神指点
全部代码在这里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 );
}
}
}
}