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

}

相关推荐
米诺zuo3 分钟前
datagrip配置新的数据库
数据库
芒果量化4 分钟前
Optuna - 自动调参利器&python实例
开发语言·python·算法·机器学习
火星MARK6 分钟前
RAID详解
数据库·oracle
JAVA学习通7 分钟前
Spring AI与DeepSeek实战:打造企业级智能体
数据库
.NET修仙日记20 分钟前
.NET WinForms + WPF 综合学习路线:从传统到现代的.NET桌面开发
学习·c#·.net·wpf·.net core·winforms
foundbug99923 分钟前
基于CSMA-CA协议的V2X通信MATLAB仿真
开发语言·网络·matlab
WangMing_X33 分钟前
C#上位机软件:2.5 体验CLR实现多语言混合编程
java·开发语言·c#
jerryinwuhan36 分钟前
pybullet入门到入门_1
开发语言·人工智能·python
豐儀麟阁贵1 小时前
4.4数组的基本操作
java·开发语言·数据结构·算法
Moniane1 小时前
XMW技术:颠覆未来的创新引擎
开发语言