DataTable table;
private SqlDataAdapter dataAdapter;
string connectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=.";
private void de3_Load(object sender, EventArgs e)
{
dataAdapter = new SqlDataAdapter("select * from Powerss", connectionString);//空表
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
table = new DataTable();
dataAdapter.Fill(table);
dataGridView1.DataSource = table;//提示作用
}
//保存
private void button3_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from Powers", connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);//这句很重要
DataSet ds_xls = new DataSet();
da.Fill(ds_xls);
DataTable dt = ds_xls.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
table.Rows.Add(dt.Rows[i].ItemArray);
Application.DoEvents();
}
dataAdapter.Update(table);//在此处更新数据
MessageBox.Show("提示操作成功。");
}
可应用在表的导入,只是导入(添加新数据),修改还在搞,自己关注中