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

}

相关推荐
啧不应该啊2 分钟前
Day1 Python 与 C 的类型区别
c语言·开发语言
Frank_refuel5 分钟前
终端环境下:Ubuntu 22.04.1 安装 MySQL 数据库
数据库·mysql·ubuntu
cen__y26 分钟前
Linux07(信号01)
linux·运维·服务器·c语言·开发语言
xingpanvip1 小时前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
guygg881 小时前
基于遗传算法的双层规划模型求解MATLAB实现
开发语言·matlab
虹科网络安全1 小时前
艾体宝产品|深度解读 Redis 8.4 新增功能:原子化 Slot 迁移(下)
数据库·redis·bootstrap
凯瑟琳.奥古斯特1 小时前
SQLAlchemy核心功能解析
开发语言·python·flask
卷Java1 小时前
GPTQ vs AWQ vs GGUF:模型量化工具横向测评
开发语言·windows·python
charlie1145141912 小时前
嵌入式C++工程实践第20篇:GPIO 输入模式内部电路 —— 芯片是如何“听“到外部信号的
开发语言·c++·stm32·单片机
xinhuanjieyi2 小时前
极语言让ai学习的方法
开发语言·学习