1. Trang chủ
  2. » Công Nghệ Thông Tin

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

4 276 2

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 95,1 KB

Nội dung

đặ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>admin@ash.com</email> </user> <user> <userid>2</userid> <username>user12</username> <password>e493716f73d8e1c410bfbfe3e910d3af</password> <email>user12@ash.com</email> </user> <user> <userid>3</userid> <username>user1</username> <password>f65696f9d46fa5b8c70b2e20c045cdbf</password> <email>user1@ash.com</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 Public ValueInsert As Integer = 0 Private Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click Try Dim strEmail As String Dim strPassword As String Dim strUserName As String 'luu ra folder tam tren may chu Dim strTempPath As String = Server.MapPath("~/Temp/") 'upload len folder tam If fileUpload.HasFile Then 'chi upload file xml If fileUpload.PostedFile.ContentType = "text/xml" Then 'luu xuong may chu fileUpload.SaveAs(strTempPath & fileUpload.FileName) 'khoi tao dataset Dim ds As DataSet = New DataSet() 'load file sau khi upload vao stream Dim fsReadXml As System.IO.FileStream = New IO.FileStream(strTempPath & fileUpload.FileName, IO.FileMode.Open) 'dataset doc file xml tu stream ds.ReadXml(fsReadXml) 'dong stream fsReadXml.Close() 'khoi tao 1 datatable de dua dataset vao 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 & fileUpload.FileName) Response.Write("* Delete Temp File<br />") 'ok Response.Write("* Import Complete<br />") 'ghi ra da insert bao nhieu ban ghi Response.Write("* Insert " & ValueInsert.ToString() & " records.<br />") Else Response.Write("* Please choose XML File") End If Else Response.Write("* File Upload Require") End If Catch ex As Exception Response.Write(ex.Message) End Try end sub Public Sub CreateUser(ByVal NickName As String, ByVal Password As String, ByVal Email As String, ByVal Salt As String) Try 'khoi tao ket noi dim dataConn As System.Data.SqlClient.SqlConnection dim dataComm As System.Data.SqlClient.SqlCommand If dataConn Is Nothing Then dataConn = New System.Data.SqlClient.SqlConnection '* neu trang thai cua dataConn la Closed thi moi mo them ket noi 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 . & fileUpload.FileName, IO.FileMode.Open) &apos ;dataset doc file xml tu stream ds.ReadXml(fsReadXml) 'dong stream fsReadXml.Close() 'khoi tao 1 datatable de dua dataset vao. 'upload len folder tam If fileUpload.HasFile Then 'chi upload file xml If fileUpload.PostedFile.ContentType = "text /xml& quot; Then 'luu xuong may chu fileUpload.SaveAs(strTempPath. fileUpload.SaveAs(strTempPath & fileUpload.FileName) 'khoi tao dataset Dim ds As DataSet = New DataSet( ) &apos ;load file sau khi upload vao stream Dim fsReadXml As System.IO.FileStream =

Ngày đăng: 29/07/2014, 06:21

TỪ KHÓA LIÊN QUAN

w