c#操作sql server2008 的界面

2024-02-23 01:48

本文主要是介绍c#操作sql server2008 的界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先是查询整张表,用到combobox选择查询哪张表,最后用DataGridView显示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){}private void Form1_Load(object sender, EventArgs e){this.dataGridView1.RowHeadersVisible = false;this.dataGridView1.AllowUserToAddRows = false;this.dataGridView1.ReadOnly = true;this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;// this.comboBox1.SelectedIndex =0;string sql = "select * from student";DataTable table = SqlManage.TableSelect(sql);this.dataGridView1.DataSource = table;comboBox1.Items.Add("学生表");comboBox1.Items.Add("教师表");}private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){string sql = "";switch (this.comboBox1.SelectedIndex){ case 0:sql = "select id as 学生号,name as 姓名,sage as 年龄 from student";break;case 1:sql = "select t_id as 教师号,t_name as 姓名,T_age as 年龄 from teacher";break;default:break;}DataTable table = SqlManage.TableSelect(sql);this.dataGridView1.DataSource = table;}}
}






然后是修改表格,这个比较简单,用到textbox和button



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{public partial class Form2 : Form{public Form2(){InitializeComponent();}private void button4_Click(object sender, EventArgs e){this.Close();}private void button1_Click(object sender, EventArgs e){string sql = string.Format("insert into teacher values('{0}','{1}','{2}')",this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);SqlManage.TableChange(sql);}private void button2_Click(object sender, EventArgs e){string sql = string.Format("update teacher set ('{0}',''{1}'','{2}')",this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);SqlManage.TableChange(sql);}private void button3_Click(object sender, EventArgs e){string sql = string.Format("delete from teacher where t_id='{0}'",this.textBox1.Text);SqlManage.TableChange(sql);}private void Form2_Load(object sender, EventArgs e){}}
}



按条件查询表格,这个是核心,用到radiobutt,combobox,,button, DataGridView




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{public partial class Form3 : Form{public Form3(){InitializeComponent();}private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){}private void Form3_Load(object sender, EventArgs e){this.comboBox1.Enabled = false;this.comboBox2.Enabled = false;this.comboBox3.Enabled = false;this.comboBox4.Enabled = false;//初始化教师编号string sql = "select t_id from teacher";DataTable table = SqlManage.TableSelect(sql);string t_id;foreach (DataRow row in table.Rows){t_id = row["t_id"].ToString();this.comboBox1.Items.Add(t_id);}if (table.Rows.Count > 0){this.comboBox1.SelectedIndex = 0;}//初始化教师姓名string sql_name = "select t_name from teacher";table.Clear();table = SqlManage.TableSelect(sql_name);string t_name;foreach (DataRow row in table.Rows){t_name= row["t_name"].ToString();this.comboBox2.Items.Add(t_name);}if (table.Rows.Count > 0){this.comboBox2.SelectedIndex = 0;}//初始化学生string sql_id = "select id from student";table.Clear();table = SqlManage.TableSelect(sql_id);string s_id;foreach (DataRow row in table.Rows){s_id = row["id"].ToString();this.comboBox3.Items.Add(s_id);}if (table.Rows.Count > 0){this.comboBox3.SelectedIndex = 0;}//初始化学生string sql_sname = "select name from student";table.Clear();table = SqlManage.TableSelect(sql_sname);string t_sname;foreach (DataRow row in table.Rows){t_sname = row["name"].ToString();this.comboBox4.Items.Add(t_sname);}if (table.Rows.Count > 0){this.comboBox4.SelectedIndex = 0;}}private void button2_Click(object sender, EventArgs e){this.Close();}private void button1_Click(object sender, EventArgs e){string sql = "";if (this.radioButton1.Checked){sql = string.Format("select t_id as 教师编号,t_name as 教师姓名,t_age as 年龄 from teacher where t_id = '{0}'",this.comboBox1.Text);}else if (this.radioButton2.Checked){sql = string.Format("select t_id as 教师编号,t_name as 教师姓名,t_age as 年龄 from teacher where t_name = '{0}'",this.comboBox2.Text);}else if (this.radioButton3.Checked){sql = string.Format("select id as 学生编号,name as 学生姓名,sage as 年龄 from student where id = '{0}'",this.comboBox3.Text);}else if (this.radioButton4.Checked){sql = string.Format("select id as 学生编号,name as 学生姓名,sage as 年龄 from student where name = '{0}'",this.comboBox4.Text);}DataTable table = SqlManage.TableSelect(sql);if (table.Rows.Count > 0){this.dataGridView1.DataSource = table;}else{MessageBox.Show("没有相关内容");}}private void radioButton1_CheckedChanged(object sender, EventArgs e){if (this.radioButton1.Checked){this.comboBox1.Enabled = true;}else{this.comboBox1.Enabled = false;}}private void radioButton2_CheckedChanged(object sender, EventArgs e){if (this.radioButton2.Checked){this.comboBox2.Enabled = true;}else{this.comboBox2.Enabled = false;}}private void radioButton3_CheckedChanged(object sender, EventArgs e){if (this.radioButton3.Checked){this.comboBox3.Enabled = true;}else{this.comboBox3.Enabled = false;}}private void radioButton4_CheckedChanged(object sender, EventArgs e){if (this.radioButton4.Checked){this.comboBox4.Enabled = true;}else{this.comboBox4.Enabled = false;}}}
}


这篇关于c#操作sql server2008 的界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/737159

相关文章

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

虚拟机Centos7安装MySQL数据库实践

《虚拟机Centos7安装MySQL数据库实践》用户分享在虚拟机安装MySQL的全过程及常见问题解决方案,包括处理GPG密钥、修改密码策略、配置远程访问权限及防火墙设置,最终通过关闭防火墙和停止Net... 目录安装mysql数据库下载wget命令下载MySQL安装包安装MySQL安装MySQL服务安装完成

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

MySQL逻辑删除与唯一索引冲突解决方案

《MySQL逻辑删除与唯一索引冲突解决方案》本文探讨MySQL逻辑删除与唯一索引冲突问题,提出四种解决方案:复合索引+时间戳、修改唯一字段、历史表、业务层校验,推荐方案1和方案3,适用于不同场景,感兴... 目录问题背景问题复现解决方案解决方案1.复合唯一索引 + 时间戳删除字段解决方案2:删除后修改唯一字

Zabbix在MySQL性能监控方面的运用及最佳实践记录

《Zabbix在MySQL性能监控方面的运用及最佳实践记录》Zabbix通过自定义脚本和内置模板监控MySQL核心指标(连接、查询、资源、复制),支持自动发现多实例及告警通知,结合可视化仪表盘,可有效... 目录一、核心监控指标及配置1. 关键监控指标示例2. 配置方法二、自动发现与多实例管理1. 实践步骤

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、