Xml sql bcp
Fermé
ranianwayra
Messages postés
3
Date d'inscription
mercredi 23 avril 2008
Statut
Membre
Dernière intervention
26 avril 2008
-
23 avril 2008 à 16:58
ranainwayra - 6 mai 2008 à 15:03
ranainwayra - 6 mai 2008 à 15:03
A voir également:
- Xml sql bcp
- Blob sql ✓ - Forum Webmastering
- Lecteur xml - Télécharger - Édition & Programmation
- Sql (+) - Forum Programmation
- Sql commence par ✓ - Forum Webmastering
- Sequence sql - Astuces et Solutions
1 réponse
Salut,
Il y à un très bon article sur ce sujet, sur le XML avec sql server ici : https://rudi.developpez.com/sqlserver/tutoriel/xquery/
Sinon tu peu aussi regarder les tutoriels :
- SQL Server : https://sqlserver.developpez.com/cours/
- XML : https://xml.developpez.com/cours/
- VB.NET : https://dotnet.developpez.com/cours/?page=vbnet
Il y à un très bon article sur ce sujet, sur le XML avec sql server ici : https://rudi.developpez.com/sqlserver/tutoriel/xquery/
Sinon tu peu aussi regarder les tutoriels :
- SQL Server : https://sqlserver.developpez.com/cours/
- XML : https://xml.developpez.com/cours/
- VB.NET : https://dotnet.developpez.com/cours/?page=vbnet
25 avril 2008 à 17:57
enfin g réussi à rédiger un bout de code pour résoudre le pb, le voila:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Private Sub ImportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportData.Click
'create a dataSet
Dim dataSetXml As DataSet
Dim dataTableXml As DataTable
Dim connString, sqlCmd, tableName As String
Dim i, x As Integer
Dim conn As SqlConnection
'Load the xml file into a dataSet
'DataSetXml.ReadXml(httpContext.Current.Server.MapPath(textBoxXml.Text));
dataSetXml.ReadXml(textBoxXml.Text)
'Get the first table in the DataSet
dataTableXml = dataSetXml.Tables(0)
'verify the table contains data
If (dataTableXml.Rows.Count > 0) Then
'cerate an sql connection
connString = TextBoxSql.Text
conn.Open()
'create a temporary table in the dataBase
'use the xml file's name for the temporary table
tableName = TextBoxXml.Text.Substring(0, TextBoxXml.TextLength - 4)
'make the create table SQL command by iterating throught the xml file's coulumns
'This way the dataBase columns will have the same name as the xml file
sqlCmd = "create table #" + tableName + "("
For i = 0 To dataTableXml.Columns.Count
'this adds columns as string type with a length of 100
sqlCmd = sqlCmd + dataTableXml.Columns(i).ColumnName.ToString() + "char(100),"
Next
'remove the last ","
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ")"
'create and exute the create table command
Dim command As SqlCommand(sqlCmd,conn)
Command(sqlCmd, conn).ExecuteNonQuery()
'iterate rows in the dataTable
For Each dr As DataRow In dataTableXml.Rows
'create the sql insert command for each row
sqlCmd = "insert into [" + tableName + "] ("
'iterate the dataTable columns
For i = 0 To dataTableXml.Columns.Count
'add the column name
sqlCmd = sqlCmd + dataTableXml.Columns(i).ColumnName.ToString() + ","
Next
'remove the last ","
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ") values ("
'iterate the dataTable columns
For x = 0 To dataTableXml.Columns.Count
'add the column value for this row
sqlCmd = sqlCmd + "'" + dr(x).ToString().Replace("'", "''") + "'"
Next
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");"
'create and execute the insert command
Dim command As SqlCommand(sqlCmd,conn)
Command(sqlCmd, conn)
Next
End If
End Sub
End Class
l'affichage ne serait pas agréable, désolée, j'espère qu' un gentil modérateur le prendra en charge
Bref, il ya qd même des erreurs (2 principales qui consernent l'objet command)
si vous découvrez la solution merci de m'informer
le code a un autre incovénient c qu'il n'est pas modulaire, j'essayerai de le faire plus tard
je suis débutante en codage et j'apprécierai tout conseil de la part des menmbres
à bien tôt ;)
26 avril 2008 à 10:11
voila une rectification à faire dans le code:
il faut remplacer la ligne de commande : "Command(sqlCmd, conn) " qui figure deux foix dans le code par ces 3 lignes:
command.Connection = conn
command.CommandText = sqlCmd
command.ExecuteNonQuery()
ainsi il n'ya plus d'erreurs de syntaxe
maintenant il faut vérifier le travail
je dois d'abord m'en sortir avec la chaine de connection
je vous tiens au courant si ça marche
et j'attends vos commentaires et conseils
à+
6 mai 2008 à 15:03
encore pas de commentaires! dommage
en fait g un autre problème dans ce code c en relation avec la structure du fichier xml que je dois importer dans ma base.
g besoin d'acéder aux neouds enfants aussi pour en extraire des données, saviez vous comment le faire
Merci de répondre