本文主要是从头开始讲如何创建一个项目,本文以创建一个歌曲管理系统为例。

首先创建数据库:MyMusic(我的音乐)

其中添加表:SongInformation(歌曲信息表)

if DB_ID('MyMusic') is not null
drop database MyMusic
go
create database MyMusic  --创建MyMusic(我的音乐)数据库
on
(
name=MyMusic,
filename='D:\CS架构\学习\窗体\DB\MyMusic.mdf'
)
--打开数据库
use MyMusic
--创建表歌曲
if OBJECT_ID('SongInformation') is not null
drop table SongInformation --歌曲信息表
go
create table SongInformation
(Songid int primary key identity(10000,1),  --编号SongName varchar(50) not null,   --歌名Singer varchar(50) not null,  --演唱者Album varchar(50)  --专辑
)
--查询表SongInformation
select *from SongInformation
--添加测试数据
insert SongInformation select '演员','薛之谦','绅士'union
select '尽头','赵方倩','音阙诗听'union
select '当你','王心凌','遇上爱'union
select '七里香','周杰伦','七里香'union
select '微微一笑很倾城','杨洋','微微一笑很倾城'union
select '岁月神偷','金玟岐','金玟岐作品集'union
select '带你去旅行','校长','带你去旅行'

创建主窗口:FrmMain(歌曲信息管理系统

创建窗口:歌曲修改(增加)窗口FrmModifySong

准备工作 --建立帮助类:DBHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using System.Data;
using System.Data.SqlClient;namespace 窗口
{public   class DBHelper{public static string ConStr = "server=.;uid=sa;pwd=sa;database=MyMusic";public  static SqlConnection con = null;#region 创建连接对象public  static SqlConnection GetConnection(){if (con == null || con.ConnectionString == ""){con = new SqlConnection(ConStr);}return con;} #endregion#region 打开连接public static void OpenConnection(){if (con.State == ConnectionState.Closed){con.Open();}}#endregion#region 关闭连接public static void CloseConnection(){if (con.State == ConnectionState.Open){con.Close();}}#endregion#region 查询多行多列的值public static SqlDataReader ExecuteReader(string sql,params SqlParameter [] para){SqlConnection con = GetConnection();OpenConnection();SqlCommand com = new SqlCommand(sql, con);com.Parameters.AddRange(para);SqlDataReader dr = com.ExecuteReader();return dr;}#endregion#region 动作查询public static int ExecuteNonQuery(string sql,params SqlParameter[] para){SqlConnection con = GetConnection();OpenConnection();SqlCommand com = new SqlCommand(sql, con);       com.Parameters.AddRange(para);int n = com.ExecuteNonQuery();CloseConnection();return n;}#endregion}
}

建立实体类:Song(对应数据库中的表SongInformation)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 窗口
{public  class Song{public int Songid { get; set; }public string SongName { get; set; }public string Singer { get; set; }public string Album { get; set; }}
}

然后实现窗体加载功能(将数据加载到DataGirdView中)

  #region 将后台信息加载到网格private void LoadDOV(string sql){if (sql == ""){sql = "select*from SongInformation";}SqlConnection con = new SqlConnection(DBHelper.ConStr);SqlDataAdapter sda = new SqlDataAdapter(sql, con);DataTable dt = new DataTable();sda.Fill(dt);dgvSong.DataSource = dt;}#endregion#region 窗体加载事件方法private void FrmMain_Load(object sender, EventArgs e){LoadDOV("");}#endregion

View Code

实现查询功能:

 #region 查询事件方法private void btnQuery_Click(object sender, EventArgs e){string sql = "select*from SongInformation where 1=1";if (txtSongName.Text != ""){sql += "and SongName like '%" + txtSongName.Text + "%'";}if (txtSinger.Text != ""){sql += "and Singer like '%" + txtSinger.Text + "%'";}LoadDOV(sql);}#endregion

最后!也是难点!传参,将DataGridView中的数据传到FrmModifySong窗口中对应的Textbox中

首先创建实体操作类SongManage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using System.Data.SqlClient;namespace 窗口
{public class SongManage{//传参public static List<Song> SelectCarsAll(){List<Song> song = new List<Song>();string sql = "select*from SongInformation";SqlDataReader dr = DBHelper.ExecuteReader(sql);while (dr.Read()){Song songs = new Song();songs.Songid = Convert.ToInt32(dr["Songid"]);songs.SongName = Convert.ToString(dr["SongName"]);songs.Singer = Convert.ToString(dr["Singer"]);songs.Album = Convert.ToString(dr["Album"]);song.Add(songs);}dr.Close();DBHelper.CloseConnection();return song;}//修改public static Song SelectCarsByCarId(int Songid){Song song = null;string sql = "select * from SongInformation where Songid=" + Songid;SqlDataReader dr = DBHelper.ExecuteReader(sql);if (dr.Read()){song = new Song();song.Songid = Convert.ToInt32(dr["Songid"]);song.SongName = Convert.ToString(dr["SongName"]);song.Singer = Convert.ToString(dr["Singer"]);song.Album = Convert.ToString(dr["Album"]);}dr.Close();DBHelper.CloseConnection();return song;}//添加public static int InsertSong(Song song){string sql = string.Format("insert  SongInformation (SongName,Singer,Album) values('{0}','{1}','{2}')", song.SongName, song.Singer, song.Album);return DBHelper.ExecuteNonQuery(sql);}//更新public static int UpdateSong(Song song){string sql = "update SongInformation set SongName=@SongName,Singer=@Singer,Album=@Album where Songid=@Songid";return DBHelper.ExecuteNonQuery(sql,new SqlParameter[] {new SqlParameter("@SongName",song.SongName),new SqlParameter("@Singer",song.Singer),new SqlParameter("@Album",song.Album),new SqlParameter("@SongId",song.Songid),});}}
}

View Code

然后回到主窗口FrmMain中创建修改、添加的方法

  #region 修改/// <summary>/// 修改事件方法/// </summary>private void TsmModify_Click(object sender, EventArgs e){if (dgvSong.SelectedRows.Count > 0){int Songid = (int)dgvSong.SelectedRows[0].Cells["Songid"].Value;FrmModifySong frm = new FrmModifySong();frm.Songid = Songid;frm.ShowDialog();}}#endregion#region 添加事件方法private void tsmAdd_Click(object sender, EventArgs e){FrmModifySong frm = new FrmModifySong();          frm.Show();} #endregion

然后回到歌曲添加(删除)窗口FrmModifySong实现保存功能和取消功能

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;namespace 窗口
{public partial class FrmModifySong : Form{public int Songid { get; set; }private string type = "添加";public FrmModifySong(){InitializeComponent();}#region 取消按钮事件方法private void btnCancel_Click(object sender, EventArgs e){this.Close();}#endregion#region 保存按钮事件方法private void btnSave_Click(object sender, EventArgs e){Song song = new Song{    SongName=txtSongName.Text,Singer=txtSinger.Text,Album=txtAlbum.Text            };int n = 0;if (type == "添加"){n = SongManage.InsertSong(song);}else{song.Songid = Songid;n = SongManage.UpdateSong(song);}if (n > 0){MessageBox.Show(type + "成功!");}else{MessageBox.Show(type + "失败!");}}#endregionprivate void FrmModifySong_Load(object sender, EventArgs e){if (Songid != 0){type = "修改";txtSongid.ReadOnly = true;Song song = SongManage.SelectCarsByCarId(Songid);if (song != null){txtSongid.Text = song.Songid.ToString();txtSongName.Text = song.SongName;txtSinger.Text = song.Singer;txtAlbum.Text = song.Album;}this.Text = "修改歌曲信息";}else{txtSongid.ReadOnly = false;this.Text = "添加歌曲信息";}}}
}

View Code

最后实现删除功能

  #region 删除事件方法//删除事件方法private void TsmDelete_Click(object sender, EventArgs e){int Songid = (int)dgvSong.SelectedRows[0].Cells[0].Value;string sql = "delete SongInformation  where SongId=" + Songid;int n = DBHelper.ExecuteNonQuery(sql);if (n > 0){MessageBox.Show("删除成功", "删除提示");LoadDOV("");}else{MessageBox.Show("删除失败", "删除提示");}} #endregion

OK,一个简单的项目就这么完成了!

转载于:https://www.cnblogs.com/F6F6F6/p/8082259.html

零基础开发--歌曲管理系统相关推荐

  1. 小程序获取头像试试水 02《 程序员变现指南之 微信QQ 小程序 真的零基础开发宝典》

    本系列教程是针对粉丝的变现教程,还不是粉丝的可以关注我并且到社区:https://bbs.csdn.net/topics/603436232 进行打卡,不是老粉的也可以获取最终的技术变现学习,最终还有 ...

  2. 小程序的 HelloWord 01《 程序员变现指南之 微信QQ 小程序 真的零基础开发宝典》

    本系列教程是针对粉丝的变现教程,还不是粉丝的可以关注我并且到社区:https://bbs.csdn.net/topics/603436232 进行打卡,不是老粉的也可以获取最终的技术变现学习,最终还有 ...

  3. html5游戏开发-零基础开发RPG游戏-开源讲座(四)

    了解上三篇的内容请点击: html5[color=rgb(68, 68, 68) !important]游戏开发-零基础开发RPG游戏-开源讲座(一) http://www.html5cn.org/a ...

  4. html5游戏开发-零基础开发《圣诞老人送礼物》小游戏

    开言: 以前lufy前辈写过叫"html5游戏开发-零基础开发RPG游戏"的系列文章,在那里面我学习了他的引擎以及了解了游戏脚本.自从看了那几篇文章,我便对游戏开发有了基本的认识. ...

  5. React + Redux + Express + Mongodb 零基础开发完整大型商城网站视频教程(97 个视频)

    React + Redux + Express + Mongodb 零基础开发完整大型商城网站视频教程(97 个视频) mern 架构零基础开发完整电商网站 React + Redux + Expre ...

  6. Android零基础开发到项目实战

    Android零基础开发到项目实战(目录) 前言:本教程适合零基础学习安卓开发的伙伴,下面是目录,本博主会每天定时更新每一章节的教程,未完..... 一.Java基础阶段 day01_Java语言概述 ...

  7. 零基础开发 Node.js Addons 插件:参数与返回值处理

    上一篇回顾 零基础开发 Node.js Addons 插件:Hello Node-API.本篇介绍使用 Node-API 为 Node.js 开发基于 C 的 Addons 时,如何接收与处理 Nod ...

  8. 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解...

    salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解 建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema ...

  9. html5游戏开发-零基础开发RPG游戏-开源讲座(三)-卷轴对话实现

    前两篇,RPG的开发已经实现了添加地图和添加游戏人物,本篇来实现地图的卷轴滚动和人物对话的实现,效果如下 想要了解前两篇内容,请电击下面的链接 html5游戏开发-零基础开发RPG游戏-开源讲座(一) ...

最新文章

  1. REDIS的几个测试结果
  2. 洛谷——P1102 A-B数对
  3. UNIX再学习 -- shell编程
  4. AjaxPro怎么用
  5. 微信小程序制作课程表_课表微信小程序实现(纯技术文)
  6. 鸿蒙系统跟腾讯合作,要和华为鸿蒙竞争?腾讯系统开源了,仅1.8K,支持众多芯片 - 区块网...
  7. 洛谷—— P2251 质量检测
  8. java.util.function包下的四大Function
  9. linux中cd中文意思,linux中cd ~和!!是什么意思?
  10. 倒计时7天丨2022全球边缘计算大会深圳站参会指南来啦!
  11. linux 中du和df的使用
  12. 执行celery -A tasks worker --loglevel=info报错
  13. SEO初学者如何快速做好 SEO 优化?seo数据查询
  14. python制作相册
  15. 用Java给您的图片瘦身之Thumbnailator技术
  16. 前端埋点和后端埋点能分开使用吗?【数据埋点介绍】
  17. ps改变叶子中的图片
  18. 免费的电子书下载网站
  19. SuperMap 试用许可申请
  20. 夺命雷公狗---linux NO:14 linux系统重定向

热门文章

  1. 【工具安装】Quartus II 安装与驱动
  2. SpringBoot spring-data-jpa表的生成
  3. 【入门数据分析】淘宝用户行为分析
  4. diy 服务器 支持 esxi,【我的技术我做主】IT屌丝DIY ESXI虚拟化服务器再度升级ESXI6.0...
  5. 如何用计算机算出我喜欢你,如何用数字表白我喜欢你?
  6. 音视频的相关名词、术语、概念
  7. “大数据杀熟”?商家对数据的使用可能远超出想象
  8. ccd视觉定位教程_CCD视觉定位识别系统,视觉系统ccd定位原理
  9. 多层神经网络 ——小批量梯度下降法
  10. INCREMENTAL NETWORK QUANTIZATION: TOWARDS LOSSLESS CNNS WITH LOW-PRECISION WEIGHTS