提供一个数据服务类,调用GetDataSet就得到需要的DataSet,然后在用户界面进行修改,最后调用SaveDate就可以把未定的更改保存到数据库。
using System;
using System.Data ;
using System.Data.SqlClient ;
namespace AsterDnet.DataBind
{
/// <summary>
/// Summary description for GetData.
/// </summary>
public class TestData
{
private SqlDataAdapter da;
public TestData()
{
//
// TODO: Add constructor logic here
//
}
public DataSet GetDataSet()
{
string strConn="data source=ASTERDNET;initial catalog=Test;integrated security=SSPI;persist security info=False;user id=sa;workstation id=ASTERDNET;packet size=4096";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
DataSet ds=new DataSet();
SqlParameter workParam=new SqlParameter();
da=new SqlDataAdapter();
//Build the select Command
da.SelectCommand =new SqlCommand("select * from Education ",conn);
da.Fill(ds,"Education");
// Build the insert Command
da.InsertCommand = new SqlCommand("Insert into Education (ID, Education) VALUES (@ID, @Education)", conn);
workParam = da.InsertCommand.Parameters.Add("@ID", SqlDbType.Int);
workParam.SourceColumn = "ID";
workParam.SourceVersion = DataRowVersion.Current;
workParam = da.InsertCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
workParam.SourceVersion = DataRowVersion.Current;
workParam.SourceColumn = "Education";
// Build the update command
da.UpdateCommand = new SqlCommand("Update Education Set Education = @Education WHERE ID = @ID" , conn);
workParam = da.Update