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

}

相关推荐
9***4463几秒前
Spring 核心技术解析【纯干货版】- Ⅶ:Spring 切面编程模块 Spring-Instrument 模块精讲
前端·数据库·spring
新手村领路人几秒前
c++ opencv缺少openh264-1.8.0-win64.dll
开发语言·c++
I***2615几秒前
MySQL 的mysql_secure_installation安全脚本执行过程介绍
数据库·mysql·安全
周杰伦fans1 分钟前
C# - 直接使用 new HttpClient() 和使用 HttpClientFactory 的区别
开发语言·c#
kyle~2 分钟前
C++ --- noexcept关键字 明确函数不抛出任何异常
java·开发语言·c++
a***56062 分钟前
MySQL数据库误删恢复_mysql 数据 误删
数据库·mysql·adb
不知所云,3 分钟前
6. c++ 20 Modules 使用
开发语言·c++20·c++ modules
沐浴露z4 分钟前
详解Java ArrayList
java·开发语言·哈希算法
x***B4115 分钟前
Rust unsafe代码规范
开发语言·rust·代码规范
a***13147 分钟前
Django视图与URLs路由详解
数据库·django·sqlite