📄 pointoperate.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class pointoperate : Form
{
private DataSet ds = new DataSet();
private SqlDataAdapter da = new SqlDataAdapter();
public pointoperate()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox7.Text != "")
{
dataGridView1.DataSource = null;
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "select * from 成绩表 where 学号='" + Convert.ToInt32(textBox7.Text.Trim()) + "'";
mycommand.Connection = myconnection;
da.SelectCommand = mycommand;
da.SelectCommand.Connection = myconnection;
da.Fill(ds, "成绩表");
if (ds.Tables["成绩表"].Rows.Count < 1)
{
MessageBox.Show("不存在该学生!");
}
else
{
dataGridView1.DataSource = ds.Tables["成绩表"];
button5.Enabled = true;
button3.Enabled = false;
}
textBox7.Text = "";
}
catch (FormatException fe)
{
MessageBox.Show("输入错误" + fe.Message);
}
}
else
{
MessageBox.Show("请输入需要修改的学生的学号!");
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "" ; textBox3.Text = "";textBox2.Text= "";
textBox4.Text = "";
}
private void button4_Click(object sender, EventArgs e)
{
textBox7.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != ""&&
textBox4.Text != "" )
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "select * from 成绩表";
mycommand.Connection = myconnection;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = mycommand;
da.SelectCommand.Connection = myconnection;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "成绩表");
DataTable dt = ds.Tables["成绩表"];
DataRow dr = dt.NewRow();
//dr["编号"] = ds.Tables["成绩表"].Rows.Count + 1;
dr["学号"] = Convert.ToInt64(textBox1.Text);
dr["课程名"] = textBox2.Text;
if (Convert.ToInt32(textBox3.Text) >= 0 && Convert.ToInt32(textBox3.Text) <= 100)
dr["分数"] = Convert.ToInt32(textBox3.Text);
else
MessageBox.Show("分数应该为0-100之间!");
dr["学分"] = Convert.ToInt32(textBox4.Text);
dr["学期"] = comboBox1.SelectedItem.ToString();
ds.Tables["成绩表"].Rows.Add(dr);
da.Update(ds, "成绩表");
MessageBox.Show("添加成功!");
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";
textBox4.Text = "";
myconnection.Close();
}
catch (SqlException se)
{
MessageBox.Show("该学号在学生信息表中不存在!" + se.Message);
}
}
else
{
MessageBox.Show("请把各项都输入完!");
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true;
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true;
}
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true;
}
private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true;
}
private void button5_Click(object sender, EventArgs e)
{
try
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "成绩表");
MessageBox.Show("更新成功!");
ds.Clear();
button3.Enabled = true;
button5.Enabled = false;
}
catch (SqlException ie)
{
MessageBox.Show("请先修改需要更改的数据!" + ie.Message);
ds.Clear();
}
}
private void button22_Click(object sender, EventArgs e)
{
if (textBox7.Text != "")
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "delete from 成绩表 where 学号='" + (textBox7.Text.Trim()) + "'";
mycommand.Connection = myconnection;
int result = mycommand.ExecuteNonQuery();
if (result > 0)
MessageBox.Show("删除成功!");
else
MessageBox.Show("该学生不存在");
myconnection.Close();
}
catch (FormatException fe)
{
MessageBox.Show("学号格式输入错误," + fe.Message);
}
catch (SqlException se)
{
MessageBox.Show("出现错误!" + se.Message);
}
textBox7.Text = "";
}
else
{
MessageBox.Show("请输入需要删除的学生的学号!");
}
}
private void pointoperate_Load(object sender, EventArgs e)
{
button5.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -