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

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

图片说明

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

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

http://download.csdn.net/detail/sky_blue_flying/9405690这是我刚上传的一个例子,和你的不太一样。不过是简单的数据库操作,你可以看一下代码。

http://download.csdn.net/detail/sky_blue_flying/9405690

 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();
        }
    }
}

图片说明