栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

【数据库】从零开始C#开发数据库并搭建完整系统

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【数据库】从零开始C#开发数据库并搭建完整系统

【数据库】从零开始C#开发数据库并搭建完整系统
  • 选择窗体应用程序
    • 系统概览
    • 找到工具箱
    • 选择 L a b e l Label Label,右键属性修改名称,在 T e x t Text Text旁边进行修改
    • 选择修改文字大小
    • 加入 T e x t B o x TextBox TextBox,即图中小白框
    • 选择一个 B u t t o n Button Button拖下去
    • 选择 R a d i o B u t t o n RadioButton RadioButton拖下去
    • 刚开始 R a d i o B u t t o n RadioButton RadioButton得有个初始选中
  • 添加类
    • 选择 A D O . N E T ADO.NET ADO.NET实体
    • 选择空 C o d e F i r s t Code First CodeFirst模型
    • 添加一个用户存储数据库 I D ID ID和用户名的 U n a m e Uname Uname的 . c s .cs .cs文件
    • 添加一个用于数据库连接的 . c s .cs .cs文件
  • 建数据库
    • 新建数据库
    • 建完数据库后建表
    • 建管理员表,输完后 C t r l + S Ctrl+S Ctrl+S保存表,命名
    • 建用户表,并设置主键
    • 创建一个书的 t B o o k t_Book tB​ook表
    • 创建一个借书的 t l e n d t_lend tl​end
    • 刷新一下
    • 编辑 d b o . t a d m i n dbo.t_admin dbo.ta​dmin前200行
    • 选择 d b o . t u s e r dbo.t_user dbo.tu​ser编辑前200行
    • 新建查询,进行查询
    • 改成 r a d i o B u t t o n U s e r radioButtonUser radioButtonUser和 r a d i o B u t t o n A d m i n radioButtonAdmin radioButtonAdmin
  • 双击登录按钮准备实现功能
    • (1)login.cs:登录函数
    • (2)dao.cs:数据库连接函数
  • 编辑管理员页面
    • 改名
    • 把菜单 ( M e n u S t r i p ) (MenuStrip) (MenuStrip)拉过去
    • 加 l a b e l label label
  • 加个图书管理页面,添加 D a t a G r i d V i e w DataGridView DataGridView,进行窗体分区
    • t _ b o o k t_book t_book里加数据
    • C#里添加数据
    • 属性改成 F a l s e False False最后空白行删去
    • 一次只能修改一个单元格行和列
    • 样式选择 f i x e d 3 D fixed3D fixed3D
    • 最左边白框去掉
    • 选择全局的填充
    • 每次一选选一行
    • 加六个 B u t t o n Button Button,两个 T e x t b o x Textbox Textbox
  • 回到 A d m i n 1 Admin1 Admin1进行跳转操作
    • 从数据库读取数据显示在表格里,修改 A d m i n 2 Admin2 Admin2
  • 添加 A d m i n 21 Admin21 Admin21即 A d m i n 2 Admin2 Admin2下属子窗体,并制作
    • 删除的时候添加点击事件

选择窗体应用程序

系统概览

找到工具箱

选择 L a b e l Label Label,右键属性修改名称,在 T e x t Text Text旁边进行修改


选择修改文字大小

加入 T e x t B o x TextBox TextBox,即图中小白框

选择一个 B u t t o n Button Button拖下去

选择 R a d i o B u t t o n RadioButton RadioButton拖下去

刚开始 R a d i o B u t t o n RadioButton RadioButton得有个初始选中

在属性框里吧 c h e c k e d checked checked的值置为 T r u e True True

此时为选中状态:

添加类

选择 A D O . N E T ADO.NET ADO.NET实体

选择空 C o d e F i r s t Code First CodeFirst模型

添加一个用户存储数据库 I D ID ID和用户名的 U n a m e Uname Uname的 . c s .cs .cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookMS
{
    class Data
    {
        public static string UID = "", Uname = "";//登陆用户的ID
    }
}
添加一个用于数据库连接的 . c s .cs .cs文件
using System.Data.SqlClient;

namespace BookMS
{
    class Dao
    {
        public SqlConnection connect()
        {
            string str = @"Data Source=DESKTOP-AF01JNFSQLEXPRESS;Initial Catalog=BookDB;Integrated Security=True";//数据库连接字符串
            SqlConnection sc = new SqlConnection(str);//创建数据库连接对象
            sc.Open();//打开数据库
            return sc;//返回数据库连接对象
        }
        public SqlCommand command(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connect());
            return cmd;
        }
        public int Execute(string sql)
        {
            return command(sql).ExecuteNonQuery();
        }
        public SqlDataReader read(string sql)
        {
            return command(sql).ExecuteReader();
        }
    }
}

建数据库 新建数据库

建完数据库后建表

建管理员表,输完后 C t r l + S Ctrl+S Ctrl+S保存表,命名

建用户表,并设置主键



再设置主键

加 c h e c k check check约束,点击添加:

表达式选择:

输入:

(sex='男' or sex='女')

创建一个书的 t B o o k t_Book tB​ook表

创建一个借书的 t l e n d t_lend tl​end

选择索引/键,点击添加

刷新一下

编辑 d b o . t a d m i n dbo.t_admin dbo.ta​dmin前200行


选择 d b o . t u s e r dbo.t_user dbo.tu​ser编辑前200行

新建查询,进行查询

改成 r a d i o B u t t o n U s e r radioButtonUser radioButtonUser和 r a d i o B u t t o n A d m i n radioButtonAdmin radioButtonAdmin


双击登录按钮准备实现功能 (1)login.cs:登录函数
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 BookMS
{
    public partial class login : Form
    {    

        public login()
        {
            InitializeComponent();
        }

         private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text!=""&&textBox2.Text!="")
            {
                Login();
            }
            else
            {
                MessageBox.Show("输入有空项,请重新输入");
            }
        }
        //登陆方法,验证是否允许登陆,允许返回真
        public void  Login()
        {
            //用户
            if(radioButtonUser.Checked==true)
            {
                Dao dao = new Dao();
                string sql = "select * from t_user where id='"+textBox1.Text+"' and psw='"+textBox2.Text+"'";
                IDataReader dc = dao.read(sql);
                if(dc.Read())
                {
                    MessageBox.Show("登陆成功");
                    User1 user = new User1();
                    this.Hide();
                    user.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("登陆失败");
                }
                dao.DaoClose();
             }
                //管理员
                if(radioButtonAdmin.Checked==true)
                {
                    Dao dao = new Dao();
                    string sql = "select * from t_admin where id='" + textBox1.Text + "' and psw='" + textBox2.Text + "'";
                    IDataReader dc = dao.read(sql);
                    if (dc.Read())
                    {
                        MessageBox.Show("登陆成功");
                    Admin1 a = new Admin1();
                    //this.Hide();
                    a.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("登陆失败");
                    }
                dao.DaoClose();
            }
            MessageBox.Show("单选框请先选中");
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}

(2)dao.cs:数据库连接函数
using System.Data.SqlClient;
namespace BookMS
{
    class Dao
    {
        SqlConnection sc;
        public SqlConnection connect()
        {
            string str = @"Data Source=DESKTOP-AF01JNFSQLEXPRESS;Initial Catalog=BookDB;Integrated Security=True";//数据库连接字符串
            sc = new SqlConnection(str);//创建数据库连接对象
            sc.Open();//打开数据库
            return sc;//返回数据库连接对象
        }
        public SqlCommand command(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connect());
            return cmd;
        }
        public int Execute(string sql)//更新操作
        {
            return command(sql).ExecuteNonQuery();
        }
        public SqlDataReader read(string sql)//读取操作
        {
            return command(sql).ExecuteReader();
        }
        public void DaoClose()
        {
            sc.Close();//关闭数据库连接
        }
    }
 } 
编辑管理员页面 改名

把菜单 ( M e n u S t r i p ) (MenuStrip) (MenuStrip)拉过去

加 l a b e l label label

加个图书管理页面,添加 D a t a G r i d V i e w DataGridView DataGridView,进行窗体分区

t _ b o o k t_book t_book里加数据
insert into t_book values('20210125','计算机组成原理','Marx','CAU publisher','10'),('20210126','数据库原理','Marx','NKU publisher','10')
select *from t_Book

C#里添加数据


然后点击编辑列,点击添加

添加页眉文本即可

添加成功,出现以下样式

属性改成 F a l s e False False最后空白行删去

一次只能修改一个单元格行和列

样式选择 f i x e d 3 D fixed3D fixed3D

最左边白框去掉

选择全局的填充

每次一选选一行

加六个 B u t t o n Button Button,两个 T e x t b o x Textbox Textbox

回到 A d m i n 1 Admin1 Admin1进行跳转操作

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 BookMS
{
    public partial class Admin1 : Form
    {
       

        private void Admin1_Load(object sender, EventArgs e)
        {

        }

        private void 图书管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Admin2 admin = new Admin2();
            admin.ShowDialog();
        }
    }
}

从数据库读取数据显示在表格里,修改 A d m i n 2 Admin2 Admin2
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 BookMS
{
    public partial class Admin2 : Form
    {
        public Admin2()
        {
            //个个控件的显示
            InitializeComponent();
            Table();
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void Admin2_Load(object sender, EventArgs e)
        {

        }
        public void Table()
        {
            dataGridView1.Rows.Clear();//清楚旧数据
            Dao dao = new Dao();
            string sql = "select *from t_book";
            IDataReader dc = dao.read(sql);
            while(dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(),dc[1].ToString(),dc[2].ToString(),dc[3].ToString(),dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }
    }
}
添加 A d m i n 21 Admin21 Admin21即 A d m i n 2 Admin2 Admin2下属子窗体,并制作


”添加图书“和“取消”的代码

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 BookMS
{
    public partial class Admin21 : Form
    {
        public Admin21()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != ""&&textBox5.Text != "")
            {
                Dao dao = new Dao();
                string sql = $"insert into t_book values ('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}','{textBox5.Text}')";
                int n = dao.Execute(sql);
                if (n > 0)
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
            }
            else
            {
                MessageBox.Show("输入不许为空");
            }
        }

        private void Admin21_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }
    }
}

删除的时候添加点击事件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023158.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号