Insert data from a Dataset into a SQL Database

R

Ralph Hanan

Hi guys my name is Ralph, i am kinda new to this so please bear with
me.
I have written a .net web service which accepts a stream of data
see relevent source code).

Public Function PutBondInfo(ByVal data As String) As DataSet
Try
Dim zh As New ZipHelper
Dim sz As String
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As New DataSet
Dim xSet As New DataSet
Dim sSql As String

Dim mySerializer As XmlSerializer = New
XmlSerializer(ds.GetType())

cn = New
SqlConnection("server=HLSQL;uid=test;pwd=;database=test")
cn.Open()
da = New SqlDataAdapter("SELECT * FROM BondStatus", cn)
'da.TableMappings.Add("BondStatus", "NewDataSet")

sz = zh.Decompress(data)
zh = Nothing

da.FillSchema(ds, SchemaType.Mapped, "BondStatus")
'da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(ds, "BondStatus")

Dim st As New MemoryStream(Encoding.UTF8.GetBytes(sz))
' Create the object from the xml file
xSet = CType(mySerializer.Deserialize(st), DataSet)
xSet.Tables(0).TableName = ds.Tables(0).TableName

ds.Merge(xSet, False, System.Data.MissingSchemaAction.Add)

'ds.AcceptChanges()

Dim SqlCB As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Update(ds, "BondStatus")

st.Close()
da.Dispose()
SqlCB.Dispose()
cn.Close()

Return ds
Catch ex As Exception
ex.Message().ToString()
End Try

End Function

This data is compressed.I decompress the data and receive the xml
which is then
deserialized into a dataset. this part works great no problems. My
problem is then taking that dataset and pushing the data into a table
on my SQL server. The elements in the xml and the field names of my
SQL table 'BondStatus'
are exactly the same. The table names are different the xml has
'Table' as its table name and so on. As you can see from the code I
have tried table mappings, assigns, merges etc with no luck.

Ladies and gents if anyone has a clue as to what I am doing and can
help me, it would be appreciated.

Thanks in advance
Ralph
 
A

Andrew Conrad

The simplest way to solve this is to just rename the table after load and
before update:

dataSet.Tables[0].TableName = "XXXXXX";

It also appears that you incorrectly specify the DataTableMapping - it
should include the source table and the dataset table:

da.TableMappings.Add("BondStatus", "Table")

Andrew Conrad
Microsoft Corp
http://blogs.msdn.com/aconrad/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top