C#窗体程序学生管理

代码如下:

public static string constr = "Data Source=FUSHUAI;Initial Catalog=product;Integrated Security=True";

public static SqlConnection con = new SqlConnection(constr);

private void Form1_Load(object sender, EventArgs e)

{

gettable();

}

//执行查询操作

public static DataTable query(string sql)

{

SqlDataAdapter sda = new SqlDataAdapter(sql, constr);

DataTable dtable = new DataTable();

sda.Fill(dtable);

return dtable;

}

//执行更新操作

public static string update(string sql)

{

try

{

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=数据表;Integrated Security=True"); // 打开数据库连接

conn.Open();

SqlCommand cmd = new SqlCommand(sql, conn);

int result = cmd.ExecuteNonQuery();

conn.Close();

return "success";

}

catch (Exception e)

{

MessageBox.Show(e.Message);

return e.ToString();

}

}

// 添加按钮

private void button1_Click(object sender, EventArgs e)

{

string id = textBox1.Text.Trim(); // 学号

string name = textBox2.Text.Trim();// 名称

string gender = comboBox1.Text.Trim();// 性别

string age = textBox3.Text.Trim();// 年龄

string intosql = $"insert into [Students] values('{id}','{name}','{gender}','{age}')";

update(intosql);

MessageBox.Show("添加成功");

gettable();

}

public void gettable()

{

string sql = "select * from students";

dataGridView1.DataSource = query(sql);

}

// 删除按钮

private void button2_Click(object sender, EventArgs e)

{

string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); // 学号

string intosql = $"delete from [Students] where Studentid='{id}'";

update(intosql);

MessageBox.Show("删除成功");

gettable();

}

相关推荐
智航GIS12 分钟前
10.7 pyspider 库入门
开发语言·前端·python
跟着珅聪学java16 分钟前
JavaScript 底层原理
java·开发语言
l1t18 分钟前
DeepSeek辅助编写的利用位掩码填充唯一候选数方法求解数独SQL
数据库·sql·算法·postgresql
项目題供诗21 分钟前
C语言基础(二)
c语言·开发语言
J_liaty25 分钟前
RabbitMQ面试题终极指南
开发语言·后端·面试·rabbitmq
墨月白27 分钟前
[QT] QT中的折线图和散点图
数据库·qt
福大大架构师每日一题35 分钟前
2026年1月TIOBE编程语言排行榜,Go语言排名第16,Rust语言排名13。C# 当选 2025 年度编程语言。
golang·rust·c#
wangnaisheng35 分钟前
【C#】gRPC的使用,以及与RESTful的区别和联系
c#
JosieBook35 分钟前
【开源】基于 C# 和 Halcon 机器视觉开发的车牌识别工具(附带源码)
开发语言·c#