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

 

  首先创建数据库: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

相关文章:

  • 2021-12-24
  • 2021-09-21
  • 2022-12-23
  • 2021-11-30
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
相关资源
相似解决方案