Jump to content
Sign in to follow this  
gastao

Gravar em ficheiro TXT

Recommended Posts

gastao

boas tou a tentar ler uma query de uma base de dados e gravar o seu resultado num .TXT andei na pesquisa e tou aqui com umas pequenas duvidas de variaveis. alguem me consegue dar uma ajuda

Dim strIniFile As String = My.Application.Info.DirectoryPath & "\config.ini"

'Le qual o caminho das Facturas escrito no Ini

Dim bdsql As String = GetIniSetting("CaminhoBaseDados", "MudaDocumentosSage", strIniFile)
Dim bdnome As String = GetIniSetting("NomeBaseDados", "MudaDocumentosSage", strIniFile)
Dim bduser As String = GetIniSetting("User", "MudaDocumentosSage", strIniFile)
Dim bdpass As String = GetIniSetting("Password", "MudaDocumentosSage", strIniFile)
Dim nomecliente As String = GetIniSetting("Cliente", "MudaDocumentosSage", strIniFile)

'conexao SQL
Dim consql As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection("Data Source='" & bdsql & "';Initial Catalog='" & bdnome & "'; Integrated Security=false; User Id='" & bduser & "';Password='" & bdpass & "';")

'query
Dim query As String = "SELECT SaleTransactionDetails.ItemID, SaleTransactionDetails.Quantity, SaleTransactionDetails.TaxIncludedPrice FROm SaleTransactionDetails INNER JOIN Item ON SaleTransactionDetails.ItemID = Item.ItemID WHERE SaleTransactionDetails.DtAlt = '2012-02-01'"
Dim command As New SqlCommand(query, consql)
command.ExecuteNonQuery()
dr = command.ExecuteReader

Dim sb As New StringBuilder
While dr.Read()
'vai ler linha por linha StringBuilder
sb.AppendLine(String.Format("{0} | {1} | {2} | {3}",
dr.item("ItemID"), dr.item("Quantity"), dr.item("TaxIncludedPrice")))
End While

'ondecolocar o ficheiro
File.WriteAllLines("D:\teste.txt", sb.ToString())

ta me a dar -> Warning 1 Runtime errors might occur when converting 'String' to 'System.Collections.Generic.IEnumerable(Of String)'.

Edited by gastao
GeSHi

Share this post


Link to post
Share on other sites
NunoDinis

poe uma msgbox do sb e diz-me o que ele retorna sff.

em todo o caso o erro esta em estares a converter o sb para string... vamos ver a melhor maneira de como podemos fazer isso.


Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro.

ndsotware.org

Share this post


Link to post
Share on other sites
gastao

MsgBox(sb.ToString)

retoma aquilo que eu pretendo.

por exemplo:

1000|1|-33,34

1000|5|3,29

1000|2,4|2,34

1000|1,42|1,24

...

e por ai fora isto na msgbox ta bacano ;) so que para o file e mentira :(

isto quando eu meto o

File.WriteAllLines("c:\test.txt", sb.ToString()) no sb.tostring aparece me a sublinhado a dar aquela situaçao que coloquei em cima

Edited by Rui Carlos

Share this post


Link to post
Share on other sites
gastao

File.WriteAllText("c:\test.txt", sb.ToString)

desta maneira funciona ;) agora e so manipular os dados desejados para la ;)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.