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

}

相关推荐
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql