DbDataAdapter 的Fill(DataTable dataTable) 和 Update(DataSet dataSet);方法的使用
2014-02-24 17:32
483 查看
DbDataAdapter 的Fill(DataTable dataTable) 和 Update(DataSet dataSet);方法的使用
方法定义:
public int Fill(DataTable dataTable);
在 System.Data.DataSet 的指定范围中添加或刷新行,以与使用 System.Data.DataTable 名称的数据源中的行匹配。
public override int Update(DataSet dataSet);
为指定 System.Data.DataTable 中每个已插入、已更新或已删除的行调用相应的 INSERT、UPDATE 或 DELETE 语句。
下面我们先使用Fill(DataTable dataTable);方法把数据从数据库中提取到DataSet中,数据修改之后,我们可以再使用Update方法,将我们对DataSet
中数据的修改提交至数据库.
示例代码如下
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(PubConstant.ConnectionString))
{
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter("SELECT * FROM [jarlinfowkk].[dbo].[oSensorData1]", connection);
command.Fill(ds);
DataTable table = ds.Tables[0];
int count = table.Rows.Count;
for (int s = 0; s < table.Columns.Count; s++)
{
DataRow row = table.Rows[s];
DataRow newRow = table.NewRow();
for (int k = 0; k < table.Columns.Count - 1; k++)
{
newRow[k] = row[k];
}
DateTime dt = DateTime.Parse(row["pAcquisitionTime"].ToString());
newRow["pAcquisitionTime"] = dt.AddYears(3);
//修改 DataSet数据集
ds.Tables[0].Rows.Add(newRow);
}
//因为我们只是插入了数据,所以只需要生成InsertCommand,否则 还需要 UpdateCommand ,DeleteCommand
//由SqlCommandBuilder为我们生成 InsertCommand
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(command);
command.InsertCommand = myCommandBuilder.GetInsertCommand();
command.Update(ds);
connection.Close();
MessageBox.Show("OK");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
方法定义:
public int Fill(DataTable dataTable);
在 System.Data.DataSet 的指定范围中添加或刷新行,以与使用 System.Data.DataTable 名称的数据源中的行匹配。
public override int Update(DataSet dataSet);
为指定 System.Data.DataTable 中每个已插入、已更新或已删除的行调用相应的 INSERT、UPDATE 或 DELETE 语句。
下面我们先使用Fill(DataTable dataTable);方法把数据从数据库中提取到DataSet中,数据修改之后,我们可以再使用Update方法,将我们对DataSet
中数据的修改提交至数据库.
示例代码如下
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(PubConstant.ConnectionString))
{
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter("SELECT * FROM [jarlinfowkk].[dbo].[oSensorData1]", connection);
command.Fill(ds);
DataTable table = ds.Tables[0];
int count = table.Rows.Count;
for (int s = 0; s < table.Columns.Count; s++)
{
DataRow row = table.Rows[s];
DataRow newRow = table.NewRow();
for (int k = 0; k < table.Columns.Count - 1; k++)
{
newRow[k] = row[k];
}
DateTime dt = DateTime.Parse(row["pAcquisitionTime"].ToString());
newRow["pAcquisitionTime"] = dt.AddYears(3);
//修改 DataSet数据集
ds.Tables[0].Rows.Add(newRow);
}
//因为我们只是插入了数据,所以只需要生成InsertCommand,否则 还需要 UpdateCommand ,DeleteCommand
//由SqlCommandBuilder为我们生成 InsertCommand
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(command);
command.InsertCommand = myCommandBuilder.GetInsertCommand();
command.Update(ds);
connection.Close();
MessageBox.Show("OK");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
相关文章推荐
- 数据库链接字符串查询网站
- 我是运营,我没有假期
- DB2实例管理
- DB2实例管理
- mysql问答汇集
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- DB2数据库的安装
- “传奇”图象数据存储方式
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- sql2008 还原数据库解决方案
- Oracle 数据库自动存储管理-安装配置
- Oracle 数据库 临时数据的处理方法
- 最近比较流行的数据库挂马
- 修复mysql数据库
- 重装主控服务器后,数据库连接失败的解决办法:请正确还原数据库。
- 九种防MDB数据库被下载的方法小结