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.SelectedRows0.Cells0.Value.ToString(); // 学号

string intosql = $"delete from Students where Studentid='{id}'";

update(intosql);

MessageBox.Show("删除成功");

gettable();

}

相关推荐
北域码匠6 分钟前
SHA-1算法:安全哈希原理与应用解析
算法·c#·哈希算法
skylar012 分钟前
小白1分钟安装flash-attn
开发语言·python
syt_biancheng15 分钟前
Redis初识
数据库·redis·缓存
默子昂20 分钟前
ollama 自定义ui
开发语言·python·ui
cmes_love42 分钟前
股票逐笔level2历史行情下载十档订单薄五档tick分钟下载分享
数据库·区块链
仙俊红43 分钟前
SQL 调优需要掌握的知识
数据库·sql
123的故事1 小时前
工具分享(7)-多Excel文件内容查询工具
c#·excel·实用工具
fofantasy1 小时前
NSK LH12AN 微型导轨技术手册
运维·网络·数据库·经验分享·规格说明书
杨运交1 小时前
[032][缓存模块]基于Redis Bitmap的用户行为统计实战:签到与日活分析
数据库·redis·缓存
赴生-2 小时前
C++进阶 C++11(下)
开发语言·c++