begin process at 2008 08 20 02:45:11
1 228 657 membres
37 nouveaux aujourd'hui
14 254 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

SQLCE EXTRAIRE LES DONNÉES D'UNE BASE AU FORMAT TEXTE


Information sur la source

Description

Ces quelques lignes permettent d'extraire les données d'une base de donnée
Le fichier Requete.txt doit-être dans le même répertoire que l'appli

Source

  • Imports System.IO
  • Imports System.Text
  • Imports System.Data.SqlServerCe
  • Module Data
  • #Region "Declarations"
  • Public CheminCourant As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase)
  • Public Const ListeSeparation = ";"
  • #End Region
  • Public Sub ShowErrors(ByVal e As SqlCeException)
  • Dim errorCollection As SqlCeErrorCollection = e.Errors
  • Dim bld As New StringBuilder
  • Dim inner As Exception = e.InnerException
  • If Not inner Is Nothing Then
  • MessageBox.Show(("Inner Exception: " & inner.ToString()))
  • End If
  • Dim err As SqlCeError
  • ' Enumerate each error to a message box.
  • For Each err In errorCollection
  • bld.Append((ControlChars.Cr & " Error Code: " & err.HResult.ToString("X")))
  • bld.Append((ControlChars.Cr & " Message : " & err.Message))
  • bld.Append((ControlChars.Cr & " Minor Err.: " & err.NativeError))
  • bld.Append((ControlChars.Cr & " Source : " & err.Source))
  • ' Retrieve the error parameter numbers for each error.
  • Dim numPar As Integer
  • For Each numPar In err.NumericErrorParameters
  • If 0 <> numPar Then
  • bld.Append((ControlChars.Cr & " Num. Par. : " & numPar))
  • End If
  • Next numPar
  • ' Retrieve the error parameters for each error.
  • Dim errPar As String
  • For Each errPar In err.ErrorParameters
  • If [String].Empty <> errPar Then
  • bld.Append((ControlChars.Cr & " Err. Par. : " & errPar))
  • End If
  • Next errPar
  • MessageBox.Show(bld.ToString())
  • bld.Remove(0, bld.Length)
  • Next err
  • End Sub
  • Public Sub LireRequete()
  • Dim Sr As New StreamReader(CheminCourant & "\Requete.txt")
  • Dim StrInput, NomFichier, Requete, NomBase, Nombre As String
  • Try
  • NomBase = StrInput
  • Requete = Sr.ReadLine
  • Nombre = Sr.ReadLine
  • NomFichier = Sr.ReadLine
  • MsgBox("Requete " & Requete & "(" & CStr(Nombre) & ") sur " & NomBase & " pour " & NomFichier)
  • ExtractData(NomBase, Nombre, Requete, NomFichier)
  • Sr.Close()
  • Catch ex As Exception
  • ShowErrors(ex)
  • End Try
  • End Sub
  • Public Sub ExtractData(ByVal NomBase As String, ByVal Nombre As String, ByVal Requete As String, ByVal NomFichier As String)
  • Dim maconnexion As SqlCeConnection = Nothing
  • Dim Detail() As String
  • Dim Sw As New StreamWriter(CheminCourant & NomFichier, True)
  • Dim I, Compteur, Etat As Integer
  • Dim Tampon As String
  • Detail = Nombre.Split(ListeSeparation)
  • I = 0
  • Try
  • maconnexion = New SqlCeConnection(("Data Source=" & CheminCourant & NomBase))
  • maconnexion.Open()
  • Dim mycmd As SqlCeCommand
  • Dim MonResultat As SqlCeDataReader
  • mycmd = maconnexion.CreateCommand
  • mycmd.CommandText = Requete
  • MonResultat = mycmd.ExecuteReader
  • While MonResultat.Read()
  • If Detail(1) = "str" Then Tampon = MonResultat.GetString(I)
  • If Detail(1) = "int" Then Tampon = CStr(MonResultat.GetInt16(I))
  • If CInt(Detail(0)) > 0 Then
  • For I = 1 To CInt(Detail(0))
  • Tampon = Tampon & ListeSeparation
  • If Detail(I + 1) = "str" Then Tampon = Tampon & MonResultat.GetString(I)
  • If Detail(I + 1) = "int" Then Tampon = Tampon & CStr(MonResultat.GetInt16(I))
  • Next
  • End If
  • Sw.WriteLine(Tampon)
  • Tampon = ""
  • I = 0
  • Compteur += 1
  • End While
  • Sw.Flush()
  • Sw.Close()
  • maconnexion.Dispose()
  • maconnexion.Close()
  • MsgBox(Compteur & " enregistrements exportés dans " & NomFichier)
  • Catch ex As Exception
  • MsgBox(ex.Message & "-" & CStr(Compteur), MsgBoxStyle.Exclamation)
  • ShowErrors(ex)
  • End Try
  • End Sub
  • End Module
Imports System.IO
Imports System.Text
Imports System.Data.SqlServerCe
Module Data
#Region "Declarations"
    Public CheminCourant As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase)
    Public Const ListeSeparation = ";"
#End Region
    Public Sub ShowErrors(ByVal e As SqlCeException)
        Dim errorCollection As SqlCeErrorCollection = e.Errors

        Dim bld As New StringBuilder
        Dim inner As Exception = e.InnerException

        If Not inner Is Nothing Then
            MessageBox.Show(("Inner Exception: " & inner.ToString()))
        End If

        Dim err As SqlCeError

        ' Enumerate each error to a message box.
        For Each err In errorCollection
            bld.Append((ControlChars.Cr & " Error Code: " & err.HResult.ToString("X")))
            bld.Append((ControlChars.Cr & " Message   : " & err.Message))
            bld.Append((ControlChars.Cr & " Minor Err.: " & err.NativeError))
            bld.Append((ControlChars.Cr & " Source    : " & err.Source))

            ' Retrieve the error parameter numbers for each error.
            Dim numPar As Integer
            For Each numPar In err.NumericErrorParameters
                If 0 <> numPar Then
                    bld.Append((ControlChars.Cr & " Num. Par. : " & numPar))
                End If
            Next numPar

            ' Retrieve the error parameters for each error.
            Dim errPar As String
            For Each errPar In err.ErrorParameters
                If [String].Empty <> errPar Then
                    bld.Append((ControlChars.Cr & " Err. Par. : " & errPar))
                End If
            Next errPar

            MessageBox.Show(bld.ToString())
            bld.Remove(0, bld.Length)
        Next err
    End Sub
    Public Sub LireRequete()
        Dim Sr As New StreamReader(CheminCourant & "\Requete.txt")
        Dim StrInput, NomFichier, Requete, NomBase, Nombre As String
        Try
            NomBase = StrInput
            Requete = Sr.ReadLine
            Nombre = Sr.ReadLine
            NomFichier = Sr.ReadLine
            MsgBox("Requete " & Requete & "(" & CStr(Nombre) & ") sur " & NomBase & " pour " & NomFichier)
            ExtractData(NomBase, Nombre, Requete, NomFichier)
            Sr.Close()
        Catch ex As Exception
            ShowErrors(ex)
        End Try
    End Sub
    Public Sub ExtractData(ByVal NomBase As String, ByVal Nombre As String, ByVal Requete As String, ByVal NomFichier As String)
        Dim maconnexion As SqlCeConnection = Nothing
        Dim Detail() As String
        Dim Sw As New StreamWriter(CheminCourant & NomFichier, True)
        Dim I, Compteur, Etat As Integer
        Dim Tampon As String
        Detail = Nombre.Split(ListeSeparation)
        I = 0
        Try
            maconnexion = New SqlCeConnection(("Data Source=" & CheminCourant & NomBase))
            maconnexion.Open()
            Dim mycmd As SqlCeCommand
            Dim MonResultat As SqlCeDataReader
            mycmd = maconnexion.CreateCommand
            mycmd.CommandText = Requete
            MonResultat = mycmd.ExecuteReader
            While MonResultat.Read()
                If Detail(1) = "str" Then Tampon = MonResultat.GetString(I)
                If Detail(1) = "int" Then Tampon = CStr(MonResultat.GetInt16(I))
                If CInt(Detail(0)) > 0 Then
                    For I = 1 To CInt(Detail(0))
                        Tampon = Tampon & ListeSeparation
                        If Detail(I + 1) = "str" Then Tampon = Tampon & MonResultat.GetString(I)
                        If Detail(I + 1) = "int" Then Tampon = Tampon & CStr(MonResultat.GetInt16(I))
                    Next
                End If
                Sw.WriteLine(Tampon)
                Tampon = ""
                I = 0
                Compteur += 1
            End While
            Sw.Flush()
            Sw.Close()
            maconnexion.Dispose()
            maconnexion.Close()
            MsgBox(Compteur & " enregistrements exportés dans " & NomFichier)
        Catch ex As Exception
            MsgBox(ex.Message & "-" & CStr(Compteur), MsgBoxStyle.Exclamation)
            ShowErrors(ex)
        End Try
    End Sub
End Module

Conclusion

exemple d'un fichier Requete.txt
\My Documents\MaBase.sdf
SELECT Nom, Prenom, Points FROM Score
4;str;str;int
\Score.csv

Il est très pratique d'utiliser query analyser pour naviguer dans les base et connaître le nom des champs
  • signaler à un administrateur
    Commentaire de OULKABIR le 11/08/2006 13:19:28

    TRES BIEN

    MAIS JE VEUX SAVOIR COMMENT JE PEUT SE CONNECTER DIRECTEMENT A SQL SERVER 2000 SANS PASSER PAR SQLCE
    MAJ DES DONNEES DIRECT SUR SQL SERVER 2000 AVEC POCKET PC
    MERCI
    oulkabirabd@hotmail.com

  • signaler à un administrateur
    Commentaire de ciagar le 01/02/2007 15:47:02

    Merci d'avoir partager ce code. Il va surement me servir.

  • signaler à un administrateur
    Commentaire de tudt_phochome le 11/05/2007 05:20:25

    help me
    i want one web prorgamming with mobile (asp.net)

  • signaler à un administrateur
    Commentaire de Stephane33 le 11/05/2007 19:43:51 administrateur CS

    Désolé TUDT mais le français est de rigueur ;
    You can see  this animated presentation here http://www.asp.net/mobile/flasharchitecture.aspx?tabindex=3&tabID=44
    But I do not practise Asp.net

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS