请问dataset中dataTable数据怎么进行修改?
我现在想到的方法是循环访问dataTable中的行,若行中的标识列满足条件,该列数据更新。
如何将dataset中dataTable数据更新到数据库? 这里是要批量更新数据库表数据。
我的思路:循环dataTable将里面的数据一行一行更新到数据库表中。
我感觉这些方法都不好,不清楚有没有方便高效的方法。
我现在想到的方法是循环访问dataTable中的行,若行中的标识列满足条件,该列数据更新。
如何将dataset中dataTable数据更新到数据库? 这里是要批量更新数据库表数据。
我的思路:循环dataTable将里面的数据一行一行更新到数据库表中。
我感觉这些方法都不好,不清楚有没有方便高效的方法。
public static void BatchUpdate(DataTable dataTable,Int32 batchSize)
{
// Assumes GetConnectionString() returns a valid connection string.
string connectionString = GetConnectionString();
// Connect to the AdventureWorks database.
using (SqlConnection connection = new
SqlConnection(connectionString))
{
// Create a SqlDataAdapter.
SqlDataAdapter adapter = new SqlDataAdapter();
// Set the UPDATE command and parameters.
adapter.UpdateCommand = new SqlCommand(
"UPDATE Production.ProductCategory SET "
+ "Name=@Name WHERE ProductCategoryID=@ProdCatID;",
connection);
adapter.UpdateCommand.Parameters.Add("@Name",
SqlDbType.NVarChar, 50, "Name");
adapter.UpdateCommand.Parameters.Add("@ProdCatID",
SqlDbType.Int, 4, "ProductCategoryID");
adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
// Set the INSERT command and parameter.
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Production.ProductCategory (Name) VALUES (@Name);",
connection);
adapter.InsertCommand.Parameters.Add("@Name",
SqlDbType.NVarChar, 50, "Name");
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
// Set the DELETE command and parameter.
adapter.DeleteCommand = new SqlCommand(
"DELETE FROM Production.ProductCategory "
+ "WHERE ProductCategoryID=@ProdCatID;", connection);
adapter.DeleteCommand.Parameters.Add("@ProdCatID",
SqlDbType.Int, 4, "ProductCategoryID");
adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
// Set the batch size.
adapter.UpdateBatchSize = batchSize;
// Execute the update.
adapter.Update(dataTable);
}
批量更新
System.Data.DataTable dt = new DataTable();
d.Fill(dt);
dt.Rows[0][0] = "aaa";
d.Update(dt);
string queryString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(queryString, connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); connection.Open(); DataSet customers = new DataSet();
adapter.Fill(customers); //code to modify data in dataset here adapter.Update(customers); return customers;
}
}参考http://msdn.microsoft.com/zh-cn/library/at8a576f(VS.80).aspx
DataSet ds = new DataSet();
d.Fll(ds,"表名");
ds["表名"].Rows[0] = "test";
d.Update(ds,"表名");
这个应该可以实现批量插入的操作,这个queryString 的插入语句怎么写?
dataset中数据修改问题还没有找到好的方法dataset中的
dataTable.Rows.Find()这个需要主键。
SqlDataAdapter ad=new SqlDataAdapter(selectCommand参数);
ad.Fill(Set);//填充SetSqlCommandBuilder builder=new SqlCommandBuilder(ad);//不要丢了,只能用在DataTable中的数据来自一个表的场合Set.Tables[0].Rows[0]["ID"]="0x1234";//更改内存数据ad.Update(Set);//更新到物理数据库
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from Tb", conn);
SqlCommandBuilder Builder = new SqlCommandBuilder(sda);sda.Fill(ds);
DataTable dt = ds.Tables[0];
ds.Dispose();
sda.Dispose();
conn.Close();}