qq_32071203
qq_32071203
2016-01-15 07:54
采纳率: 50%
浏览 3.5k

使用VS2010写出程序并连接数据库实现增删改功能

照着图片做出一个程序实现增删改功能

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • caozhy
    已采纳

    这还不简单,就是datagridview控件一个,再放一个dataadapter,都不需要什么代码,配置配置控件就完了。如果你需要我帮你,先采纳了,写给你。

    点赞 评论
  • caozhy
     CREATE TABLE [dbo].[Table] (
        [工号]   INT            IDENTITY (1, 1) NOT NULL,
        [姓名]   NVARCHAR (20)  NOT NULL,
        [性别]   BIT            NOT NULL,
        [出生日期] DATETIME       NOT NULL,
        [工作年限] INT            NOT NULL,
        [电话号码] NVARCHAR (20)  NOT NULL,
        [家庭地址] NVARCHAR (100) NOT NULL,
        PRIMARY KEY CLUSTERED ([工号] ASC)
    );
    
    
    点赞 2 评论
  • sky_blue_flying
    sky_flying對滴 2016-01-15 08:58
  • sky_blue_flying
    sky_flying對滴 2016-01-15 08:59
    点赞 评论
  • caozhy
     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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                foreach (string s in "工号,姓名,性别,出生日期,工作年限,电话号码,家庭地址".Split(','))
                    dataGridView1.Columns.Add(s, s);
                LoadList();
            }
    
            private void LoadList()
            {
                db1Entities db = new db1Entities();
                dataGridView1.Rows.Clear();
                foreach (var item in db.Tables)
                    dataGridView1.Rows.Add(item.工号, item.姓名, item.性别 ? "男" : "女", item.出生日期, item.工作年限, item.电话号码, item.家庭地址);
            }
    
            private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
            {
    
                db1Entities db = new db1Entities();
                if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && db.Tables.ToList().Any(x => x.工号 == int.Parse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString())))
                {
                    var t = db.Tables.ToList().Single(x => x.工号 == int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
                    t.姓名 = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    t.性别 = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() == "男";
                    t.出生日期 = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
                    t.工作年限 = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                    t.电话号码 = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                    t.家庭地址 = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
                }
                else
                {
                    if (dataGridView1.Rows[e.RowIndex].Cells[1].Value == null) return;
                    var t = new Table();
                    t.姓名 = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    t.性别 = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() == "男";
                    t.出生日期 = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
                    t.工作年限 = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                    t.电话号码 = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                    t.家庭地址 = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
                    db.Tables.Add(t);
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = t.工号.ToString();
                }
                db.SaveChanges();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                dataGridView1.Rows.Add();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                db1Entities db = new db1Entities();
                var t = db.Tables.ToList().Single(x => x.工号 == int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
                db.Tables.Remove(t);
                db.SaveChanges();
                dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                db1Entities db = new db1Entities();
                var t = db.Tables.ToList().Single(x => x.工号 == int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
                t.姓名 = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                t.性别 = dataGridView1.CurrentRow.Cells[2].Value.ToString() == "男";
                t.出生日期 = DateTime.Parse(dataGridView1.CurrentRow.Cells[3].Value.ToString());
                t.工作年限 = int.Parse(dataGridView1.CurrentRow.Cells[4].Value.ToString());
                t.电话号码 = dataGridView1.CurrentRow.Cells[5].Value.ToString();
                t.家庭地址 = dataGridView1.CurrentRow.Cells[5].Value.ToString();
                db.SaveChanges();
            }
        }
    }
    
    
    点赞 评论
  • caozhy

    图片说明

    点赞 评论

相关推荐