private SqlDataAdapter sqlAdapter; public DataSetService() { InitializeComponent();
// declare Command builder SqlCommandBuilder builder; // select statement string sql = "SELECT * FROM titles"; string connstr = "user id=sa;password=;database=pubs;server=localhost"; sqlAdapter = new SqlDataAdapter(sql, connstr); builder = new SqlCommandBuilder(sqlAdapter); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { } #endregion /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { } [WebMethod()] public DataSet GetTitles() { DataSet ds = new DataSet(); // queries the data base and fills the data set sqlAdapter.Fill(ds, "Titles"); return ds; } [WebMethod] public void UpdateTitles(DataSet ds) { // we call update if there are changes made // to the data set if (ds.HasChanges()) sqlAdapter.Update(ds, "Titles"); } 你看看,这样没问题
private void button1_Click(object sender, System.EventArgs e) { //Declare variables localhost.DataSetService service; DataSet ds; //Create web service proxy service = new localhost.DataSetService(); //Call web service ds = service.GetTitles(); //Display result dataGrid1.DataSource = ds; } private void button2_Click(object sender, System.EventArgs e) { //Declare variables localhost.DataSetService service; DataSet ds; //Get data set from grid ds = (DataSet)dataGrid1.DataSource; //Create web service proxy service = new localhost.DataSetService(); //Call web service service.UpdateTitles(ds); //Mark changes as updated ds.AcceptChanges(); }
第一个是ws 第二个是winform的更新
其实这是一个B/S结构的程序,在WebApplication端,我生成了一个DataSet,然后调用一个WebService的DataSetUpdate方法去更新数据库。 例如: Web端调用了一个WebService把 select field_a, field_b from table where id=5; 的执行结果返回到页面中的一个CheckList中去,当我修改了CheckList中的内容后,又调用DataSetUpdate将改动结果存储到数据库中,主要过程就是这样。 别忘了Web和数据库是没有连接的,我在WebService和WebApplication之间是通过DataSet传递的,实际返回到WebService的DataSet是在WebApplication中动态生成的,它的内容是根据CheckList中的内容一行一行加入的,所以所有的行的RowState都应该是Added的。但更新到数据库时并不是这样应该有部分行可能是Moidfied或者Deleted的,我也可以变通一下,先将数据库中所有的id=5的行都删掉,那么所有行都是Added的就合乎逻辑了。 问题是这个DataSet如何同SqlAdapater建立联系,请指教
如果你直接把控件放到窗体上。他会帮你建立许多的东西。你可以看看
包括。insertcommand ,updatecommand, deletecommand, selectcommand还有sqlconnection等等。。这些都需要设置上,并且像insertcommand等里面的sql语句也需要写全
sqldataadapter才能够好使的。
不然的话。你只能用sqlcommand自己写update语句,然后再更新。
{
string strSql = "select * from " + ds.Tables[0].TableName;
SqlDataAdapter da = new SqlDataAdapter(strSql, sqlConnection);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
int updateResult = 0; try
{
sqlConnection.Open();/* da.TableMappings.Add(ds.Tables[0].TableName, ds.Tables[0].TableName);
foreach(DataColumn col in ds.Tables[0].Columns)
{
da.TableMappings[0].ColumnMappings.Add(col.ColumnName, col.ColumnName);
}*/ updateResult = da.Update(ds);
return "";
}
catch (Exception e)
{
return e.Message;
}
finally
{
sqlConnection.Close();
}
}
你的da没有查询语句,
建议填充和更新用同一个SqlDataAdapter,或者有同一数据的查询!
{
InitializeComponent();
// declare Command builder
SqlCommandBuilder builder; // select statement
string sql = "SELECT * FROM titles"; string connstr =
"user id=sa;password=;database=pubs;server=localhost"; sqlAdapter = new SqlDataAdapter(sql, connstr); builder = new SqlCommandBuilder(sqlAdapter);
} #region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
} [WebMethod()]
public DataSet GetTitles()
{
DataSet ds = new DataSet();
// queries the data base and fills the data set
sqlAdapter.Fill(ds, "Titles");
return ds;
} [WebMethod]
public void UpdateTitles(DataSet ds)
{
// we call update if there are changes made
// to the data set
if (ds.HasChanges())
sqlAdapter.Update(ds, "Titles");
}
你看看,这样没问题
{
//Declare variables
localhost.DataSetService service;
DataSet ds; //Create web service proxy
service = new localhost.DataSetService(); //Call web service
ds = service.GetTitles(); //Display result
dataGrid1.DataSource = ds;
} private void button2_Click(object sender, System.EventArgs e)
{
//Declare variables
localhost.DataSetService service;
DataSet ds; //Get data set from grid
ds = (DataSet)dataGrid1.DataSource; //Create web service proxy
service = new localhost.DataSetService(); //Call web service
service.UpdateTitles(ds); //Mark changes as updated
ds.AcceptChanges();
}
第二个是winform的更新
例如:
Web端调用了一个WebService把
select field_a, field_b from table where id=5;
的执行结果返回到页面中的一个CheckList中去,当我修改了CheckList中的内容后,又调用DataSetUpdate将改动结果存储到数据库中,主要过程就是这样。
别忘了Web和数据库是没有连接的,我在WebService和WebApplication之间是通过DataSet传递的,实际返回到WebService的DataSet是在WebApplication中动态生成的,它的内容是根据CheckList中的内容一行一行加入的,所以所有的行的RowState都应该是Added的。但更新到数据库时并不是这样应该有部分行可能是Moidfied或者Deleted的,我也可以变通一下,先将数据库中所有的id=5的行都删掉,那么所有行都是Added的就合乎逻辑了。
问题是这个DataSet如何同SqlAdapater建立联系,请指教
oleDbCommand1=new OleDbCommand(updatestring,oleDbConnection1);oleDbDataAdapter1.Update(dateset);
oleDbDataAdapter1.fill(dataset);在upstring中一定要有主健标志