电影记录管理系统2[增删改查]

来源:互联网 发布:怎样参加淘宝天天特价 编辑:程序博客网 时间:2024/06/05 22:46

 

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

namespace 电影记录管理系统
{
    public partial class FrmManager : Form
    {
        public FrmManager()
        {
            InitializeComponent();
        }

        string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //添加数据
            //定义一个数据n=0,用于判断后期是否成功插入数据
            int n = 0;
            string sql = "insert into Record1(Movie_Name,Movie_Director,Date_Released) values(@Movie_Name,@Movie_Director,@Date_Released)";
            //判断插入的数据是否为空,如果为空,则提示重新插入
            if(txtMovie.Text.Trim()==""||txtDirector.Text.Trim()==""||txtDate.Text.Trim()=="")
            {
                MessageBox.Show("插入数据不能为空,请按要求插入数据!");
                return;
            }
            //向数据插入参数
            SqlParameter[] param =
            {
            new SqlParameter("@Movie_Name",txtMovie.Text),
            new SqlParameter("@Movie_Director",txtDirector.Text),
            new SqlParameter("@Date_Released",Convert.ToDateTime(txtDate.Text))
            };
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            cmd.Parameters.AddRange(param);
            n = cmd.ExecuteNonQuery();
            if(n==0)
            {
                MessageBox.Show("添加失败!");
            }
            else if(n>0)
            {
                MessageBox.Show("添加成功!");
            }
            conn.Close();
            //调用refresh方法,在添加完成数据后 自动刷新并显示数据
            Refresh();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //使用sql删除语句
            string sql = "delete from Record1 where 1=1";
            //如果datagridview的当前行被选中
            if (dgvManager.CurrentRow.Selected)
            {
                //将sql语句 delete from Record where 1=1 + and Id = + 当前选中行的第0个单元格的号码(即Id号)
                sql = sql + "and Id=" + Convert.ToInt32(dgvManager.CurrentRow.Cells[0].Value.ToString());
            }
            int n = 0;
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            n = cmd.ExecuteNonQuery();
            if (n == 0)
            {
                MessageBox.Show("不存在的ID!");
                return;
            }
            else if (n > 0)
            {
                MessageBox.Show("删除成功!");
            }
            conn.Close();
            //删除完后 刷新一下当前数据
            Refresh();
        }   


        private void dgvManager_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            //通过单元格内字段"Movie_Name"来获取某单元格的内容 并将其传递给 txtMovie
            txtMovie.Text = dgvManager.Rows[e.RowIndex].Cells["Movie_Name"].Value.ToString();
            //同理 获取当前点击行里的 name属性为Movie_Director的单元格 获取并将其传至txtDirector 文本框
            txtDirector.Text = dgvManager.Rows[e.RowIndex].Cells["Movie_Director"].Value.ToString();
            //new一个时间对象 目的是将电影发行时间的小时,分和秒给去掉 保留到最小单位为日
            DateTime datetoDay = new DateTime().Date;
            //将当前行的日期单元格的值 赋给 时间对象datetoDay
            datetoDay = Convert.ToDateTime(dgvManager.Rows[e.RowIndex].Cells["Date_Released"].Value);
            //通过ToShortDateString()方法 将日期后的00:00:00 给剔除掉 并赋给 txtDate文本框
            txtDate.Text = datetoDay.ToShortDateString();


        }

      

        private void btnSave_Click(object sender, EventArgs e)
        {
          
       if(txtMovie.Text.Trim()==""||txtDirector.Text.Trim()==""||txtDate.Text.Trim()=="") 
        { 
                MessageBox.Show("文本框的输入不能为空!"); 
                return; 
       } 
        //使用SQL update 更新语句 
        //获取文本框中输入的内容, 通过Id进行更新(Id为当前鼠标点击行的Id) 
        string sqlUpdate = "update Record1 set Movie_Name ='" + txtMovie.Text + "',Movie_Director ='"  
         + txtDirector.Text + "',Date_Released='" + txtDate.Text +  
        "'where Id='"+dgvManager.CurrentRow.Cells[0].Value.ToString()+"'";  
        SqlConnection conn = new SqlConnection(connStr); 
        SqlCommand cmdUpdate = new SqlCommand(sqlUpdate,conn); 
        conn.Open(); 
        int n = cmdUpdate.ExecuteNonQuery(); 
        if(n==0) 
        { 
            //提示更新失败 
            MessageBox.Show("更新失败!"); 
            return;// 并且返回 
        } 
        else if(n>0) 
        {   
            //否则更新成功 
            MessageBox.Show("恭喜你!更新成功!"); 
        } 
        //执行完数据更新操作后 需要关闭数据库 节省资源
              conn.Close(); 
        //更新完以后  调用刷新方法,将更新后的数据 显示在datagridview上面 
        Refresh(); 


        }
        private void btnView_Click(object sender, EventArgs e)
        {
            string sql = "select Id,Movie_Name,Movie_Director,Date_Released from Record1";
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            dgvManager.DataSource = dt;

        }
      
    }
}

原创粉丝点击