![!
单机button将datagridview中编辑的数据保存到SQL数据库指定表中
headertext没有关联数据库 因为我只要输入 不要读取
![!
单机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 { }
}
}
}
}