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

相关文章

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满