C# 怎么把datagridview编辑的数据怎么存到数据库,如下图 最好给代码

图片说明![图片说明!图片说明

单机button将datagridview中编辑的数据保存到SQL数据库指定表中
headertext没有关联数据库 因为我只要输入 不要读取

 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;

namespace Q691582
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                try
                {
                    tPeople p = new tPeople()
                    {
                        姓名 = item.Cells[0].Value.ToString(),
                        工号 = item.Cells[1].Value.ToString(),
                        邮箱 = item.Cells[2].Value.ToString()
                    };
                    if (db.tPeoples.Any(x => x.工号 == p.工号))
                    {
                        var p1 = db.tPeoples.First(x => x.工号 == p.工号);
                        p1.姓名 = p.姓名;
                        p1.邮箱 = p.邮箱;
                    }
                    else
                    {
                        db.tPeoples.InsertOnSubmit(p);
                    }
                    db.SubmitChanges();
                }
                catch { }
            }
        }
    }
}

采纳了给你源代码,数据库是access的,sqlserver只需要更改对象和驱动就可以实现兼容
图片说明

图片说明

我的代码已经上传,满意请下载:https://download.csdn.net/download/dabocaiqq/10468711

图片说明

图片说明

图片说明