代码如下 帮帮忙!
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;
using System.Data.SqlClient;
namespace gdvForm
{
public partial class Gdv : Form
{
//数据库连接字符串
public static string ConnString="Data Source=.;Initial Catalog=Student;Integrated Security=True";
//数据集
DataSet ds = new DataSet("Test");
//适配器对象dap
SqlDataAdapter dap = null;
public Gdv()
{
InitializeComponent();
}
private void Gdv_Load(object sender, EventArgs e)
{
//数据库连接对象
SqlConnection conn = new SqlConnection(ConnString);
//数据库语句
string sql = "SELECT * FROM Test";
//dap适配器对象
dap = new SqlDataAdapter(sql,conn);
//命令对象
SqlCommandBuilder builder = new SqlCommandBuilder(dap);
//添加数据到数据集
dap.Fill(ds,"Test");
this.dgv_show.DataSource=ds.Tables["Test"];
}
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
dap.Update(ds,"Test");
}
private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
{
ds.Clear();
dap.Fill(ds, "Test");
this.dgv_show.DataSource = ds.Tables["Test"];
}
}
}