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

}

相关推荐
⑩-15 分钟前
Redis(1)
数据库·redis·缓存
2301_8035545223 分钟前
主从同步配置的步骤
数据库
无敌最俊朗@27 分钟前
00-7天攻破sqlite数据库(总览sqlite)
数据库·sqlite
William_cl32 分钟前
C# ASP.NET MVC 数据验证实战:View 层双保险(Html.ValidationMessageFor + jQuery Validate)
后端·c#·asp.net·mvc
Python私教35 分钟前
Python 开发环境安装与配置全指南(2025版)
开发语言·python
Access开发易登软件38 分钟前
Access导出带图表的 HTML 报表:技术实现详解
数据库·后端·html·vba·导出·access
百锦再1 小时前
第12章 测试编写
android·java·开发语言·python·rust·go·erlang
无敌最俊朗@1 小时前
C++ 并发与同步速查笔记(整理版)
开发语言·c++·算法
_Minato_1 小时前
数据库知识整理——SQL数据定义
数据库·sql·mysql·oracle·database·数据库开发·数据库架构
Elastic 中国社区官方博客1 小时前
Observability:适用于 PHP 的 OpenTelemetry:EDOT PHP 加入 OpenTelemetry 项目
大数据·开发语言·人工智能·elasticsearch·搜索引擎·全文检索·php