bài tập kỹ thuật load XML vào dataset và insert vào database doc - Pdf 20

đặt ra 1 bài toán : bạn được bên A giao cho dữ liệu của 1 bảng (User)
bằng XML và yêu cầu bạn Insert toàn bộ dữ liệu đó vào DB của mình.
Mình sẽ giải quyết như sau:

file.xml
<newForumAsh>
<user>
<userid>1</userid>
<username>admin</username>
<password>3349311e8bdb5b11d9f49ff2047e5631</password>
<email></email>
</user>
<user>
<userid>2</userid>
<username>user12</username>
<password>e493716f73d8e1c410bfbfe3e910d3af</password>
<email></email>
</user>
<user>
<userid>3</userid>
<username>user1</username>
<password>f65696f9d46fa5b8c70b2e20c045cdbf</password>
<email></email>
</user>
</newForumAsh>

trong trang aspx bạn tạo 1 form upload cho người dùng để upload file xml
lên
sau đó trong code behind
aspx.vb


Dim dbTable As DataTable = ds.Tables(0)
'duyet den tung dong trong table
For Each row As DataRow In dbTable.Rows
'gan cho moi bien vao cac truong
strEmail = row("email").ToString()
strPassword = row("password").ToString()
strUserName = row("username").ToString()

If strEmail = vbNullString Then
strEmail = " "
End If

If strPassword = vbNullString Then
strPassword = " "
End If

If strUserName = vbNullString Then
strUserName = " "
End If

'tao user
Me.CreateUser(strUserName, strPassword,
strEmail)
'tang gia tri insert len 1 sau moi lan
insert
ValueInsert = ValueInsert + 1

Next
'xoa file vua upload sau khi import xong
File.Delete(strTempPath &

If dataConn.State = ConnectionState.Closed Then
dataConn.ConnectionString = vbNullString
dataConn.ConnectionString = strDataString
dataConn.Open()
End If

If dataComm Is Nothing Then dataComm = New
System.Data.SqlClient.SqlCommand
dataComm.Connection = dataConn
dataComm.CommandTimeout = 20
dataComm.CommandType = CommandType.Text

'Query Insert
Dim strQuery As String = "INSERT INTO
[tbl_User]([UserName],[Password],[Email]) VALUES('" & NickName
& "','" & Password & "','" & Email & "')"
'Execute
dataComm.CommandText = strQuery
dataComm.ExecuteNonQuery()

'dong connect sau khi insert
If Not dataConn Is Nothing Then If dataConn.State =
ConnectionState.Open Then dataConn.Close()
dataConn = Nothing
Catch ex As Exception
Throw New ApplicationException(ex.Message)
End Try
End Sub


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status