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

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

MySQL字符串常用函数详解

《MySQL字符串常用函数详解》本文给大家介绍MySQL字符串常用函数,本文结合实例代码给大家介绍的非常详细,对大家学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql字符串常用函数一、获取二、大小写转换三、拼接四、截取五、比较、反转、替换六、去空白、填充MySQL字符串常用函数一、

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

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