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

}

相关推荐
前端炒粉1 小时前
35.LRU 缓存
开发语言·javascript·数据结构·算法·缓存·js
星释2 小时前
Rust 练习册 75:ETL与数据转换
开发语言·rust·etl
happyjoey2172 小时前
使用Qt自带的Maintenance Tool将Qt6.9升级为QT6.10
开发语言·qt
2301_789380493 小时前
vsc中使用DBHub-MCP连接云Mysql到vsc-copilot
数据库·mysql
杨DaB3 小时前
【MySQL】02 数据库的基本操作
数据库·mysql·oracle
m***66733 小时前
SQL 实战—递归 SQL:层级结构查询与处理树形数据
java·数据库·sql
昙鱼4 小时前
Markdown文件导入Milvus向量数据库完整指南
数据库·ai·milvus
A__tao4 小时前
gotool.top 的 SQL 转 Markdown
数据库·sql
Austindatabases4 小时前
基于SQLite如何设计应用程序,拆散,散,还的散!
数据库·sqlite
矶鹬笛手5 小时前
(2.1) 信息技术及其发展
sql·计算机网络·c#