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 T3Demo4
{
    class DBHelper
    {
        //数据库连接字符串
        public static string ConnString = "server=.;database=TVDB2;uid=sa;pwd=123456;";

        //数据库连接对象
        public static SqlConnection Conn = null;

        //初始化数据库连接
        public static void InitConnection()
        {
            //如果连接对象不存在,则创建连接
            if (Conn == null)
                Conn = new SqlConnection(ConnString);
            //如果连接对象关闭,则打开连接
            if (Conn.State == ConnectionState.Closed)
                Conn.Open();
            //如果连接中断,则重启连接
            if (Conn.State == ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }
        }

        //查询,获取DataReader
        public static SqlDataReader GetDataReader(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            //CommandBehavior.CloseConnection 命令行为:当DataReader对象被关闭时,自动关闭占用的连接对象
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        //查询,获取DataTable
        public static DataTable GetDataTable(string sqlStr)
        {
            InitConnection();
            DataTable table = new DataTable();
            SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
            dap.Fill(table);
            Conn.Close();
            return table;
        }

        //增删改
        public static bool ExecuteNonQuery(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            int result = cmd.ExecuteNonQuery();
            Conn.Close();
            return result > 0;
        }

        //执行集合函数
        public static object ExecuteScalar(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            object result = cmd.ExecuteScalar();
            Conn.Close();
            return result;
        }
    }
}

1.1

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 WindowsFormsApp5._27课上;

namespace _1._1teacher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
            //刷新数据
            GetData();//调用方法,重新刷新
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //命名
        /// <summary>
        /// 窗体加载
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        DataTable dt = new DataTable();
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                GetData();
            }
            catch (Exception ex)//加个变量
            {
                MessageBox.Show(ex.Message);//加个提示

                throw;
            }
        }

        private void GetData()
        {
            
            string sql = "select*from tb_stu";
            dt=DBHelper.GetDataTable(sql);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string SName = textBox1.Text;
                //DataView筛选
                DataView dv = dt.DefaultView;
                dv.RowFilter = $"SName like '%{SName}%'";
                dataGridView1.DataSource = dv;//数据在datagridview里
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}

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 WindowsFormsApp5._27课上;

namespace _1._1teacher
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string SName = textBox1.Text;
                string Sex = comboBox1.Text;
                string SAddress = textBox3.Text;
                string Stel = textBox4.Text;
                if (SName != "")//考虑为空的情况
                {
                    string sql = $"insert into tb_stu values '{SName}','{Sex}','{SAddress}','{Stel}'";
                    bool flag = DBHelper.ExecuteNonQuery(sql);
                    if (flag)
                    {
                        MessageBox.Show("添加成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                else
                {
                    MessageBox.Show("姓名不为空");//
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();//关闭当前窗口
        }
    }
}

2.1

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 T3Demo4;

namespace two_one
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        //双击
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            DataView dv = new DataView(dt);
            //RowFilter筛选行
            dv.RowFilter = $"id='{id}'";
            label2.Text = dv[0]["title"].ToString();
            label4.Text = dv[0]["date"].ToString();
            label6.Text = dv[0]["nums"].ToString();
            label8.Text = dv[0]["mark"].ToString();
            string image = dv[0]["image"].ToString();
            //图片集里面的图片=事件图片.集合[image];
            pictureBox1.Image = imageList1.Images[image];
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string sql = "select * from tb_tv";
                dt = DBHelper.GetDataTable(sql);//调用DBHelper
                dataGridView1.AutoGenerateColumns = false;//绑定数据
                dataGridView1.DataSource = dt;

                if (dt.Rows.Count > 0)
                {
                    
                    label2.Text = dt.Rows[0]["title"].ToString();
                    label4.Text = dt.Rows[0]["date"].ToString();
                    label6.Text = dt.Rows[0]["nums"].ToString();
                    label8.Text = dt.Rows[0]["mark"].ToString();
                    string image = dt.Rows[0]["image"].ToString();
                    pictureBox1.Image = imageList1.Images[image];
                }

            }
            catch (Exception)
            {

                throw;
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

3.1

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 T3Demo4;

namespace _3._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        private void NewMethod()
        {
            string sql = "select * from tb_book";
            dt = DBHelper.GetDataTable(sql);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ///窗体
            try
            {
                string sql = "select * from tb_book";
                dt = DBHelper.GetDataTable(sql);//调用DBHelper
                dataGridView1.AutoGenerateColumns = false;//绑定数据
                dataGridView1.DataSource = dt;
               
                DataView dv = new DataView(dt);
                //RowFilter筛选行

                textBox2.Text = dv[0]["BName"].ToString();
                textBox6.Text = dv[0]["BAuthor"].ToString();
                textBox4.Text = dv[0]["BSort"].ToString();
                textBox5.Text = dv[0]["BNum"].ToString();
                textBox1.Text = dv[0]["BContent"].ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        
        private void 删除书籍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Bid = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string sql = $"delete from tb_book where BID={Bid}";
            bool flag = DBHelper.ExecuteNonQuery(sql);
            DialogResult result = MessageBox.Show("确定删除", "删除", MessageBoxButtons.OKCancel);
            if (result==DialogResult.OK)
            {
                if (flag)
                {
                    MessageBox.Show("删除成功");

                }
            }
            
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        private void 刷新书籍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewMethod();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            DataView dv = new DataView(dt);
            //RowFilter筛选行
            dv.RowFilter = $"BID = {id}";
            textBox2.Text = dv[0]["BName"].ToString();
            textBox6.Text = dv[0]["BAuthor"].ToString();
            textBox4.Text = dv[0]["BSort"].ToString();
            textBox5.Text = dv[0]["BNum"].ToString();
            textBox1.Text = dv[0]["BContent"].ToString();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 功能菜单ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            //修改的话就是在数据库原有的基础上增添
            try
            {
                string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                
                string sql = $"update tb_book set BContent= '{textBox1.Text}'where BID='{id}'";
                bool tu = DBHelper.ExecuteNonQuery(sql);
                if (tu)
                {
                    MessageBox.Show("添加成功");
                    NewMethod();

                }
                else
                {
                    MessageBox.Show("添加失败");
                }

            }
            catch (Exception)
            {

                throw;
            }

        }

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值