使用Ado.Net中的XML和DataSet更新表 [英] Update table using XML and DataSet in Ado.Net

查看:70
本文介绍了使用Ado.Net中的XML和DataSet更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在不使用任何insert或update命令的情况下更新表。我有一个与db的连接,表名也在那里。我必须读取一个xml文件并直接将其更新到表格。



Hi,
I wants to update a table without using any insert or update command. I have a connection to db and the table name is also there. I have to read an xml file and update it to table directly.

<Sample>
  <SampleTable>
    <OBRESessionID>OPSBRE.25C5A42D404A4437B6B3EA3D8522D982</OBRESessionID>
    <CurrentStep>1         </CurrentStep>
    <Status>Lab Scheduled Successfully</Status>
  </SampleTable>
  <SampleTable>
    <OBRESessionID>OPSBRE.4A2753AED7EA4B41A4B7FC9D9A086A38</OBRESessionID>
    <CurrentStep>1         </CurrentStep>
    <Status>Lab Scheduled Successfully</Status>
  </SampleTable>
  <SampleTable>
    <OBRESessionID>OPSBRE.3B531367D5C04D139E86056C94D428A4</OBRESessionID>
    <CurrentStep>5         </CurrentStep>
    <Status>Cleanup Success</Status>
  </SampleTable>
</Sample>





这是我的xml文件,我想更新如下:





This is my xml file and i want to update this like:

myDataSet->ReadXml("G:/data.xml");
 myDataAdapter->Update(myDataSet,"LabOpsStatus");





有没有办法做到这一点。



Is there any way to do this.

推荐答案

这不是真正的预期方式但是你可以通过 http://msdn.microsoft.com/en获得一段距离。 -us / library / fks3666w(v = vs.110).aspx [ ^ ]



但至少你必须指定select命令和任何一个之前的主键都可以工作
That's not really the intended way but you can get a distance with http://msdn.microsoft.com/en-us/library/fks3666w(v=vs.110).aspx[^]

But at the very least you will have to specify the select command and also the primary key before any of it will work


mySQLConnection = new SqlConnection(S"Data Source=(local);Initial Catalog=myDb;User Id=user;Password=pw;");
mySQLConnection->Open();

myDataAdapter = new SqlDataAdapter(S"select * from [SampleTable]",mySQLConnection);

myDataSet = new DataSet("Sample");

myDataSet->ReadXml("G:/data.xml");

SqlCommandBuilder *myBuilder = new SqlCommandBuilder( myDataAdapter );
myDataAdapter->Update(myDataSet,"SampleTable");







这就是我现在正在做的事情。在阅读xml和更新之前,我必须提供选择查询。有没有办法避免这种选择查询??




This is what i am doing now. I would have to provide a selection query, before reading the xml and update. Is there any way to avoid this selection query??


首先在sql中创建一个与数据集具有相同列的Type表有\



语法

创建类型演示为表格

(coluname





在sql中创建一个程序



创建proc updatdate

@data demo readonly



as

开始

1)方式

从表名中删除

然后

插入表名

从@data中选择辣烛栏



执行以上程序



现在来自.net



Sqlcommand cmd = new Sqlcommand(updatdate,con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.ParameterWithValue(@ data,ds.Tables [urtable name]);

cmd.Executenonquery();
first create one Type table in sql which have same column as your dataset have \

syntax
create type demo as table
(coluname
)

create one procedure in sql

create proc updatdate
@data demo readonly

as
begin
1) way
delete from tablename
then
insert into tablename
select spicycolumn from @data

excute above procedure

now from .net

Sqlcommand cmd=new Sqlcommand("updatdate",con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.ParameterWithValue("@data",ds.Tables[urtable name"]);
cmd.Executenonquery();


这篇关于使用Ado.Net中的XML和DataSet更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆