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中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表

SQL server配置管理器找不到如何打开它

《SQLserver配置管理器找不到如何打开它》最近遇到了SQLserver配置管理器打不开的问题,尝试在开始菜单栏搜SQLServerManager无果,于是将自己找到的方法总结分享给大家,对SQ... 目录方法一:桌面图标进入方法二:运行窗口进入方法三:查找文件路径方法四:检查 SQL Server 安

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

Python 中的 with open文件操作的最佳实践

《Python中的withopen文件操作的最佳实践》在Python中,withopen()提供了一个简洁而安全的方式来处理文件操作,它不仅能确保文件在操作完成后自动关闭,还能处理文件操作中的异... 目录什么是 with open()?为什么使用 with open()?使用 with open() 进行

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI