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();

}

相关推荐
2301_79516720几秒前
Python 高手编程系列一十三:现实例子 — 延迟求值属性
开发语言·windows·python
翔云1234561 分钟前
MySQL offline_mode:安全隔离流量,高效完成数据库维护
数据库·mysql·安全
篮球只打两年半1 分钟前
普罗米修斯监控sql(CentOS7)
数据库·sql
Zfox_2 分钟前
【Go】结构体、自定义类型与接口
开发语言·后端·golang
枫叶丹43 分钟前
【Qt开发】Qt窗口(四) -> QDockWidget浮动窗口
c语言·开发语言·c++·qt·开源
007php0073 分钟前
nginx面试之负载均衡的实际经历与配置
运维·数据库·mysql·nginx·面试·职场和发展·负载均衡
星释4 分钟前
Rust 练习册 101:字符串序列切片的艺术
开发语言·后端·rust
6***830512 分钟前
VMware虚拟机配置桥接网络
开发语言·网络·php
de_furina12 分钟前
[C++]string类的使用和模拟实现
开发语言·c++·gitee
白茶三许12 分钟前
【OpenHarmony】Flutter 本地存储全解析:从键值对到数据库
数据库·flutter·开源·openharmony·gitcode