qq_32071203 2016-01-18 01:22 采纳率: 50%
浏览 1994
已结题

使用VS2010写出程序实现增删改查

代码已经求人写好了,但是不会建项目 各位大神帮我调试好了发到我邮箱吧~
861236126@qq.com代码在下面图片说明

建立数据库的代码
CREATE TABLE [dbo].Table 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)
);

VS的代码

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

}

  • 写回答

5条回答 默认 最新

  • 把分全给哥 2016-01-18 01:28
    关注

    sql语句基础啊,select update insert

    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致