2013年9月11日星期三

[ SqlDataAdapter.Update () ] on the use DataSet update

/ / /
/ / / add DataSet
/ / /

/ / / Oracle DataSet
/ / / tableName
public int AddDataSet (DataSet ds1, string tableName)
{
string sql = "select * from" + ; tableName;
int judge = 0;
using (SqlConnection connection = new SqlConnection (connectionString))
{
SqlDataAdapter da;
try
{
DataSet ds = ; new DataSet ();
connection.Open ();
da = new ; SqlDataAdapter (sql, connection);
SqlCommandBuilder sb = ; new SqlCommandBuilder (da);
da.InsertCommand = ; sb.GetInsertCommand ();
da.Fill (ds, "ds");
/ / Merge Oracle -Sql DataSet
ds.Merge (ds1) ;
judge = da . Update (ds, "ds");
ds.AcceptChanges ();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception ( ex.Message);
}
}
return judge;
}

Method Introduction : Data updating : the Oracle data updates to Sql Server. According to the same database table name ( same structure ) that reads from the oracle database to the DataSet and Sql Server DataSet ( no data ) for Merge () merge ( success ) , to get to update DataSet, and then use Update to update to Sql Server.. Front no problem,
judge = da.Update (ds, "ds");
not updated .
------ Solution -------------------------------- ------------
If you just insert the words .

In the Update before adding this two look .

ds.AcceptChanges ();
foreach (DataRow dr in ds.Tables [0 ]. Rows)
{
dr.SetAdded ();
}
da.Update (ds, "ds");

------ For reference only ---------------------------------- -----
artificial Sticky bit, went to take a nap , wake up at two o'clock hope to have good news.
------ For reference only ---------------------------------------
Results posted , thank floor , woke up to give me a surprise.

没有评论:

发表评论