二个三四五

表类

 public class Course
    {
        private int cid;
        private string cno;
        private string cname;
        private double credit;
        private int period;
        public int Cid
        {
            set { this.cid = value; }
            get { return this.cid; }
        }
        public string Cno
        {
            set { this.cno = value; }
            get { return this.cno; }
        }
        public string Cname
        {
            set { this.cname = value; }
            get { return this.cname; }
        }
        public double Credit
        {
            set { this.credit = value; }
            get { return this.credit; }
        }
        public int Period
        {
            set { this.period = value; }
            get { return this.period; }
        }

        public Course()
        {
        }
        public Course(int cid, string cno, string cname, double credit, int period)
        {
            this.cid = cid;
            this.cno = cno;
            this.cname = cname;
            this.credit = credit;
            this.period = period;
        }

    }

sql类方法

   public class SqlHelper
    {
        public static string connStr = "Data Source=.; Initial Catalog=studentdb;User ID=sa;Password=xiaobao";
        public static int ExecuteNonQuery(string sqlStr, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(connStr))///建立连接通道
            {
                conn.Open();///打开连接通道
                using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
                {
                        if (para != null)
                        {
                            foreach (SqlParameter p in para)
                            {
                                cmd.Parameters.AddWithValue(p.ParameterName, p.Value);
                            }
                        }
                        return cmd.ExecuteNonQuery();
                }
            }
        }
        public static DataSet ExecuteQuery(string sqlStr, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(connStr))///建立连接通道
            {
                using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
                {

                    if (para != null)
                    {
                        foreach (SqlParameter p in para)
                        {
                            cmd.Parameters.AddWithValue(p.ParameterName, p.Value);
                        }
                    }
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    return ds;
                }
            }
        }

    form1

 

 

 添加。查询。删除。修改按钮后面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(2,null);
            frm.ShowDialog();

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr= dataGridView1.CurrentRow;
            if (dr == null) 
            {
                MessageBox.Show("请选择要修改的数据");
                return;
            }
            //建立一个课程对象将DataGridViewRow里面的值赋值给课程对象
            Course course = new Course();
            course.Cid = int.Parse(dr.Cells["Cid"].Value.ToString());
            course.Cno = dr.Cells["cno"].Value.ToString();
            course.Cname = dr.Cells["cname"].Value.ToString();
            course.Credit = double.Parse(dr.Cells["credit"].Value.ToString());
            course.Period = int.Parse(dr.Cells["period"].Value.ToString());
            //跳转到form2并传值过去
            Form2 frm = new Form2(1,course);
            frm.ShowDialog();
        }
        /// <summary>
        /// 查找数据并绑定到dataGridView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            
            string sqlStr = "select * from tb_course";
            DataSet ds = SqlHelper.ExecuteQuery(sqlStr);
            if (ds != null && ds.Tables.Count > 0)
            {
                dataGridView1.DataSource=ds.Tables[0];
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dataGridView1.CurrentRow;
            if (dr == null)
            {
                MessageBox.Show("请选择要修改的数据");
                return;
            }
            Course course = new Course();
            int cid = int.Parse(dr.Cells["Cid"].Value.ToString());
            string sqlStr = "delete from tb_course where cid=@cid";
            SqlParameter para = new SqlParameter("@cid", cid);
            if (SqlHelper.ExecuteNonQuery(sqlStr, para) > 0)
            {
                MessageBox.Show("删除成功!");
                
            }
        }
    }
}

form2

 

添加按钮后面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        int _type;
        public Form2(int type,Course course)///构造函数复用Form2
        {
            InitializeComponent();
            if (type == 1) 
            {
                button1.Text = "修改";
            }
            this._type = type;
            if (course != null)
            {
                txtcid.Text = course.Cid.ToString();
                txtcno.Text=course.Cno;
                txtcname.Text=course.Cname;
                txtcredit.Text=course.Credit.ToString();
                txtperiod.Text=course.Period.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_type == 1)
            {
                
                
                string sqlStr = "update tb_course set cno=@cno,cname=@cname,credit=@credit,period=@period where cid=@cid";
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@cid",txtcid.Text),
                    new SqlParameter("@cno",txtcno.Text),
                    new SqlParameter("@cname",txtcname.Text),
                    new SqlParameter("@credit",txtcredit.Text),
                    new SqlParameter("@period",txtperiod.Text)
                };
                if (SqlHelper.ExecuteNonQuery(sqlStr, para) > 0)
                {
                    MessageBox.Show("修改成功!");
                    this.DialogResult = DialogResult.OK;
                }
            }
            else 
            {
                string sqlStr = "insert into tb_course (cno,cname,credit,period) values(@cno,@cname,@credit,@period)";
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@cno",txtcno.Text),
                    new SqlParameter("@cname",txtcname.Text),
                    new SqlParameter("@credit",txtcredit.Text),
                    new SqlParameter("@period",txtperiod.Text)
                };
                if (SqlHelper.ExecuteNonQuery(sqlStr, para)>0)
                {
                    MessageBox.Show("添加成功!");
                    this.DialogResult = DialogResult.OK;
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值