C#连接数据库SqlServer ,非断开式访问
发布日期:2022-01-20 01:08:34 浏览次数:7 分类:技术文章

本文共 7201 字,大约阅读时间需要 24 分钟。

在这里插入图片描述

2. 制作两个页面:歌手管理页面,添加歌手的页面
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述3.添加事件,显示添加歌手对话框
在这里插入图片描述在这里插入图片描述4.编写添加按钮的事件处理代码
在这里插入图片描述4.1 创建DBHelper类`

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SuperKTV.Common{
class DBHelper {
// 定义连接字符串 public static string connString = "server=192.168.10.129;database=SuperKTV;uid=sa;pwd=123456;"; }}`

4.2 编写事件处理代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient; // 引入using SuperKTV.Common; // 引入namespace SuperKTV.Admin{
public partial class AddSingerForm : Form {
public AddSingerForm() {
InitializeComponent(); } private void btn_submit_Click(object sender, EventArgs e) {
// 接收用户输入 string name = this.tb_name.Text; string gender = this.cb_gender.Text; string birthday = this.dtp_birthday.Text; string area = this.cb_area.Text; // 验证 if (name == "") {
MessageBox.Show("用户名不可为空!"); this.tb_name.Focus(); return; } // 增删改造作:非断开式访问 // 创建Connection对象 SqlConnection conn = new SqlConnection(DBHelper.connString); // 打开连接 conn.Open(); // 定义sql语句:insert delete update string sqlString = string.Format("insert into Singer(SingerName,Gender,Birthday,Area) values('{0}','{1}','{2}','{3}')", name, gender, birthday, area); // 创建Command对象 SqlCommand cmd = new SqlCommand(sqlString, conn); // 执行命令 int rows = cmd.ExecuteNonQuery(); // 关闭连接 conn.Close(); if (rows > 0) MessageBox.Show("数据添加成功!"); else MessageBox.Show("数据添加失败!"); } }}

在这里插入图片描述

在这里插入图片描述聚合函数
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient; //引入

using SuperKTV.Common; // 引入

namespace SuperKTV.Admin

{
public partial class SingerManagerForm : Form
{
public SingerManagerForm()
{
InitializeComponent();
}

private void tsb_add_Click(object sender, EventArgs e)    {        AddSingerForm form = new AddSingerForm();        form.ShowDialog();    }    private void SingerManagerForm_Load(object sender, EventArgs e)    {        GetCount();    }    // 统计方法    private void GetCount()    {        // 聚合函数的操作:非断开式访问        // 创建Connection对象        SqlConnection conn = new SqlConnection(DBHelper.connString);        // 打开连接        conn.Open();        // 定义sql语句:聚合函数        string sqlString = "select count(*) from Singer";        string maleString = "select count(*) from Singer where gender='男'";        string femaleString = "select count(*) from Singer where gender='女'";        string groupString = "select count(*) from Singer where gender='组合'";        // 创建Command对象        SqlCommand cmd = new SqlCommand(sqlString, conn);        // 执行命令        int total = (int)cmd.ExecuteScalar();        // 执行后续语句        cmd.CommandText = maleString;        int male = (int)cmd.ExecuteScalar();        cmd.CommandText = femaleString;        int female = (int)cmd.ExecuteScalar();        cmd.CommandText = groupString;        int group = (int)cmd.ExecuteScalar();        // 关闭连接        conn.Close();        //,。        this.tsl_msg.Text = string.Format("共有歌手{0}人,其中男歌手{1}人,女歌手{2}人,组合有{3}个",             total,male,female,group);    }}

}

`
非断开式访问的数据库的查询
在这里插入图片描述非断开式数据库访问:Connection, Command, DataReader
在这里插入图片描述在这里插入图片描述在这里插入图片描述示例3 : 完成歌手管理页面listview控件数据显示
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient; //引入using SuperKTV.Common; // 引入namespace SuperKTV.Admin{
public partial class SingerManagerForm : Form {
public SingerManagerForm() {
InitializeComponent(); } private void tsb_add_Click(object sender, EventArgs e) {
AddSingerForm form = new AddSingerForm(); form.ShowDialog(); } private void SingerManagerForm_Load(object sender, EventArgs e) {
GetCount(); LoadSinger(); } // 统计方法 private void GetCount() {
// 聚合函数的操作:非断开式访问 // 创建Connection对象 SqlConnection conn = new SqlConnection(DBHelper.connString); // 打开连接 conn.Open(); // 定义sql语句:聚合函数 string sqlString = "select count(*) from Singer"; string maleString = "select count(*) from Singer where gender='男'"; string femaleString = "select count(*) from Singer where gender='女'"; string groupString = "select count(*) from Singer where gender='组合'"; // 创建Command对象 SqlCommand cmd = new SqlCommand(sqlString, conn); // 执行命令 int total = (int)cmd.ExecuteScalar(); // 执行后续语句 cmd.CommandText = maleString; int male = (int)cmd.ExecuteScalar(); cmd.CommandText = femaleString; int female = (int)cmd.ExecuteScalar(); cmd.CommandText = groupString; int group = (int)cmd.ExecuteScalar(); // 关闭连接 conn.Close(); // 显示 this.tsl_msg.Text = string.Format("共有歌手{0}人,其中男歌手{1}人,女歌手{2}人,组合有{3}个", total,male,female,group); } // 加载歌手列表方法 private void LoadSinger() {
// 非断开式数据库查询访问 // 1. 创建Connection对象 SqlConnection conn = new SqlConnection(DBHelper.connString); // 2. 打开连接 conn.Open(); // 3. 定义sql语句 string sqlString = "select * from Singer"; // 4. 创建Command对象 SqlCommand cmd = new SqlCommand(sqlString, conn); // 5. 执行命令,返回DataReader对象 SqlDataReader rd = cmd.ExecuteReader(); // 6. 逐行读取数据 while (rd.Read()) {
// 创建 ListViewItem 对象 ListViewItem item = new ListViewItem(rd["SingerID"].ToString()); // subitems属性实现后续列的数据显示 item.SubItems.Add(rd["SingerName"].ToString()); item.SubItems.Add(rd["Gender"].ToString()); item.SubItems.Add(((DateTime)rd["Birthday"]).ToString("yyyy-MM-dd")); item.SubItems.Add(rd["Area"].ToString()); // 把ListViewItem 对象添加listview控件中 this.lv_singermanager.Items.Add(item); } // 7. 关闭 读取器 rd.Close(); // 8. 关闭数据库连接 conn.Close(); } private void tsb_refresh_Click(object sender, EventArgs e) {
this.lv_singermanager.Items.Clear(); // 刷新前需要清空项 GetCount(); LoadSinger(); } }}

在这里插入图片描述

转载地址:https://blog.csdn.net/answero/article/details/106159655 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:c#断开式访问数据库
下一篇:C#连接数据库认识需要的东西

发表评论

最新留言

很好
[***.229.124.182]2024年03月07日 09时35分45秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

php 枚举cookie内容,php设置和获取cookie 2019-04-21
单防区扩展模块怎么用_AB罗克韦尔自动化Micro800 扩展 I/O模块型号及功能介绍 2019-04-21
java矩阵类_Java泛型——泛型矩阵类 2019-04-21
java车牌正则表达式_车牌正则表达式 2019-04-21
wordpress4.9.4 mysql_WordPress 将不再支持 PHP4 和 MySQL 4 2019-04-21
安卓是用java语言写的吗_android开发是用java语言吗? 2019-04-21
java 符号 t_java – 运算符”不能应用于’T’,’T’表示有界泛型类型 2019-04-21
用matlab写出信源熵,计算离散信源的熵matlab实现 2019-04-21
php表单yii2,Yii2创建表单(ActiveForm)方法详解 2019-04-21
php 程序授权机制,授权认证详细说明 2019-04-21
java 命令提示符,如何使用Java打开命令提示符并插入命令? 2019-04-21
IP/tzgm.php,LianjiaSpider/在售数量.ipynb at master · BerSerK/LianjiaSpider · GitHub 2019-04-21
linux移动文件的脚本,使用Linux脚本移动文件 2019-04-21
linux查看系统所有变量,Linux系统各指标命令 2019-04-21
linux打印机守护程序,linux下怎么编写守护程序呢? 2019-04-21
嵌入式linux 设置时间,time_clock控件应用,使用命令date -s 12:00:00手动设置时间后,时间就停住不走了,我在Ubuntu和嵌入式Linux平台都测试到了... 2019-04-21
linux 8086下编译,Ubuntu18.04/Linux下安装DosBox进行8086汇编 2019-04-21
linux监控windows,zabbix监控之linux及windows客户端安装配置 2019-04-21
linux中怎么卸载tree,Liunx系统命令中tree命令详解 2019-04-21
linux 网络音箱 混音6,Linux音频编程(三)混音器介绍 2019-04-21