Xml sql bcp

ranianwayra Messages postés 4 Statut Membre -  
 ranainwayra -
Bonjour,
j'essaye d'importer des donées stockées dans un fichier xml vers ma base de données sql server 2005, je connais la structure du fichier et ça correspond exactement à celle de ma bd, j'utilise vb.net
g trouvé des informations sur l'utilitaire bcp, mais je le comprends pas vraiment
puvez vous m'expliquer comment ça marche?
des bouts de code si c possible,
sinon y a pas d'autres méthodes
parce que là je bloques

merci de répondre
A voir également:

1 réponse

Charles
 
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
0
ranianwayra Messages postés 4 Statut Membre
 
Merci bcp Charles,
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 ;)
0
ranianwayra Messages postés 4 Statut Membre > ranianwayra Messages postés 4 Statut Membre
 
Bonjour,

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

à+
0
ranainwayra > ranianwayra Messages postés 4 Statut Membre
 
Bonjour,
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
0