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

相关文章

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句

六个案例搞懂mysql间隙锁

《六个案例搞懂mysql间隙锁》MySQL中的间隙是指索引中两个索引键之间的空间,间隙锁用于防止范围查询期间的幻读,本文主要介绍了六个案例搞懂mysql间隙锁,具有一定的参考价值,感兴趣的可以了解一下... 目录概念解释间隙锁详解间隙锁触发条件间隙锁加锁规则案例演示案例一:唯一索引等值锁定存在的数据案例二:

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

MYSQL查询结果实现发送给客户端

《MYSQL查询结果实现发送给客户端》:本文主要介绍MYSQL查询结果实现发送给客户端方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql取数据和发数据的流程(边读边发)Sending to clientSending DataLRU(Least Rec

MySQL分区表的具体使用

《MySQL分区表的具体使用》MySQL分区表通过规则将数据分至不同物理存储,提升管理与查询效率,本文主要介绍了MySQL分区表的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、分区的类型1. Range partition(范围分区)2. List partition(列表分区)3. H