datagridview如何批量更新数据并保存到数据库
我在两个项目里面用同一种方法把datagridview里修改的数据保存更新,一个可以,另一个总是只更新了部分数据,找了好久也知道是什么原因,望高手指点一下。我把我的方法贴出来.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using DataAccess;
using DataBusiness;
namespace savaDataGridView
{
public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private SqlDataAdapter da = new SqlDataAdapter();
public Form1()
{
InitializeComponent();
}
private void bindData()
{
try
{
DataTable dt = EmployeeDAO.getAllEmployees();
if (dt != null)
{
this.dataGridView1.DataSource = dt.DefaultView;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetData(string sql)
{
try
{
string sqlConnection = @"Data Source=CHINA-1FF0245C3\SQLEXPRESS;Initial Catalog=Equipment_Manage;Integrated Security=True";
da=new SqlDataAdapter(sql,sqlConnection);
SqlCommandBuilder commandBuild=new SqlCommandBuilder(da);
DataTable dt=new DataTable();
dt.Locale=System.Globalization.CultureInfo.InvariantCulture;
da.Fill(dt);
bindingSource.DataSource=dt;
}
catch
{
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.dataGridView1.DataSource = bindingSource;
GetData("select * from dbo.Employee");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
da.Update((DataTable)bindingSource.DataSource);
MessageBox.Show("数据保存成功!");
}
}
}