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

使用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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路