c#操作sql server2008 的界面实例代码
发布时间 - 2026-01-11 00:10:13 点击率:次先是查询整张表,用到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 的界面实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# c#
# 操作sql
# server
# 2008界面
# sql
# C#如何实现对sql server数据库的增删改查
# C#查询SqlServer数据库并返回单个值的方法
# C#访问SqlServer设置链接超时的方法
# C#更新SQLServer中TimeStamp字段(时间戳)的方法
# C#实现异步连接Sql Server数据库的方法
# SQL Server中调用C#类中的方法实例(使用.NET程序集)
# C#控制台程序实现开启、关闭SQLServer服务的代码分享
# C#连接到sql server2008数据库的实例代码
# 小编
# 相关内容
# 在此
# 给大家
# 所述
# 给我留言
# 整张
# 感谢大家
# 疑问请
# 有任何
# true
# SelectionMode
# ReadOnly
# FullRowSelect
# SelectedIndex
# DataGridViewSelectionMode
# EventArgs
# Form1_Load
# DataGridViewCellEventArgs
# AllowUserToAddRows
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Android实现代码画虚线边框背景效果
Laravel如何使用查询构建器?(Query Builder高级用法)
Python自然语言搜索引擎项目教程_倒排索引查询优化案例
Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议
如何快速搭建安全的FTP站点?
如何在IIS中新建站点并配置端口与IP地址?
微信小程序 HTTPS报错整理常见问题及解决方案
Laravel如何创建自定义中间件?(Middleware代码示例)
nodejs redis 发布订阅机制封装实现方法及实例代码
标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析
java ZXing生成二维码及条码实例分享
新三国志曹操传主线渭水交兵攻略
Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】
香港服务器租用费用高吗?如何避免常见误区?
Laravel PHP版本要求一览_Laravel各版本环境要求对照
Laravel如何使用Blade组件和插槽?(Component代码示例)
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
如何快速启动建站代理加盟业务?
Python企业级消息系统教程_KafkaRabbitMQ高并发应用
Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言
Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】
如何使用 jQuery 正确渲染 Instagram 风格的标签列表
Android仿QQ列表左滑删除操作
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
Laravel如何从数据库删除数据_Laravel destroy和delete方法区别
,交易猫的商品怎么发布到网站上去?
如何在建站宝盒中设置产品搜索功能?
Laravel请求验证怎么写_Laravel Validator自定义表单验证规则教程
小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像
在centOS 7安装mysql 5.7的详细教程
Laravel中的Facade(门面)到底是什么原理
如何在万网利用已有域名快速建站?
Win11任务栏卡死怎么办 Windows11任务栏无反应解决方法【教程】
php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】
Laravel怎么设置路由分组Prefix_Laravel多级路由嵌套与命名空间隔离【步骤】
Laravel队列由Redis驱动怎么配置_Laravel Redis队列使用教程
如何用JavaScript实现文本编辑器_光标和选区怎么处理
Laravel怎么创建自己的包(Package)_Laravel扩展包开发入门到发布
如何在Windows 2008云服务器安全搭建网站?
Laravel如何使用Collections进行数据处理?(实用方法示例)
Laravel队列任务超时怎么办_Laravel Queue Timeout设置详解
如何在万网ECS上快速搭建专属网站?
Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验
Laravel如何使用Gate和Policy进行权限控制_Laravel权限判定与策略规则配置
如何用搬瓦工VPS快速搭建个人网站?
*服务器网站为何频现安全漏洞?
Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)
图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?
如何用VPS主机快速搭建个人网站?

